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

PEAR/Functions/FunctionDeclaration: add extra defensive coding #420

Merged
merged 3 commits into from
Apr 3, 2024
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
35 changes: 19 additions & 16 deletions src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ public function process(File $phpcsFile, $stackPtr)
// and the opening parenthesis.
// Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
// by checking for the scope_opener.
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if ($tokens[$stackPtr]['code'] === T_FUNCTION
&& (isset($tokens[$stackPtr]['scope_opener']) === true || $phpcsFile->getMethodProperties($stackPtr)['has_body'] === false)
&& (isset($tokens[$stackPtr]['scope_opener']) === true || $methodProps['has_body'] === false)
) {
if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
Expand All @@ -125,25 +126,27 @@ public function process(File $phpcsFile, $stackPtr)
}

// Must be no space before semicolon in abstract/interface methods.
if ($phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) {
if ($methodProps['has_body'] === false) {
$end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket);
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
$spaces = $tokens[($end - 1)]['length'];
} else {
$spaces = 0;
}
if ($end !== false) {
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
$spaces = 'newline';
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
$spaces = $tokens[($end - 1)]['length'];
} 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), '');
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
}//end if

// Must be one space before and after USE keyword for closures.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error/live coding test.
// This must be the only test in this file.
// Safeguarding that the sniff does not throw a PHP notice for this test.

function liveCoding()
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ final class FunctionDeclarationUnitTest extends AbstractSniffUnitTest
*/
public function getErrorList($testFile='')
{
if ($testFile === 'FunctionDeclarationUnitTest.inc') {
$errors = [
switch ($testFile) {
case 'FunctionDeclarationUnitTest.1.inc':
return [
3 => 1,
4 => 1,
5 => 1,
Expand Down Expand Up @@ -108,8 +109,9 @@ public function getErrorList($testFile='')
483 => 1,
490 => 2,
];
} else {
$errors = [

case 'FunctionDeclarationUnitTest.js':
return [
3 => 1,
4 => 1,
5 => 1,
Expand All @@ -121,9 +123,10 @@ public function getErrorList($testFile='')
41 => 1,
48 => 1,
];
}//end if

return $errors;
default:
return [];
}//end switch

}//end getErrorList()

Expand Down