Skip to content

Commit d93a15f

Browse files
✨ More case for valid scalar
1 parent 04e5251 commit d93a15f

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
class ValidScalarTypeNameSniff implements Sniff
1717
{
18+
private const OPENER = '\<\[\{\(';
19+
private const CLOSER = '\>\]\}\)';
20+
private const MIDDLE = '\,\:';
21+
1822
/**
1923
* @return array
2024
*/
@@ -32,29 +36,26 @@ public function process(File $phpcsFile, $stackPtr): void
3236
$tokens = $phpcsFile->getTokens();
3337

3438
if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
39+
$openerWithSpace = '['.self::OPENER.']\s*';
40+
$closerWithSpace =
41+
'(?:['.self::CLOSER.']\s+)(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])'
42+
.'|['.self::CLOSER.']';
43+
$middleWithSpace = '\s*['.self::MIDDLE.']\s*';
44+
3545
preg_match(
36-
'`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\<\,\>\]]+))*)( .*)?`i',
46+
"`^((?:\|?(?:(?:[\\\\a-z0-9]|${openerWithSpace}|${middleWithSpace}|${closerWithSpace})+))*)(.*)?`i",
3747
$tokens[($stackPtr + 2)]['content'],
3848
$match
3949
);
4050

51+
var_dump($match);
4152
if (isset($match[1]) === false) {
4253
return;
4354
}
4455

4556
// Check type (can be multiple, separated by '|').
4657
$type = $match[1];
47-
$typeNames = explode('|', $type);
48-
$suggestedNames = [];
49-
foreach ($typeNames as $i => $typeName) {
50-
$suggestedName = $this->getValidTypeName($typeName);
51-
52-
if (in_array($suggestedName, $suggestedNames, true) === false) {
53-
$suggestedNames[] = $suggestedName;
54-
}
55-
}
56-
57-
$suggestedType = implode('|', $suggestedNames);
58+
$suggestedType = $this->getValidTypeName($type);
5859
if ($type !== $suggestedType) {
5960
$fix = $phpcsFile->addFixableError(
6061
'For type-hinting in PHPDocs, use %s instead of %s',
@@ -82,7 +83,13 @@ public function process(File $phpcsFile, $stackPtr): void
8283
*/
8384
private function getValidTypeName(string $typeName): string
8485
{
85-
$parts = preg_split('/([\<\,\>])/', $typeName, -1, PREG_SPLIT_DELIM_CAPTURE);
86+
$typeNameWithoutSpace = str_replace(' ', '', $typeName);
87+
$parts = preg_split(
88+
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])/',
89+
$typeNameWithoutSpace,
90+
-1,
91+
PREG_SPLIT_DELIM_CAPTURE
92+
);
8693
$partsNumber = count($parts) - 1;
8794

8895
$validType = '';
@@ -104,10 +111,6 @@ private function getValidTypeName(string $typeName): string
104111
*/
105112
private function suggestType(string $typeName): string
106113
{
107-
if ('[]' === substr($typeName, -2)) {
108-
return $this->suggestType(substr($typeName, 0, -2)).'[]';
109-
}
110-
111114
$lowerType = strtolower($typeName);
112115
switch ($lowerType) {
113116
case 'bool':

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ echo ( float ) $a;
4343
/** @var real $c */
4444

4545
/** @method integer|string */
46-
/** @method array<integer,boolean>|integer[]|string */
46+
/** @method array<integer,boolean>|integer[]|array<integer> truc */
47+
/** @method array<integer, boolean>|integer[] |string|(integer|boolean)[] truc */

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ echo ( float ) $a;
4343
/** @var float $c */
4444

4545
/** @method int|string */
46-
/** @method array<int,bool>|int[]|string */
46+
/** @method array<int,bool>|int[]|array<int> truc */
47+
/** @method array<int,bool>|int[]|string|(int|bool)[] truc */

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getErrorList(): array
3636
43 => 1,
3737
45 => 1,
3838
46 => 1,
39+
47 => 1,
3940
];
4041
}
4142

0 commit comments

Comments
 (0)