Skip to content

Commit d8dab6b

Browse files
authored
Merge pull request #54 from worksome/bugfix/regex-phpdoc
fix: update `PropertyDoc` to support unions
2 parents efd1338 + 066dd59 commit d8dab6b

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

src/Sniffs/Support/PropertyDoc.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use InvalidArgumentException;
99
use function Safe\preg_match;
1010

11-
final class PropertyDoc
11+
final readonly class PropertyDoc
1212
{
1313
public function __construct(
1414
private string $scope,
@@ -75,11 +75,11 @@ private static function regexForTypes(): string
7575
return <<<REGEXP
7676
/
7777
( # Capture group #1.
78-
[$\\\\\w-]+ # Match any word, including words with '$', '\', or '-' symbols.
78+
[$\\\\\w&|-]+ # Match any word, including words with '$', '\', '&', '|', or '-' symbols.
7979
( # Capture group #2.
80-
[\[{<] # Match any '<' or '{' symbols, which are used in PHPStan generics.
81-
(?:[^\[\]{}<>]+|(?2))*+ # Recursively ignore any matching sets of '<' and '>', '{' and '}' or '[' and ']' found in nested types. Eg: `Collection<int, array<string, string>>`.
82-
[\]}>] # Until we match the closing '>' or '}'.
80+
[\[{<] # Match any '<' or '{' symbols, which are used in PHPStan generics.
81+
(?:[^\[\]{}<>]+|(?2))*+ # Recursively ignore any matching sets of '<' and '>', '{' and '}' or '[' and ']' found in nested types. Eg: `Collection<int, array<string, string>>`.
82+
[\]}>] # Until we match the closing '>' or '}'.
8383
)? # End capture group #2. Not all types are generics, so capture group 2 is optional.
8484
) # End capture group #1.
8585
( # Capture group #3.

testResources/Sniffs/PhpDoc/PropertyDollarSignSniff/Fixed/InvalidProperties.php.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Worksome\WorksomeSniff\Tests\Resources\Sniffs\PhpDoc\PropertyDollarSig
1212
* @property string[] $whoops
1313
* @property positive-int $nice_int
1414
* @property non-empty-array<\Exception> $non_empty_array
15+
* @property ABC|EFG $union_type
16+
* @property ABC&EFG $intersection_type
1517
*/
1618
class InvalidProperties
1719
{

testResources/Sniffs/PhpDoc/PropertyDollarSignSniff/InvalidProperties.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @property string[] whoops
1313
* @property positive-int nice_int
1414
* @property non-empty-array<\Exception> non_empty_array
15+
* @property ABC|EFG union_type
16+
* @property ABC&EFG intersection_type
1517
*/
1618
class InvalidProperties
1719
{

tests/Sniffs/PhpDoc/PropertyDollarSignSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
})->with([
3030
'invalid properties' => [
3131
__DIR__ . '/../../../testResources/Sniffs/PhpDoc/PropertyDollarSignSniff/InvalidProperties.php',
32-
[6, 7, 8, 9, 10, 11, 12, 13, 14],
32+
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
3333
],
3434
]);
3535

0 commit comments

Comments
 (0)