Skip to content

Commit

Permalink
Merge branch 'master' into php-8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra authored Oct 15, 2024
2 parents b6238c8 + 0c2af2f commit 5c81cbe
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 79 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": "^7.3 || ^8.0",
"squizlabs/php_codesniffer": "^3.7.2"
"squizlabs/php_codesniffer": "^3.10.3"
},
"require-dev": {
"phpunit/phpunit": "^9.6.15"
Expand Down
58 changes: 43 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
use const T_NULL;
use const T_NULLABLE;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_PARENTHESIS;
use const T_PARENT;
use const T_SELF;
use const T_SEMICOLON;
use const T_STRING;
use const T_TRUE;
use const T_TYPE_CLOSE_PARENTHESIS;
use const T_TYPE_INTERSECTION;
use const T_TYPE_OPEN_PARENTHESIS;
use const T_TYPE_UNION;
use const T_USE;
use const T_WHITESPACE;
Expand Down Expand Up @@ -116,8 +117,8 @@ public function process(File $phpcsFile, $stackPtr)
T_TYPE_UNION,
T_PARENT,
T_WHITESPACE,
T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS,
T_TYPE_OPEN_PARENTHESIS,
T_TYPE_CLOSE_PARENTHESIS,
],
$parenthesisCloser + 1,
$eol,
Expand Down Expand Up @@ -150,8 +151,8 @@ public function process(File $phpcsFile, $stackPtr)
}

$notTypes = [
T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS,
T_TYPE_OPEN_PARENTHESIS,
T_TYPE_CLOSE_PARENTHESIS,
T_BITWISE_AND,
T_BITWISE_OR,
T_TYPE_INTERSECTION,
Expand Down
72 changes: 25 additions & 47 deletions src/WebimpressCodingStandard/Sniffs/PHP/DisallowFqnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,33 @@
use const GLOB_NOSORT;
use const T_BITWISE_AND;
use const T_BITWISE_OR;
use const T_CASE;
use const T_CATCH;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSURE;
use const T_COLON;
use const T_COMMA;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
use const T_DOC_COMMENT_WHITESPACE;
use const T_DOUBLE_COLON;
use const T_ECHO;
use const T_ELLIPSIS;
use const T_EXTENDS;
use const T_FN;
use const T_FUNCTION;
use const T_IMPLEMENTS;
use const T_INCLUDE;
use const T_INCLUDE_ONCE;
use const T_INSTANCEOF;
use const T_INSTEADOF;
use const T_LOGICAL_AND;
use const T_LOGICAL_OR;
use const T_LOGICAL_XOR;
use const T_NAMESPACE;
use const T_NEW;
use const T_NS_SEPARATOR;
use const T_NULLABLE;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_PARENTHESIS;
use const T_PRINT;
use const T_REQUIRE;
use const T_REQUIRE_ONCE;
use const T_RETURN;
use const T_SEMICOLON;
use const T_STRING;
use const T_THROW;
use const T_TYPE_CLOSE_PARENTHESIS;
use const T_TYPE_INTERSECTION;
use const T_TYPE_OPEN_PARENTHESIS;
use const T_TYPE_UNION;
use const T_USE;
use const T_VARIABLE;

Expand Down Expand Up @@ -332,10 +325,23 @@ private function processString(
];

if (in_array($tokens[$prev]['code'], $prevClassTokens, true)
|| in_array($tokens[$next]['code'], [T_VARIABLE, T_ELLIPSIS, T_DOUBLE_COLON], true)
|| in_array($tokens[$next]['code'], [
T_VARIABLE,
T_ELLIPSIS,
T_DOUBLE_COLON,
// DNF
T_TYPE_UNION,
T_TYPE_INTERSECTION,
T_TYPE_CLOSE_PARENTHESIS,
T_OPEN_CURLY_BRACKET,
], true)
) {
$type = 'class';
} elseif ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
} elseif ($tokens[$next]['code'] === T_OPEN_PARENTHESIS
// The below condition is temporary due to upstream issue
// @see https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/630
|| $tokens[$next]['code'] === T_TYPE_OPEN_PARENTHESIS
) {
$type = 'function';
} else {
$type = 'const';
Expand All @@ -362,15 +368,10 @@ private function processString(
) {
$type = 'class';
}
} elseif ($tokens[$prev]['code'] === T_COMMA) {
$before = $phpcsFile->findPrevious(
Tokens::$emptyTokens + [T_STRING => T_STRING, T_NS_SEPARATOR => T_NS_SEPARATOR],
$prev - 1,
null,
true
);

if ($tokens[$before]['code'] === T_IMPLEMENTS) {
} elseif ($tokens[$next]['code'] === T_SEMICOLON) {
$before = $phpcsFile->findPrevious(Tokens::$emptyTokens, $prev, null, true);

if (in_array($tokens[$before]['code'], [T_TYPE_UNION, T_TYPE_INTERSECTION], true)) {
$type = 'class';
}
}
Expand Down Expand Up @@ -553,29 +554,6 @@ private function fixError(File $phpcsFile, int $stackPtr, string $expected) : vo
return;
}

if (in_array($tokens[$stackPtr - 1]['code'], [
T_NEW,
T_USE,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_CASE,
T_PRINT,
T_ECHO,
T_REQUIRE,
T_REQUIRE_ONCE,
T_INCLUDE,
T_INCLUDE_ONCE,
T_RETURN,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_THROW,
], true)) {
$expected = ' ' . $expected;
}

$phpcsFile->fixer->replaceToken($stackPtr, $expected);
$i = $stackPtr;
while (isset($tokens[++$i])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function in_array;

use const T_AS;
use const T_DECLARE;
use const T_FN_ARROW;
use const T_INSTANCEOF;
use const T_INSTEADOF;
Expand Down Expand Up @@ -40,16 +41,30 @@ public function register() : array
$tokens[] = T_INSTEADOF;
$tokens[] = T_FN_ARROW;

// Also register the contexts we want to specifically skip over.
$tokens[] = T_DECLARE;

return $tokens;
}

/**
* @param int $stackPtr
* @return null|int
*/
public function process(File $phpcsFile, $stackPtr) : void
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Skip over declare statements as those should be handled by different sniffs.
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
// Parse error / live coding.
return $phpcsFile->numTokens;
}

return $tokens[$stackPtr]['parenthesis_closer'];
}

$originalValue = $this->ignoreNewlines;
if (in_array($tokens[$stackPtr]['code'], $this->doNotIgnoreNewLineForTokens, true)) {
$this->ignoreNewlines = false;
Expand Down
13 changes: 13 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,17 @@ class TheClass extends \ MyNamespace \ Hello \ ParentClass implements \ArrayAcce
new \MyNamespace\Foo1\Bar1\Bar2();
new \Foo\BarBaz\Bar3();
}

public function method3(): \DNF\Type1|(\DNF\Type2&\DNF\Type3)
{
}

public function method4(): (\DNF\Type4|\DNF\Type5)&\DNF\Type6
{
}

abstract public function method5(): (\DNF\Type7|\DNF\Type8)&\DNF\Type9;

private \DNF\Type10&(\DNF\Type11|\DNF\Type12) $prop1;
private (\DNF\Type13&\DNF\Type14)|\DNF\Type15 $prop2;
}
30 changes: 29 additions & 1 deletion test/Sniffs/PHP/DisallowFqnUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ArrayAccess;
use Countable;
use DateTime;
use InClosure\Param;
use const InClosure\ReturnType;
use InClosure\ReturnType;
use const InClosure\CONST_IN_CLOSURE;
use function InClosure\functionInClosure;
use RuntimeException;
Expand All @@ -29,6 +29,21 @@ use B;
use InFnClosure\Param1;
use InFnClosure\ReturnType1;
use MyNamespace\Foo1\Bar1;
use DNF\Type1;
use DNF\Type2;
use DNF\Type3;
use DNF\Type4;
use DNF\Type5;
use DNF\Type6;
use DNF\Type7;
use DNF\Type8;
use DNF\Type9;
use DNF\Type10;
use DNF\Type11;
use DNF\Type12;
use DNF\Type13;
use DNF\Type14;
use DNF\Type15;

use \ArrayObject as AO;
use Foo\BarBaz;
Expand Down Expand Up @@ -207,4 +222,17 @@ class TheClass extends ParentClass implements ArrayAccess, Countable
new Bar1\Bar2();
new BarBaz\Bar3();
}

public function method3(): Type1|(Type2&Type3)
{
}

public function method4(): (Type4|Type5)&Type6
{
}

abstract public function method5(): (Type7|Type8)&Type9;

private Type10&(Type11|Type12) $prop1;
private (Type13&Type14)|Type15 $prop2;
}
5 changes: 5 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ protected function getErrorList(string $testFile = '') : array
178 => 1,
179 => 1,
180 => 1,
183 => 3,
187 => 3,
191 => 3,
193 => 3,
194 => 3,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Safeguard to ensure that sniff handles parse error/live coding correctly.
declare(strict_types=
Loading

0 comments on commit 5c81cbe

Please sign in to comment.