Skip to content

Commit 5ae660e

Browse files
Merge pull request #97 from VincentLanglet/fixSpace
🐛 Avoid space bug
2 parents c247273 + 2730b56 commit 5ae660e

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php renamed to SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
13-
* Throws errors if scalar type name are not valid.
13+
* Throws errors if PHPDocs type hint are not valid.
1414
*/
15-
class ValidScalarTypeNameSniff implements Sniff
15+
class ValidTypeHintSniff implements Sniff
1616
{
17-
private const TYPING = '\\\\a-z0-9';
17+
private const TEXT = '\\\\a-z0-9';
1818
private const OPENER = '\<\[\{\(';
1919
private const CLOSER = '\>\]\}\)';
20-
private const MIDDLE = '\,\:\&\|';
20+
private const MIDDLE = '\,\:';
21+
private const SEPARATOR = '\&\|';
2122

2223
/**
2324
* @return array
@@ -38,9 +39,10 @@ public function process(File $phpcsFile, $stackPtr): void
3839
if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
3940
preg_match(
4041
'`^((?:'
41-
.'['.self::OPENER.self::MIDDLE.']\s*'
42-
.'|(?:['.self::TYPING.self::CLOSER.']\s+)(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])'
43-
.'|['.self::TYPING.self::CLOSER.']'
42+
.'['.self::OPENER.self::MIDDLE.self::SEPARATOR.']\s*'
43+
.'|(?:['.self::TEXT.self::CLOSER.']\s+)'
44+
.'(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.self::SEPARATOR.'])'
45+
.'|['.self::TEXT.self::CLOSER.']'
4446
.')+)(.*)?`i',
4547
$tokens[($stackPtr + 2)]['content'],
4648
$match
@@ -82,7 +84,7 @@ private function getValidTypeName(string $typeName): string
8284
{
8385
$typeNameWithoutSpace = str_replace(' ', '', $typeName);
8486
$parts = preg_split(
85-
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])/',
87+
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.self::SEPARATOR.'])/',
8688
$typeNameWithoutSpace,
8789
-1,
8890
PREG_SPLIT_DELIM_CAPTURE
@@ -93,7 +95,7 @@ private function getValidTypeName(string $typeName): string
9395
for ($i = 0; $i < $partsNumber; $i += 2) {
9496
$validType .= $this->suggestType($parts[$i]).$parts[$i + 1];
9597

96-
if (in_array($parts[$i + 1], [',', ':'])) {
98+
if (preg_match('/['.self::MIDDLE.']/', $parts[$i + 1])) {
9799
$validType .= ' ';
98100
}
99101
}
@@ -102,7 +104,7 @@ private function getValidTypeName(string $typeName): string
102104
$validType .= $this->suggestType($parts[$partsNumber]);
103105
}
104106

105-
return $validType;
107+
return trim($validType);
106108
}
107109

108110
/**

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc renamed to SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ echo ( float ) $a;
4646
/** @method array<integer,boolean>|integer[]|array<integer> truc */
4747
/** @method array<integer, boolean>| integer[] |string|(integer|boolean)[] truc */
4848
/** @method array{scheme:string,host:string} truc */
49+
50+
/**
51+
* Avoid weird bug
52+
*
53+
* @method array{
54+
* @method array{scheme:integer,host:
55+
* @method array<
56+
* @method array<integer,
57+
* @method array|
58+
*/

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc.fixed renamed to SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ echo ( float ) $a;
4646
/** @method array<int, bool>|int[]|array<int> truc */
4747
/** @method array<int, bool>|int[]|string|(int|bool)[] truc */
4848
/** @method array{scheme: string, host: string} truc */
49+
50+
/**
51+
* Avoid weird bug
52+
*
53+
* @method array{
54+
* @method array{scheme: int, host:
55+
* @method array<
56+
* @method array<int,
57+
* @method array|
58+
*/

SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.php renamed to SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @group SymfonyCustom
1313
*/
14-
class ValidScalarTypeNameUnitTest extends AbstractSniffUnitTest
14+
class ValidTypeHintUnitTest extends AbstractSniffUnitTest
1515
{
1616
/**
1717
* @return array
@@ -38,6 +38,8 @@ protected function getErrorList(): array
3838
46 => 1,
3939
47 => 1,
4040
48 => 1,
41+
54 => 1,
42+
56 => 1,
4143
];
4244
}
4345

0 commit comments

Comments
 (0)