findExtendedClassName() public method

Returns FALSE on error or if there is no extended class name.
public findExtendedClassName ( integer $stackPtr ) : string
$stackPtr integer The stack position of the class.
return string
 /**
  * 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;
     }
 }
Example #2
0
 /**
  * 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');
         }
     }
 }
 /**
  * @param \PHP_CodeSniffer_File $phpCsFile
  * @param int $stackPointer
  *
  * @return bool
  */
 private function extendsAbstractPlugin(\PHP_CodeSniffer_File $phpCsFile, $stackPointer)
 {
     $extendedClassName = $phpCsFile->findExtendedClassName($stackPointer);
     if ($extendedClassName === 'AbstractPlugin') {
         return true;
     }
     return false;
 }
 public function process(CodeSnifferFile $file, $stackPtr)
 {
     if (!preg_match('@/[^/]+Bundle/Form/@', $file->getFilename())) {
         return;
     }
     if ($file->findExtendedClassName($stackPtr) !== 'AbstractType') {
         return;
     }
     $className = $file->getDeclarationName($stackPtr);
     if (!preg_match('/.*FormType$/D', $className)) {
         $file->addError(sprintf('Form types are expected to be named "<Something>FormType", "%s" found', $className), $stackPtr, 'Architecture.FormTypeConvention');
     }
 }
Example #5
0
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $className = $phpcsFile->getDeclarationName($stackPtr);
     $this->_dbClasses->registerClass($className, $phpcsFile->getFilename());
     $extends = $phpcsFile->findExtendedClassName($stackPtr);
     if ($extends !== false) {
         $this->_dbClasses->registerClassExtends($className, $extends);
     }
     $implements = $phpcsFile->findImplementsClassName($stackPtr);
     if (count($implements) > 0) {
         $this->_dbClasses->registerClassImplements($className, $implements);
     }
 }
 /**
  * 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);
     }
 }
 /**
  * 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');
     }
 }