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

Fix abstract/interface methods in PEAR.Functions.FunctionDeclaration #2148

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 22 additions & 1 deletion src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function process(File $phpcsFile, $stackPtr)
// Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
// by checking for the scope_opener.
if ($tokens[$stackPtr]['code'] === T_FUNCTION
&& isset($tokens[$stackPtr]['scope_opener']) === true
&& (isset($tokens[$stackPtr]['scope_opener']) === true || $tokens[$stackPtr]['level'] === 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that checking the token level is the best way to do this. It wouldn't work, for example, if you had the class wrapped in a condition (like an autoloader maybe) and while I can't think of why you'd define an abstract method in an anon class, it wouldn't catch that either. It's probably best to call File::getMethodProperties() and check the isAbstract array index to be sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a failing test in eeb758a, will have another look at the implementation.

) {
if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
Expand All @@ -123,6 +123,27 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->replaceToken(($openBracket - 1), '');
}
}

// Must be no space before semicolon in abstract/interface methods.
$end = $phpcsFile->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $closeBracket);
if ($tokens[$end]['code'] === T_SEMICOLON) {
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
$spaces = strlen($tokens[($end - 1)]['content']);
} else {
$spaces = 0;
}

if ($spaces !== 0) {
$error = 'Expected 0 spaces before semicolon; %s found';
$data = [$spaces];
$fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($end - 1), '');
}
}
}
}//end if

// Must be one space before and after USE keyword for closures.
Expand Down
38 changes: 38 additions & 0 deletions src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,41 @@ function foo(
}

$a = function () {

function foo ()
{}

class Foo {
function bar ()
{
}

abstract function baz () ;

abstract function qux () : void ;
}

interface Foo {
function bar () ;

function baz (
$longArgument,
$longerArgument,
$muchLongerArgument
) ;

function qux (
$longArgument,
$longerArgument,
$muchLongerArgument
) : void ;
}

trait Foo {
function bar ()
{
}

abstract function baz ()
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,40 @@ function foo(
}

$a = function () {

function foo()
{}

class Foo {
function bar()
{
}

abstract function baz();

abstract function qux() : void;
}

interface Foo {
function bar();

function baz(
$longArgument,
$longerArgument,
$muchLongerArgument
);

function qux(
$longArgument,
$longerArgument,
$muchLongerArgument
) : void;
}

trait Foo {
function bar()
{
}

abstract function baz();
}
12 changes: 12 additions & 0 deletions src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc')
223 => 1,
230 => 1,
237 => 1,
243 => 1,
247 => 1,
251 => 2,
253 => 2,
257 => 2,
259 => 1,
263 => 1,
265 => 1,
269 => 1,
273 => 1,
277 => 1,
278 => 1,
];
} else {
$errors = [
Expand Down