Skip to content

Commit 847fe40

Browse files
authored
Merge pull request #272 from PHPCSStandards/universal/constructordestructorreturn-allow-for-namespaced-classes
CodeAnalysis/ConstructorDestructorReturn: correctly ignore PHP-4-style constructors in namespaced classes
2 parents f8b1620 + 1f7fb8a commit 847fe40

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Universal/Sniffs/CodeAnalysis/ConstructorDestructorReturnSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPCSUtils\Tokens\Collections;
1919
use PHPCSUtils\Utils\FunctionDeclarations;
2020
use PHPCSUtils\Utils\GetTokensAsString;
21+
use PHPCSUtils\Utils\Namespaces;
2122
use PHPCSUtils\Utils\NamingConventions;
2223
use PHPCSUtils\Utils\ObjectDeclarations;
2324
use PHPCSUtils\Utils\Scopes;
@@ -105,6 +106,17 @@ public function process(File $phpcsFile, $stackPtr)
105106
return;
106107
}
107108

109+
if (Namespaces::determineNamespace($phpcsFile, $stackPtr) !== '') {
110+
/*
111+
* Namespaced methods with the same name as the class are treated as
112+
* regular methods, so we can bow out if we're in a namespace.
113+
*
114+
* Note: the exception to this is PHP 5.3.0-5.3.2. This is currently
115+
* not dealt with.
116+
*/
117+
return;
118+
}
119+
108120
$functionType = 'A PHP 4-style constructor';
109121
}
110122

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/*
4+
* The PHP4-style constructors are not constructors in namespaced files.
5+
* No errors should be thrown for it, nor any auto-fixes made.
6+
*/
7+
namespace Some\Name;
8+
9+
class ReturnsAValue {
10+
function returnsAValue(): string
11+
{
12+
return 'php4style';
13+
}
14+
}

0 commit comments

Comments
 (0)