本文整理汇总了PHP中PHP_CodeSniffer_File::getDeclarationName方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer_File::getDeclarationName方法的具体用法?PHP PHP_CodeSniffer_File::getDeclarationName怎么用?PHP PHP_CodeSniffer_File::getDeclarationName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer_File
的用法示例。
在下文中一共展示了PHP_CodeSniffer_File::getDeclarationName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$scope = $phpcsFile->getDeclarationName($stackPtr);
$class = $phpcsFile->getDeclarationName($phpcsFile->findPrevious([T_CLASS, T_INTERFACE], $stackPtr));
$tokens = $phpcsFile->getTokens();
$function = $tokens[$stackPtr];
if (!isset($function['scope_opener'])) {
// No scope means it's an abstract/interface function declaration
return;
}
$scopeTypes = [T_IF, T_ELSE, T_ELSEIF, T_FOR, T_FOREACH, T_SWITCH, T_WHILE];
$functionStartPtr = $function['scope_opener'];
$functionEndPtr = $function['scope_closer'];
$ptr = $functionStartPtr;
// var_dump($class, $scope);
while ($ptr = $phpcsFile->findNext($scopeTypes, $ptr + 1, $functionEndPtr)) {
$token = $tokens[$ptr];
if ($token['code'] != T_IF && $token['code'] != T_ELSE && $token['code'] != T_ELSEIF) {
$this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr - 1);
$this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr);
} elseif ($token['code'] == T_IF) {
$this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr);
$this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr, [T_ELSE, T_ELSEIF]);
} elseif ($token['code'] == T_ELSEIF) {
$this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr, [T_CLOSE_CURLY_BRACKET]);
$this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr, [T_ELSE, T_ELSEIF]);
} elseif ($token['code'] == T_ELSE) {
$this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr, [T_CLOSE_CURLY_BRACKET]);
$this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr);
}
}
}
示例2: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
// Ignore magic methods.
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = strtolower(substr($methodName, 2));
if (isset($this->magicMethods[$magicPart]) === true || isset($this->methodsDoubleUnderscore[$magicPart]) === true) {
return;
}
}
$testName = ltrim($methodName, '_');
if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) {
$error = 'Method name "%s" is not in camel caps format';
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}
示例3: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->magicMethods) === false) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// Methods should not contain underscores.
if (strpos($methodName, '_') !== false) {
if ($scopeSpecified === true) {
$error = '%s method name "%s" is not in lowerCamel format, it must not contain underscores';
$data = array(ucfirst($scope), $errorData[0]);
$phpcsFile->addError($error, $stackPtr, 'ScopeNotLowerCamel', $data);
} else {
$error = 'Method name "%s" is not in lowerCamel format, it must not contain underscores';
$phpcsFile->addError($error, $stackPtr, 'NotLowerCamel', $errorData);
}
}
}
示例4: process
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token in
* the token stack.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
$className = $phpcsFile->getDeclarationName($stackPtr);
$errorData = array(strtolower($tokens[$stackPtr]['content']));
$nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_TRAIT), $tokens[$stackPtr]['scope_closer'] + 1);
if ($nextClass !== false) {
$nextClassName = $phpcsFile->getDeclarationName($nextClass);
$extends = $phpcsFile->findExtendedClassName($nextClass);
if ($className == $nextClassName && $extends != $className && substr($extends, -1 * (strlen($className) + 1)) == '\\' . $className) {
// $nextClassName wraps $className in global namespace (probably)
$phpcsFile->recordMetric($stackPtr, 'One class per file', 'yes');
} else {
$error = 'Each %s must be in a file by itself';
$phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
$phpcsFile->recordMetric($stackPtr, 'One class per file', 'no');
}
} else {
$phpcsFile->recordMetric($stackPtr, 'One class per file', 'yes');
}
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
$namespace = $phpcsFile->findNext(array(T_NAMESPACE, T_CLASS, T_INTERFACE, T_TRAIT), 0);
if ($tokens[$namespace]['code'] !== T_NAMESPACE) {
$error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
$phpcsFile->addWarning($error, $stackPtr, 'MissingNamespace', $errorData);
$phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'Class defined in namespace', 'yes');
}
}
}
示例5: processTokenWithinScope
/**
* Processes this test when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if (strcasecmp($methodName, $className) === 0) {
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
$phpcsFile->addError($error, $stackPtr, 'OldStyle');
} else {
if (strcasecmp($methodName, '__construct') !== 0) {
// Not a constructor.
return;
}
}
$tokens = $phpcsFile->getTokens();
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
if ($parentClassName === false) {
return;
}
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
$startIndex = $stackPtr;
while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {
if ($tokens[$doubleColonIndex + 1]['code'] === T_STRING && $tokens[$doubleColonIndex + 1]['content'] === $parentClassName) {
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
$phpcsFile->addError($error, $doubleColonIndex + 1, 'OldStyleCall');
}
$startIndex = $doubleColonIndex + 1;
}
}
示例6: processTokenWithinScope
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if (!PHP_CodeSniffer::isCamelCaps($methodName, false, true, false)) {
$error = $className . '::' . $methodName . '() name is not in camel caps format';
$phpcsFile->addError($error, $stackPtr);
}
}
示例7: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->_magicMethods) === false) {
$error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$isPublic = $methodProps['scope'] === 'public' ? true : false;
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// If it's a private method, it must have an underscore on the front.
if ($isPublic === false && $methodName[0] !== '_') {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// If it's not a private method, it must not have an underscore on the front.
if ($isPublic === true && $scopeSpecified === true && $methodName[0] === '_') {
$error = "Public method name \"{$className}::{$methodName}\" must not be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// If the scope was specified on the method, then the method must be
// camel caps and an underscore should be checked for. If it wasn't
// specified, treat it like a public method and remove the underscore
// prefix if there is one because we cant determine if it is private or
// public.
$testMethodName = $methodName;
if ($scopeSpecified === false && $methodName[0] === '_') {
$testMethodName = substr($methodName, 1);
}
if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
if ($scopeSpecified === true) {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format";
} else {
$error = "Method name \"{$className}::{$methodName}\" is not in camel caps format";
}
$phpcsFile->addError($error, $stackPtr);
return;
}
}
示例8: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param integer $stackPtr The position where this token was found.
* @param integer $currScope The position of the current scope.
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
// Ignore magic methods
if (preg_match('/^__(' . implode('|', $this->_magicMethods) . ')$/', $methodName)) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if ($methodProps['scope_specified'] === false) {
// Let another sniffer take care of that
return;
}
$isPublic = $methodProps['scope'] === 'public';
$isProtected = $methodProps['scope'] === 'protected';
$isPrivate = $methodProps['scope'] === 'private';
$scope = $methodProps['scope'];
if ($isPublic === true) {
if ($methodName[0] === '_') {
$error = 'Public method name "%s" must not be prefixed with underscore';
$phpcsFile->addError($error, $stackPtr, 'PublicWithUnderscore', $errorData);
return;
}
// Underscored public methods in controller are allowed to break our rules.
if (substr($className, -10) === 'Controller') {
return;
}
// Underscored public methods in shells are allowed to break our rules.
if (substr($className, -5) === 'Shell') {
return;
}
// Underscored public methods in tasks are allowed to break our rules.
if (substr($className, -4) === 'Task') {
return;
}
} elseif ($isPrivate === true) {
$filename = $phpcsFile->getFilename();
$warning = 'Private method name "%s" in CakePHP core is discouraged';
$phpcsFile->addWarning($warning, $stackPtr, 'PrivateMethodInCore', $errorData);
}
}
示例9: processTokenWithinScope
/**
* Processes this test when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $currScope A pointer to the start of the scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
$className = $phpcsFile->getDeclarationName($currScope);
$isPhp4Constructor = strcasecmp($methodName, $className) === 0;
$isPhp5Constructor = strcasecmp($methodName, '__construct') === 0;
if ($this->php5Constructors != '0') {
if ($isPhp4Constructor) {
$error = "PHP4 style constructors are not allowed; use \"__construct\" instead";
$phpcsFile->addError($error, $stackPtr);
}
} else {
if ($isPhp5Constructor) {
$error = "PHP5 style constructors are not allowed; use \"{$className}\" instead";
$phpcsFile->addError($error, $stackPtr);
}
}
if (!$isPhp4Constructor && !$isPhp5Constructor) {
return;
}
$tokens = $phpcsFile->getTokens();
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
$wrongConstructor = '';
// prepares the error message and wrong constructor
if ($this->php5Constructors != '0') {
$error = 'PHP4 style calls to parent constructors are not allowed.';
$error = "{$error} Please use \"parent::__construct\" instead.";
if (false !== $parentClassName) {
$wrongConstructor = $parentClassName;
}
// Else $wrongConstructor will be empty
// and the test expression will always be false.
// It doesn't check that no parent method should be called
// when no parent class is defined.
} else {
$error = 'PHP5 style calls to parent constructors are not allowed.';
if (false !== $parentClassName) {
$error = "{$error} Please use \"parent::{$parentClassName}\" instead.";
}
$wrongConstructor = '__construct';
}
// looks for the use of a wrong constructor.
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
$doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $stackPtr, $endFunctionIndex);
while ($doubleColonIndex) {
if ($tokens[$doubleColonIndex + 1]['code'] === T_STRING && $tokens[$doubleColonIndex + 1]['content'] === $wrongConstructor) {
$phpcsFile->addError($error, $doubleColonIndex + 1);
}
$doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $doubleColonIndex + 1, $endFunctionIndex);
}
}
示例10: processTokenWithinScope
/**
* Override parent function to allow custom code validation
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->magicMethods) === false) {
$error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$isPublic = $methodProps['scope'] === 'private' ? false : true;
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
// If it's a private method, it must have an underscore on the front.
if ($isPublic === false && $methodName[0] !== '_') {
$error = "Private method name \"{$className}::{$methodName}\" must be prefixed with an underscore";
$phpcsFile->addError($error, $stackPtr);
return;
}
// We want to allow private and protected methods to start with an underscore
$testMethodName = $methodName;
if (($scopeSpecified === false || $scope == 'protected') && $methodName[0] === '_') {
$testMethodName = substr($methodName, 1);
}
if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
if ($scopeSpecified === true) {
$error = ucfirst($scope) . " method name \"{$className}::{$methodName}\" is not in camel caps format";
} else {
$error = "Method name \"{$className}::{$methodName}\" is not in camel caps format";
}
$phpcsFile->addError($error, $stackPtr);
return;
}
}
示例11: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$methodName = $phpcsFile->getDeclarationName($stackPtr);
// Is this a magic method. IE. is prefixed with "__".
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->_magicMethods) === false) {
$error = "Method name \"{$className}::{$methodName}\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore');
}
return;
}
// PHP4 constructors are allowed to break our rules.
if ($methodName === $className) {
return;
}
// PHP4 destructors are allowed to break our rules.
if ($methodName === '_' . $className) {
return;
}
// If this is a child class, it may have to use camelCase.
if ($phpcsFile->findExtendedClassName($currScope) || $this->findImplementedInterfaceName($currScope, $phpcsFile)) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
$scope = $methodProps['scope'];
$scopeSpecified = $methodProps['scope_specified'];
if ($methodProps['scope'] === 'private') {
$isPublic = false;
} else {
$isPublic = true;
}
// If the scope was specified on the method, then the method must be
// camel caps and an underscore should be checked for. If it wasn't
// specified, treat it like a public method and remove the underscore
// prefix if there is one because we can't determine if it is private or
// public.
$testMethodName = $methodName;
if ($scopeSpecified === false && $methodName[0] === '_') {
$testMethodName = substr($methodName, 1);
}
if (strtolower($testMethodName) !== $testMethodName) {
$suggested = preg_replace('/([A-Z])/', '_$1', $methodName);
$suggested = strtolower($suggested);
$suggested = str_replace('__', '_', $suggested);
$error = "Function name \"{$methodName}\" is in camel caps format, try '" . $suggested . "'";
$phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid');
}
}
示例12: processTokenWithinScope
/**
* Processes the tokens within the scope.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$testName = ltrim($methodName, '_');
if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) {
$error = 'Method name "%s" is not in camel caps format';
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = array($className . '::' . $methodName);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
}
示例13: process
/**
* Processes the tokens that this sniff is interested in.
*
* @param \PHP_CodeSniffer_File $phpcsFile The file where the token was found.
* @param int $stackPtr The position in the stack where the token was found.
*
* @return void
*/
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = PHP_CodeSniffer_Tokens::$methodPrefixes;
$find[] = T_WHITESPACE;
$isTagFound = false;
$functionName = '';
try {
$functionName = $phpcsFile->getDeclarationName($stackPtr);
} catch (PHP_CodeSniffer_Exception $e) {
error_log($e->getMessage());
}
// Process only test* functions
if (substr($functionName, 0, strlen(self::TEST_FUNCTION_PREFIX)) !== self::TEST_FUNCTION_PREFIX) {
return;
}
$commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
if (array_key_exists('comment_opener', $tokens[$commentEnd])) {
foreach ($tokens[$tokens[$commentEnd]['comment_opener']]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === self::ANNOTATION_COVERS) {
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
$isTagFound = true;
if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
$error = sprintf('Content missing for %s tag in test method comment', self::ANNOTATION_COVERS);
$phpcsFile->addError($error, $tag, 'EmptyCoversTag');
}
}
}
}
if ($isTagFound === false) {
$error = sprintf('Missing %s tag in test method comment', self::ANNOTATION_COVERS);
$phpcsFile->addError($error, $stackPtr, 'MissingCoversTag');
}
}
示例14: processReturn
/**
* Process the return comment of this function comment.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();
// Skip constructor and destructor.
$methodName = $phpcsFile->getDeclarationName($stackPtr);
$isSpecialMethod = $methodName === '__construct' || $methodName === '__destruct';
$return = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === '@return') {
if ($return !== null) {
$error = 'Only 1 @return tag is allowed in a function comment';
$phpcsFile->addError($error, $tag, 'DuplicateReturn');
return;
}
$return = $tag;
}
}
if ($isSpecialMethod === true) {
return;
}
if ($return !== null) {
$content = $tokens[$return + 2]['content'];
if (empty($content) === true || $tokens[$return + 2]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Return type missing for @return tag in function comment';
$phpcsFile->addError($error, $return, 'MissingReturnType');
} elseif (!$this->functionHasReturnStatement($phpcsFile, $stackPtr)) {
$error = 'Function return type is set, but function has no return statement';
$phpcsFile->addError($error, $return, 'InvalidNoReturn');
}
} elseif ($this->functionHasReturnStatement($phpcsFile, $stackPtr)) {
$error = 'Missing @return tag in function comment.';
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
}
//end if
}
示例15: processTokenWithinScope
/**
* Processes the function tokens within the class.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
* @param int $currScope The current scope opener token.
*
* @return void
*/
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
{
$tokens = $phpcsFile->getTokens();
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
if ($modifier === false || $tokens[$modifier]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Visibility must be declared on method "%s"';
$data = array($methodName);
$previous = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true);
// Only correct the trivial cases for now
if (!$modifier && $tokens[$modifier]['line'] === $tokens[$stackPtr]['line']) {
$phpcsFile->addFixableError($error, $stackPtr, 'Missing', $data);
$visbility = 'public';
$name = substr($tokens[$stackPtr]['content'], 1);
$totalUnderscores = 0;
while (strpos($name, '_') === 0) {
$totalUnderscores++;
$name = substr($name, 1);
}
if ($totalUnderscores > 1) {
$visbility = 'private';
} elseif ($totalUnderscores > 0) {
$visbility = 'protected';
}
} else {
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
}
//TODO
}
}