Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
70c864e
PHPCS 4.x / PHP 8.0: namespaced names as single token
jrfnl Jul 7, 2020
c7e78b7
Tokenizer/PHP: update the arrow function backfill to allow for PHP 8 …
jrfnl Sep 1, 2020
4837c2e
Tokenizer/PHP: update the short array tokenization to allow for PHP 8…
jrfnl Nov 2, 2020
7dd283a
Tokens::$functionNameTokens: add the new identifier name tokens
jrfnl Sep 2, 2020
2641863
File::getMethodParameters(): fix method to work with the PHP 8 identi…
jrfnl Aug 31, 2020
b1cbaa8
File::getMethodProperties(): fix method to work with the PHP 8 identi…
jrfnl Aug 31, 2020
b4eecf3
File::getMemberProperties(): fix method to work with the PHP 8 identi…
jrfnl Aug 31, 2020
f646f7b
File::isReference(): fix method to work with the PHP 8 identifier tokens
jrfnl Aug 31, 2020
57976fc
File::findEndOfStatement(): update expectations in test
jrfnl Aug 31, 2020
edcee6e
File::findExtendedClassName(): fix method to work with the PHP 8 iden…
jrfnl Aug 31, 2020
fa428cf
File::findImplementedInterfaceNames(): fix method to work with the PH…
jrfnl Aug 31, 2020
dad94a5
Generic/DuplicateClassName: fix sniff to work with the PHP 8 identifi…
jrfnl Aug 31, 2020
ea8047b
Generic/ForbiddenFunctions: fix sniff to work with the PHP 8 identifi…
jrfnl Aug 31, 2020
d354f44
Generic/LanguageConstructSpacing: fix sniff to work with the PHP 8 id…
jrfnl Aug 31, 2020
0d65b8b
PSR2/ClassDeclaration: fix sniff to work with the PHP 8 identifier to…
jrfnl Sep 1, 2020
acc821d
PSR12/ClassInstantiation: fix sniff to work with the PHP 8 identifier…
jrfnl Aug 31, 2020
b0b3557
PSR12/ImportStatement: fix sniff to work with the PHP 8 identifier to…
jrfnl Aug 31, 2020
e5c97a0
PSR12/NullableTypeDeclaration: fix sniff to work with the PHP 8 ident…
jrfnl Aug 31, 2020
43d1199
PSR12/CompoundNamespaceDepth: fix sniff to work with the PHP 8 identi…
jrfnl Aug 31, 2020
2fae85e
Squiz/LowercasePHPFunctions: fix sniff to work with the PHP 8 identif…
jrfnl Aug 31, 2020
18508ae
Squiz/SelfMemberReference: fix sniff to work with the PHP 8 identifie…
jrfnl Sep 1, 2020
d8e99c2
Squiz/FunctionCommentThrowTag: fix sniff to work with the PHP 8 ident…
jrfnl Sep 1, 2020
2458072
Squiz/VariableComment: fix sniff to work with the PHP 8 identifier to…
jrfnl Sep 1, 2020
bf94383
Squiz/OperatorBracket: fix sniff to work with the PHP 8 identifier to…
jrfnl Sep 1, 2020
e58da25
Squiz/DisallowMultipleAssignments: fix sniff to work with the PHP 8 i…
jrfnl Sep 1, 2020
033649b
Generic/ArbitraryParenthesesSpacing: add tests with the PHP 8 identif…
jrfnl Sep 2, 2020
8c36795
PEAR/FunctionCallSignature: add tests with the PHP 8 identifier tokens
jrfnl Sep 2, 2020
679d364
Squiz/ComparisonOperatorUsageUnitTest: adjust tests to use the PHP 8 …
jrfnl Sep 2, 2020
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
60 changes: 34 additions & 26 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,10 @@ public function getMethodParameters($stackPtr)
}
break;
case T_STRING:
// This is a string, so it may be a type hint, but it could
case T_NAME_QUALIFIED:
case T_NAME_FULLY_QUALIFIED:
case T_NAME_RELATIVE:
// This is an identifier name, so it may be a type declaration, but it could
// also be a constant used as a default value.
$prevComma = false;
for ($t = $i; $t >= $opener; $t--) {
Expand Down Expand Up @@ -1395,8 +1398,6 @@ public function getMethodParameters($stackPtr)
$typeHintEndToken = $i;
}
break;
case T_NAMESPACE:
case T_NS_SEPARATOR:
case T_TYPE_UNION:
case T_FALSE:
case T_NULL:
Expand Down Expand Up @@ -1600,16 +1601,17 @@ public function getMethodProperties($stackPtr)
}

$valid = [
T_STRING => T_STRING,
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_STATIC => T_STATIC,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_TYPE_UNION => T_TYPE_UNION,
T_STRING => T_STRING,
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE => T_NAME_RELATIVE,
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_STATIC => T_STATIC,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_TYPE_UNION => T_TYPE_UNION,
];

for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) {
Expand Down Expand Up @@ -1790,15 +1792,16 @@ public function getMemberProperties($stackPtr)
if ($i < $stackPtr) {
// We've found a type.
$valid = [
T_STRING => T_STRING,
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_TYPE_UNION => T_TYPE_UNION,
T_STRING => T_STRING,
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE => T_NAME_RELATIVE,
T_CALLABLE => T_CALLABLE,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_TYPE_UNION => T_TYPE_UNION,
];

for ($i; $i < $stackPtr; $i++) {
Expand Down Expand Up @@ -1998,12 +2001,13 @@ public function isReference($stackPtr)
return true;
} else {
$skip = Tokens::$emptyTokens;
$skip[] = T_NS_SEPARATOR;
$skip[] = T_SELF;
$skip[] = T_PARENT;
$skip[] = T_STATIC;
$skip[] = T_STRING;
$skip[] = T_NAMESPACE;
$skip[] = T_NAME_QUALIFIED;
$skip[] = T_NAME_FULLY_QUALIFIED;
$skip[] = T_NAME_RELATIVE;
$skip[] = T_DOUBLE_COLON;

$nextSignificantAfter = $this->findNext(
Expand Down Expand Up @@ -2539,8 +2543,10 @@ public function findExtendedClassName($stackPtr)
}

$find = [
T_NS_SEPARATOR,
T_STRING,
T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE,
T_WHITESPACE,
];

Expand Down Expand Up @@ -2590,8 +2596,10 @@ public function findImplementedInterfaceNames($stackPtr)
}

$find = [
T_NS_SEPARATOR,
T_STRING,
T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE,
T_WHITESPACE,
T_COMMA,
];
Expand Down
11 changes: 6 additions & 5 deletions src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class DuplicateClassNameSniff implements Sniff
{
Expand Down Expand Up @@ -67,12 +68,12 @@ public function process(File $phpcsFile, $stackPtr)

// Keep track of what namespace we are in.
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
$find = Tokens::$emptyTokens;
$find[] = T_STRING;
$find[] = T_NAME_QUALIFIED;

$nsEnd = $phpcsFile->findNext(
[
T_NS_SEPARATOR,
T_STRING,
T_WHITESPACE,
],
$find,
($stackPtr + 1),
null,
true
Expand Down
34 changes: 18 additions & 16 deletions src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class ForbiddenFunctionsSniff implements Sniff
{
Expand Down Expand Up @@ -69,7 +70,10 @@ public function register()
$this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
}

return [T_STRING];
return [
T_STRING,
T_NAME_FULLY_QUALIFIED,
];
}

// If we are not pattern matching, we need to work out what
Expand Down Expand Up @@ -101,7 +105,10 @@ public function register()
$this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
$this->forbiddenFunctions = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);

return array_unique($register);
$targets = array_unique($register);
$targets[] = T_NAME_FULLY_QUALIFIED;

return $targets;

}//end register()

Expand Down Expand Up @@ -137,35 +144,30 @@ public function process(File $phpcsFile, $stackPtr)

$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);

// If function call is directly preceded by a NS_SEPARATOR it points to the
// global namespace, so we should still catch it.
if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
if ($tokens[$prevToken]['code'] === T_STRING) {
// Not in the global namespace.
return;
}
}

if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
// Not a call to a PHP function.
return;
}

$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
// Not a call to a PHP function.
return;
}

if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
if (($tokens[$stackPtr]['code'] === T_STRING || $tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED)
&& $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS
) {
// Not a call to a PHP function.
return;
}

$function = strtolower($tokens[$stackPtr]['content']);
$pattern = null;
if ($tokens[$stackPtr]['code'] === T_NAME_FULLY_QUALIFIED) {
$function = ltrim($function, '\\');
}

$pattern = null;
if ($this->patternMatch === true) {
$count = 0;
$pattern = preg_replace(
Expand All @@ -188,7 +190,7 @@ public function process(File $phpcsFile, $stackPtr)
}
}//end if

$this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
$this->addError($phpcsFile, $stackPtr, $function, $pattern);

}//end process()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public function process(File $phpcsFile, $stackPtr)
$content = $tokens[$stackPtr]['content'];
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_NS_SEPARATOR) {
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_NAME_FULLY_QUALIFIED) {
// Namespace keyword used as operator, not as the language construct.
// In PHP 8 this use with whitespace/comments between the parts is a parse error.
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,7 @@ $a = (
if (true) {} ( 1+2) === 3 ? $a = 1 : $a = 2;
class A {} ( 1+2) === 3 ? $a = 1 : $a = 2;
function foo() {} ( 1+2) === 3 ? $a = 1 : $a = 2;

$b = \functioncall( $something ) ;
$b = Package\functioncall( $something ) ;
$b = namespace\functioncall( $something ) ;
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ $a = (
if (true) {} (1+2) === 3 ? $a = 1 : $a = 2;
class A {} (1+2) === 3 ? $a = 1 : $a = 2;
function foo() {} (1+2) === 3 ? $a = 1 : $a = 2;

$b = \functioncall( $something ) ;
$b = Package\functioncall( $something ) ;
$b = namespace\functioncall( $something ) ;
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,16 @@ return trim(preg_replace_callback(

$a = ['a' => function ($b) { return $b; }];
$a['a']( 1 );

$val = \functionCall ( $arg );
$val = Package\functionCall( $arg );
$val = namespace\functionCall ($arg);

$val = \functionCall ( $arg
, $arg2 );
$val = Package\functionCall(
$arg, $arg2);
$val = namespace\functionCall(
$arg,
$arg2
);
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,19 @@ return trim(

$a = ['a' => function ($b) { return $b; }];
$a['a'](1);

$val = \functionCall($arg);
$val = Package\functionCall($arg);
$val = namespace\functionCall($arg);

$val = \functionCall(
$arg
, $arg2
);
$val = Package\functionCall(
$arg, $arg2
);
$val = namespace\functionCall(
$arg,
$arg2
);
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public function getErrorList()
523 => 1,
524 => 3,
527 => 2,
529 => 3,
530 => 2,
531 => 1,
533 => 2,
534 => 1,
536 => 2,
];

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function process(File $phpcsFile, $stackPtr)
// Find the class name.
$allowed = [
T_STRING => T_STRING,
T_NS_SEPARATOR => T_NS_SEPARATOR,
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
T_NAME_RELATIVE => T_NAME_RELATIVE,
T_SELF => T_SELF,
T_STATIC => T_STATIC,
T_VARIABLE => T_VARIABLE,
Expand Down
4 changes: 2 additions & 2 deletions src/Standards/PSR12/Sniffs/Files/ImportStatementSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public function process(File $phpcsFile, $stackPtr)
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
}

if ($tokens[$next]['code'] !== T_NS_SEPARATOR) {
if ($tokens[$next]['code'] !== T_NAME_FULLY_QUALIFIED) {
return;
}

$error = 'Import statements must not begin with a leading backslash';
$fix = $phpcsFile->addFixableError($error, $next, 'LeadingSlash');

if ($fix === true) {
$phpcsFile->fixer->replaceToken($next, '');
$phpcsFile->fixer->replaceToken($next, ltrim($tokens[$next]['content'], '\\'));
}

}//end process()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class NullableTypeDeclarationSniff implements Sniff
* @var array
*/
private $validTokens = [
T_STRING => true,
T_NS_SEPARATOR => true,
T_CALLABLE => true,
T_SELF => true,
T_PARENT => true,
T_STATIC => true,
T_STRING => true,
T_NAME_QUALIFIED => true,
T_NAME_FULLY_QUALIFIED => true,
T_NAME_RELATIVE => true,
T_CALLABLE => true,
T_SELF => true,
T_PARENT => true,
T_STATIC => true,
];


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

if ($tokens[$i]['code'] === T_NAME_FULLY_QUALIFIED || $tokens[$i]['code'] === T_NAME_QUALIFIED) {
$depth += substr_count($tokens[$i]['content'], '\\');
continue;
}

if ($i === $end || $tokens[$i]['code'] === T_COMMA) {
// End of a namespace.
if ($depth > $this->maxDepth) {
Expand All @@ -72,7 +77,7 @@ public function process(File $phpcsFile, $stackPtr)

$depth = 1;
}
}
}//end for

}//end process()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Vendor\Package\SomeNamespace\{
ClassZ,
};

// Parse error in PHP 8.0+, but will be detected correctly cross-version anyway.
use Vendor\Package\SomeNamespace\{
SubnamespaceOne /* comment */
\AnotherNamespace // comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function getErrorList()
{
return [
10 => 1,
18 => 1,
21 => 1,
19 => 1,
22 => 1,
];

}//end getErrorList()
Expand Down
Loading