Returns true if the specified string is in the underscore caps format.
public static isUnderscoreName ( string $string ) : boolean | ||
$string | string | The string to verify. |
return | boolean |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (empty($tokens[$stackPtr]['conditions']) === true) { $functionName = $phpcsFile->getDeclarationName($stackPtr); if (false === strpos($phpcsFile->getFilename(), 'helper')) { $error = 'Defining functions is NOT permitted expecting helper functions.'; $phpcsFile->addError($error, $stackPtr); } if (!PHP_CodeSniffer::isUnderscoreName($functionName)) { $error = 'Function name must use underscores in accordance.' . $functionName; $phpcsFile->addError($error, $stackPtr); } } }
/** * Process the package tag. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param array $tags The tokens for these tags. * */ protected function processPackage(PHP_CodeSniffer_File $phpcsFile, array $tags) { $tokens = $phpcsFile->getTokens(); foreach ($tags as $tag) { if ($tokens[$tag + 2]['code'] !== T_DOC_COMMENT_STRING) { // No content. continue; } $namespace = $this->extractNamespace($phpcsFile); $content = $tokens[$tag + 2]['content']; if ($namespace !== $content) { $phpcsFile->addError('Package name "%s" should be "%s"', $tag, 'InvalidPackage', [$content, $namespace]); continue; } if (PHP_CodeSniffer::isUnderscoreName($content) === true) { continue; } $newContent = str_replace(' ', '_', $content); $newContent = trim($newContent, '_'); $newContent = preg_replace('/[^A-Za-z_]/', '', $newContent); $nameBits = explode('_', $newContent); $firstBit = array_shift($nameBits); $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_'; foreach ($nameBits as $bit) { if ($bit !== '') { $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_'; } } $error = 'Package name "%s" is not valid; consider "%s" instead'; $validName = trim($newName, '_'); $data = [$content, $validName]; $phpcsFile->addError($error, $tag, 'InvalidPackage', $data); } }
/** * Process the subpackage tag. * * @param int $errorPos The line number where the error occurs. * * @return void */ protected function processSubpackage($errorPos) { $package = $this->commentParser->getSubpackage(); if ($package !== null) { $content = $package->getContent(); if ($content !== '') { if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { $newContent = str_replace(' ', '_', $content); $nameBits = explode('_', $newContent); $firstBit = array_shift($nameBits); $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_'; foreach ($nameBits as $bit) { $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_'; } $validName = trim($newName, '_'); $error = "Subpackage name \"{$content}\" is not valid; consider \"{$validName}\" instead"; $this->currentFile->addError($error, $errorPos); } } else { $error = '@subpackage tag must contain a name'; $this->currentFile->addError($error, $errorPos); } } }
/** * Process the subpackage tag. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param array $tags The tokens for these tags. * * @return void */ protected function processSubpackage(PHP_CodeSniffer_File $phpcsFile, array $tags) { $tokens = $phpcsFile->getTokens(); foreach ($tags as $tag) { if ($tokens[$tag + 2]['code'] !== T_DOC_COMMENT_STRING) { // No content. continue; } $content = $tokens[$tag + 2]['content']; if (PHP_CodeSniffer::isUnderscoreName($content) === true) { continue; } $newContent = str_replace(' ', '_', $content); $nameBits = explode('_', $newContent); $firstBit = array_shift($nameBits); $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_'; foreach ($nameBits as $bit) { if ($bit !== '') { $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_'; } } $error = 'Subpackage name "%s" is not valid; consider "%s" instead'; $validName = trim($newName, '_'); $data = array($content, $validName); $phpcsFile->addError($error, $tag, 'InvalidSubpackage', $data); } //end foreach }
/** * The subpackage name must be camel-cased. * * @param int $errorPos The line number where the error occurs. * * @return void */ protected function processSubpackage($errorPos) { $subpackage = $this->commentParser->getSubpackage(); if ($subpackage !== null) { $content = $subpackage->getContent(); if (empty($content) === true) { $error = 'Content missing for @subpackage tag in file comment'; $this->currentFile->addError($error, $errorPos); } else { if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { // Subpackage name must be properly camel-cased. $nameBits = explode('_', $content); $firstBit = array_shift($nameBits); $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_'; foreach ($nameBits as $bit) { $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_'; } $validName = trim($newName, '_'); $error = "Subpackage name \"{$content}\" is not valid; "; $error .= "consider \"{$validName}\" instead"; $this->currentFile->addError($error, $errorPos); } } } }
/** * Process the subpackage tag. * * @param int $errorPos The line number where the error occurs. * * @return void */ protected function processSubpackage($errorPos) { $package = $this->commentParser->getSubpackage(); if ($package !== null) { $content = $package->getContent(); if ($content !== '') { if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { // $newContent = str_replace(' ', '_', $content); // $nameBits = explode('_', $newContent); // $firstBit = array_shift($nameBits); // $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_'; // foreach ($nameBits as $bit) { // $newName .= strtoupper($bit{0}).substr($bit, 1).'_'; // } // // $error = 'Subpackage name "%s" is not valid; consider "%s" instead'; // $validName = trim($newName, '_'); // $data = array( // $content, // $validName, // ); // $this->currentFile->addError($error, $errorPos, 'InvalidSubpackage', $data); } } else { $error = '@subpackage tag must contain a name'; $this->currentFile->addError($error, $errorPos, 'EmptySubpackage'); } } }
/** * Process the subpackage tag. * * @param int $errorPos The line number where the error occurs. * * @return void */ protected function processSubpackage($errorPos) { $package = $this->commentParser->getSubpackage(); if ($package !== null) { $content = $package->getContent(); if ($content !== '') { if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { // Ignore.. } } else { $error = '@subpackage tag must contain a name'; $this->currentFile->addError($error, $errorPos); } } }//end processSubpackage()
/** * check subpackage tag * * @param string $content Tag content * @access protected * @return array * @since 1.0.0 */ protected function checkSubpackage($content) { $result = array(true, $content); if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { $result = array(false, $this->sanitazeUnderscoreName($content)); } return $result; }
/** * Process the depreciated tag * * @return void */ public function getUses() { $uses = $this->commentParser->getUses(); if ($uses !== null) { foreach ($uses as $use) { $content = $use->getContent(); if ($content !== '') { if (PHP_CodeSniffer::isUnderscoreName($content) !== true) { $newContent = str_replace(' ', '_', $content); $nameBits = explode('_', $newContent); $firstBit = array_shift($nameBits); $newName = strtoupper($firstBit[0]) . substr($firstBit, 1) . '_'; foreach ($nameBits as $bit) { $newName .= strtoupper($bit[0]) . substr($bit, 1) . '_'; } $validName = trim($newName, '_'); $error = "34Uses name \"{$content}\" is not valid; consider \"{$validName}\" instead"; $this->currentFile->addError($error, $errorPos, 'UsesNameClassComment'); } } else { $error = '35@uses tag must contain a name'; $this->currentFile->addError($error, $errorPos, 'UsesTagMustContainNameClassComment'); } } } }