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

Improve DeprecatedPHP4StyleConstructors sniff (code review). #167

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 29 additions & 11 deletions Sniffs/PHP/DeprecatedPHP4StyleConstructorsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ public function register()

}//end register()

public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsAbove('7.0') === false) {
return;
}

if ($this->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;
}

$tokens = $phpcsFile->getTokens();

$class = $tokens[$stackPtr];
Expand All @@ -34,27 +50,29 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
}

$scopeCloser = $class['scope_closer'];
$className = $phpcsFile->getDeclarationName($stackPtr);

//get the name of the class
$classNamePos = $phpcsFile->findNext(T_STRING, $stackPtr);
$className = $tokens[$classNamePos]['content'];
if (empty($className) || is_string($className) === false) {
return;
}

$nextFunc = $stackPtr;
$newConstructorFound = false;
$oldConstructorFound = false;
$oldConstructorPos = false;
while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
$funcNamePos = $phpcsFile->findNext(T_STRING, $nextFunc);
$funcName = $phpcsFile->getDeclarationName($nextFunc);
if (empty($funcName) || is_string($funcName) === false) {
continue;
}

if ($tokens[$funcNamePos]['content'] === '__construct') {
if ($funcName === '__construct') {
$newConstructorFound = true;
}

if ($this->supportsAbove('7.0')) {
if ($funcNamePos !== false && $tokens[$funcNamePos]['content'] === $className) {
$oldConstructorFound = true;
$oldConstructorPos = $funcNamePos;
}
if ($funcName === $className) {
$oldConstructorFound = true;
$oldConstructorPos = $phpcsFile->findNext(T_STRING, $nextFunc);
}
}

Expand Down
18 changes: 17 additions & 1 deletion Tests/Sniffs/PHP/DeprecatedPHP4StyleConstructorsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
class DeprecatedPHP4StyleConstructorsSniffTest extends BaseSniffTest
{
/**
* Test PHP4 style constructors
* Test PHP4 style constructors.
*
* @group deprecatedPHP4Constructors
*
* @return void
*/
Expand All @@ -27,4 +29,18 @@ public function testIsDeprecated()
$file = $this->sniffFile('sniff-examples/deprecated_php4style_constructors.php', '7.0');
$this->assertError($file, 3, 'Deprecated PHP4 style constructor are not supported since PHP7');
}

/**
* Test valid methods with the same name as the class.
*
* @group deprecatedPHP4Constructors
*
* @return void
*/
public function testValidMethods()
{
$file = $this->sniffFile('sniff-examples/deprecated_php4style_constructors.php', '7.0');
$this->assertNoViolation($file, 9);
$this->assertNoViolation($file, 20);
}
}
11 changes: 10 additions & 1 deletion Tests/sniff-examples/deprecated_php4style_constructors.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ function foo() {

class bar {
function bar() {
echo 'I am just the foo method';
echo 'I am just the bar method';
}
function __construct() {
echo 'I am the real constructor';
}
}

namespace foobar {

class foobar {
function foobar() {
echo 'I am just the bar method';
}
}
}