Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/ConstructorName: fix bug with nested anonymous classes and more #2182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,49 @@ public function __construct()
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$className = $phpcsFile->getDeclarationName($currScope);
$tokens = $phpcsFile->getTokens();

// Determine if this is a function which needs to be examined.
$conditions = $tokens[$stackPtr]['conditions'];
end($conditions);
$deepestScope = key($conditions);
if ($deepestScope !== $currScope) {
return;
}

$className = strtolower($phpcsFile->getDeclarationName($currScope));
if ($className !== $this->currentClass) {
$this->loadFunctionNamesInScope($phpcsFile, $currScope);
$this->currentClass = $className;
}

$methodName = $phpcsFile->getDeclarationName($stackPtr);
$methodName = strtolower($phpcsFile->getDeclarationName($stackPtr));

if (strcasecmp($methodName, $className) === 0) {
if (in_array('__construct', $this->functionList) === false) {
if ($methodName === $className) {
if (in_array('__construct', $this->functionList, true) === false) {
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
$phpcsFile->addError($error, $stackPtr, 'OldStyle');
}
} else if (strcasecmp($methodName, '__construct') !== 0) {
} else if ($methodName !== '__construct') {
// Not a constructor.
return;
}

$tokens = $phpcsFile->getTokens();

$parentClassName = $phpcsFile->findExtendedClassName($currScope);
if ($parentClassName === false) {
// Stop if the constructor doesn't have a body, like when it is abstract.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}

// Stop if the constructor doesn't have a body, like when it is abstract.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
$parentClassName = strtolower($phpcsFile->findExtendedClassName($currScope));
if ($parentClassName === false) {
return;
}

$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
$startIndex = $stackPtr;
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
&& $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
&& strtolower($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');
Expand Down Expand Up @@ -136,8 +144,12 @@ protected function loadFunctionNamesInScope(File $phpcsFile, $currScope)
continue;
}

$next = $phpcsFile->findNext(T_STRING, $i);
$this->functionList[] = trim($tokens[$next]['content']);
$this->functionList[] = trim(strtolower($phpcsFile->getDeclarationName($i)));

if (isset($tokens[$i]['scope_closer']) !== false) {
// Skip past nested functions and such.
$i = $tokens[$i]['scope_closer'];
}
}

}//end loadFunctionNamesInScope()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestClass extends MyClass
}

function __construct() {
parent::MyClass();
parent::MYCLASS();
parent::__construct();
}

Expand All @@ -29,7 +29,7 @@ class MyClass

}

class MyClass extends \MyNamespace\SomeClass
class MyOtherClass extends \MyNamespace\SomeClass
{
function __construct() {
something::MyNamespace();
Expand Down Expand Up @@ -64,3 +64,29 @@ foo(new class extends MyClass
}

});

class OlderClass
{
function OlderClass() {}

function __CONSTRUCT() {}
}


// Issue #2178.
class Nested extends Another {
public function getAnonymousClass() {
return new class() extends Something {
public function nested() {
echo 'In method nested!';
parent::Another(); // OK.
}

public function __construct() {
parent::Another(); // OK.
}
};
}

abstract public function nested();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getErrorList()
11 => 1,
47 => 1,
62 => 1,
91 => 1,
];

}//end getErrorList()
Expand Down