Skip to content

Commit b178c7d

Browse files
✨ Handle space
1 parent 6f90b62 commit b178c7d

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
*/
1515
class ValidTypeHintSniff implements Sniff
1616
{
17-
private const TEXT = '[\\\\a-z0-9]';
18-
private const OPENER = '\<|\[|\{|\(';
19-
private const MIDDLE = '\,|\:|\=\>';
20-
private const CLOSER = '\>|\]|\}|\)';
21-
private const SEPARATOR = '\&|\|';
22-
23-
/*
17+
/**
2418
* <simple> is any non-array, non-generic, non-alternated type, eg `int` or `\Foo`
2519
* <array> is array of <simple>, eg `int[]` or `\Foo[]`
2620
* <generic> is generic collection type, like `array<string, int>`, `Collection<Item>` and more complex like `Collection<int, \null|SubCollection<string>>`
@@ -30,24 +24,24 @@ class ValidTypeHintSniff implements Sniff
3024
private const REGEX_TYPES = '
3125
(?<types>
3226
(?<type>
33-
(?<array>
34-
(?&simple)(\[\])*
35-
)
36-
|
37-
(?<simple>
38-
[@$?]?[\\\\\w]+
39-
)
40-
|
4127
(?<generic>
42-
(?<genericName>(?&simple))
43-
<
44-
(?:(?<genericKey>(?&types)),\s*)?(?<genericValue>(?&types)|(?&generic))
28+
(?<genericName>(?&simple))\s*
29+
<\s*
30+
(?:
31+
(?<genericKey>(?&types))\s*
32+
,\s*
33+
)?
34+
(?<genericValue>(?&types)|(?&generic))\s*
4535
>
4636
)
37+
|
38+
(?<array>(?&simple)(\s*\[\s*\])+)
39+
|
40+
(?<simple>[@$?]?[\\\\\w]+)
4741
)
4842
(?:
49-
\|
50-
(?:(?&simple)|(?&array)|(?&generic))
43+
\s*\|\s*
44+
(?:(?&generic)|(?&array)|(?&simple))
5145
)*
5246
)
5347
';
@@ -76,7 +70,7 @@ public function process(File $phpcsFile, $stackPtr): void
7670
);
7771

7872
$content = 1 === $matchingResult ? $matches['types'] : '';
79-
$endOfContent = preg_replace('/'.preg_quote($content, '/').'/', '', $tokens[$stackPtr + 2]['content'], 1);
73+
$endOfContent = substr($tokens[$stackPtr + 2]['content'], strlen($content));
8074

8175
$suggestedType = $this->getValidTypes($content);
8276

@@ -95,38 +89,19 @@ public function process(File $phpcsFile, $stackPtr): void
9589
}
9690
}
9791

98-
/**
99-
* @param string $content
100-
*
101-
* @return array
102-
*/
103-
private function getTypes(string $content): array
104-
{
105-
$types = [];
106-
while ('' !== $content && false !== $content) {
107-
preg_match('{^'.self::REGEX_TYPES.'$}x', $content, $matches);
108-
109-
$types[] = $matches['type'];
110-
$content = substr($content, strlen($matches['type']) + 1);
111-
}
112-
113-
return $types;
114-
}
115-
11692
/**
11793
* @param string $content
11894
*
11995
* @return string
12096
*/
12197
private function getValidTypes(string $content): string
12298
{
99+
$content = preg_replace('/\s/', '', $content);
123100
$types = $this->getTypes($content);
124-
125101
foreach ($types as $index => $type) {
126-
$type = str_replace(' ', '', $type);
127-
128102
preg_match('{^'.self::REGEX_TYPES.'$}x', $type, $matches);
129-
if (isset($matches['generic'])) {
103+
104+
if (isset($matches['generic']) && '' !== $matches['generic']) {
130105
$validType = $this->getValidType($matches['genericName']).'<';
131106

132107
if ('' !== $matches['genericKey']) {
@@ -157,6 +132,24 @@ private function getValidTypes(string $content): string
157132
return implode('|', $types);
158133
}
159134

135+
/**
136+
* @param string $content
137+
*
138+
* @return array
139+
*/
140+
private function getTypes(string $content): array
141+
{
142+
$types = [];
143+
while ('' !== $content && false !== $content) {
144+
preg_match('{^'.self::REGEX_TYPES.'$}x', $content, $matches);
145+
146+
$types[] = $matches['type'];
147+
$content = substr($content, strlen($matches['type']) + 1);
148+
}
149+
150+
return $types;
151+
}
152+
160153
/**
161154
* @param string $typeName
162155
*

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ echo ( float ) $a;
5151
* @return string[]|null|int[]
5252
* @return Foo<integer|null|string, boolean|null|string>|null|int[]
5353
*/
54+
55+
/** @method array < integer , null | boolean > | integer [ ] | array < array < integer > > truc */

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ echo ( float ) $a;
5151
* @return string[]|int[]|null
5252
* @return Foo<int|string|null, bool|string|null>|int[]|null
5353
*/
54+
55+
/** @method array<int, bool|null>|int[]|array<array<int>> truc */

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function getErrorList(): array
4040
50 => 1,
4141
51 => 1,
4242
52 => 1,
43+
55 => 1,
4344
];
4445
}
4546

0 commit comments

Comments
 (0)