Skip to content

Commit

Permalink
Merge pull request #272 from PHPCSStandards/universal/constructordest…
Browse files Browse the repository at this point in the history
…ructorreturn-allow-for-namespaced-classes

CodeAnalysis/ConstructorDestructorReturn: correctly ignore PHP-4-style constructors in namespaced classes
  • Loading branch information
jrfnl authored Sep 17, 2023
2 parents f8b1620 + 1f7fb8a commit 847fe40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\GetTokensAsString;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\NamingConventions;
use PHPCSUtils\Utils\ObjectDeclarations;
use PHPCSUtils\Utils\Scopes;
Expand Down Expand Up @@ -105,6 +106,17 @@ public function process(File $phpcsFile, $stackPtr)
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;
}

$functionType = 'A PHP 4-style constructor';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* The PHP4-style constructors are not constructors in namespaced files.
* No errors should be thrown for it, nor any auto-fixes made.
*/
namespace Some\Name;

class ReturnsAValue {
function returnsAValue(): string
{
return 'php4style';
}
}

0 comments on commit 847fe40

Please sign in to comment.