Skip to content

Commit

Permalink
feat: Support for DNF types in PHP\DisallowFqnSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Oct 15, 2024
1 parent 55366d0 commit c11a31d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/WebimpressCodingStandard/Sniffs/PHP/DisallowFqnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@
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 +338,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 Down Expand Up @@ -373,6 +392,12 @@ private function processString(
if ($tokens[$before]['code'] === T_IMPLEMENTS) {
$type = 'class';
}
} 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
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

0 comments on commit c11a31d

Please sign in to comment.