Skip to content

Commit d3e7e5a

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

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Common;
10-
use PHP_CodeSniffer\Util\Tokens;
1110
use SymfonyCustom\Helpers\SniffHelper;
1211

1312
/**
1413
* Throws errors if scalar type name are not valid.
1514
*/
1615
class ValidScalarTypeNameSniff implements Sniff
1716
{
17+
private const TYPING = '\\\\a-z0-9';
18+
private const OPENER = '\<\[\{\(';
19+
private const CLOSER = '\>\]\}\)';
20+
private const MIDDLE = '\,\:\&\|';
21+
1822
/**
1923
* @return array
2024
*/
@@ -33,7 +37,11 @@ public function process(File $phpcsFile, $stackPtr): void
3337

3438
if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
3539
preg_match(
36-
'`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\<\,\>\]]+))*)( .*)?`i',
40+
'`^((?:'
41+
.'['.self::OPENER.self::MIDDLE.']\s*'
42+
.'|(?:['.self::TYPING.self::CLOSER.']\s+)(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])'
43+
.'|['.self::TYPING.self::CLOSER.']'
44+
.')+)(.*)?`i',
3745
$tokens[($stackPtr + 2)]['content'],
3846
$match
3947
);
@@ -44,17 +52,7 @@ public function process(File $phpcsFile, $stackPtr): void
4452

4553
// Check type (can be multiple, separated by '|').
4654
$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);
55+
$suggestedType = $this->getValidTypeName($type);
5856
if ($type !== $suggestedType) {
5957
$fix = $phpcsFile->addFixableError(
6058
'For type-hinting in PHPDocs, use %s instead of %s',
@@ -82,7 +80,13 @@ public function process(File $phpcsFile, $stackPtr): void
8280
*/
8381
private function getValidTypeName(string $typeName): string
8482
{
85-
$parts = preg_split('/([\<\,\>])/', $typeName, -1, PREG_SPLIT_DELIM_CAPTURE);
83+
$typeNameWithoutSpace = str_replace(' ', '', $typeName);
84+
$parts = preg_split(
85+
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])/',
86+
$typeNameWithoutSpace,
87+
-1,
88+
PREG_SPLIT_DELIM_CAPTURE
89+
);
8690
$partsNumber = count($parts) - 1;
8791

8892
$validType = '';
@@ -104,10 +108,6 @@ private function getValidTypeName(string $typeName): string
104108
*/
105109
private function suggestType(string $typeName): string
106110
{
107-
if ('[]' === substr($typeName, -2)) {
108-
return $this->suggestType(substr($typeName, 0, -2)).'[]';
109-
}
110-
111111
$lowerType = strtolower($typeName);
112112
switch ($lowerType) {
113113
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)