*/ public function register() { return [\T_CLASS]; } /** * Processes this test, when one of its tokens is encountered. * * @since 7.0.0 * @since 7.0.8 The message is downgraded from error to warning as - for now - support * for PHP4-style constructors is just deprecated, not yet removed. * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(File $phpcsFile, $stackPtr) { if (ScannedCode::shouldRunOnOrAbove('7.0') === \false || ScannedCode::shouldRunOnOrBelow('7.4') === \false) { return; } if (Namespaces::determineNamespace($phpcsFile, $stackPtr) !== '') { /* * Namespaced methods with the same name as the class are treated as * regular methods, so we can bow out if we're in a namespace. * * Note: the exception to this is PHP 5.3.0-5.3.2. This is currently * not dealt with. */ return; } $className = ObjectDeclarations::getName($phpcsFile, $stackPtr); if (empty($className) || \is_string($className) === \false) { return; } $ooMethods = ObjectDeclarations::getDeclaredMethods($phpcsFile, $stackPtr); if (empty($ooMethods)) { // No methods declared in the OO construct at all. Bow out. return; } $ooMethodsLC = \array_change_key_case($ooMethods, \CASE_LOWER); $newConstructorFound = isset($ooMethodsLC['__construct']); $oldConstructorFound = \false; $oldConstructorPos = -1; foreach ($ooMethods as $methodName => $functionPtr) { if (NamingConventions::isEqual($methodName, $className) === \true) { $oldConstructorFound = \true; $oldConstructorPos = $functionPtr; break; } } if ($newConstructorFound === \false && $oldConstructorFound === \true) { $error = 'Declaration of a PHP4 style class constructor is deprecated since PHP 7.0'; $code = 'Deprecated'; $isError = \false; if (ScannedCode::shouldRunOnOrAbove('8.0') === \true) { $error .= ' and removed since PHP 8.0'; $code = 'Removed'; $isError = \true; } MessageHelper::addMessage($phpcsFile, $error, $oldConstructorPos, $isError, $code); } } }