Skip to content

Commit b2b65ef

Browse files
Merge pull request #98 from VincentLanglet/fixRuleset
Fix ruleset
2 parents 5ae660e + 12d50e9 commit b2b65ef

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
*/
1515
class ValidTypeHintSniff implements Sniff
1616
{
17-
private const TEXT = '\\\\a-z0-9';
18-
private const OPENER = '\<\[\{\(';
19-
private const CLOSER = '\>\]\}\)';
20-
private const MIDDLE = '\,\:';
21-
private const SEPARATOR = '\&\|';
17+
private const TEXT = '[\\\\a-z0-9]';
18+
private const OPENER = '\<|\[|\{|\(';
19+
private const MIDDLE = '\,|\:|\=\>';
20+
private const CLOSER = '\>|\]|\}|\)';
21+
private const SEPARATOR = '\&|\|';
2222

2323
/**
2424
* @return array
@@ -39,10 +39,12 @@ public function process(File $phpcsFile, $stackPtr): void
3939
if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
4040
preg_match(
4141
'`^((?:'
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.']'
42+
.'(?:'.self::OPENER.'|'.self::MIDDLE.'|'.self::SEPARATOR.')\s+'
43+
.'(?='.self::TEXT.'|'.self::OPENER.'|'.self::MIDDLE.'|'.self::CLOSER.'|'.self::SEPARATOR.')'
44+
.'|'.self::OPENER.'|'.self::MIDDLE.'|'.self::SEPARATOR
45+
.'|(?:'.self::TEXT.'|'.self::CLOSER.')\s+'
46+
.'(?='.self::OPENER.'|'.self::MIDDLE.'|'.self::CLOSER.'|'.self::SEPARATOR.')'
47+
.'|'.self::TEXT.'|'.self::CLOSER.''
4648
.')+)(.*)?`i',
4749
$tokens[($stackPtr + 2)]['content'],
4850
$match
@@ -52,7 +54,6 @@ public function process(File $phpcsFile, $stackPtr): void
5254
return;
5355
}
5456

55-
// Check type (can be multiple, separated by '|').
5657
$type = $match[1];
5758
$suggestedType = $this->getValidTypeName($type);
5859
if ($type !== $suggestedType) {
@@ -84,7 +85,7 @@ private function getValidTypeName(string $typeName): string
8485
{
8586
$typeNameWithoutSpace = str_replace(' ', '', $typeName);
8687
$parts = preg_split(
87-
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.self::SEPARATOR.'])/',
88+
'/('.self::OPENER.'|'.self::MIDDLE.'|'.self::CLOSER.'|'.self::SEPARATOR.')/',
8889
$typeNameWithoutSpace,
8990
-1,
9091
PREG_SPLIT_DELIM_CAPTURE
@@ -93,9 +94,15 @@ private function getValidTypeName(string $typeName): string
9394

9495
$validType = '';
9596
for ($i = 0; $i < $partsNumber; $i += 2) {
96-
$validType .= $this->suggestType($parts[$i]).$parts[$i + 1];
97+
$validType .= $this->suggestType($parts[$i]);
9798

98-
if (preg_match('/['.self::MIDDLE.']/', $parts[$i + 1])) {
99+
if ('=>' === $parts[$i + 1]) {
100+
$validType .= ' ';
101+
}
102+
103+
$validType .= $parts[$i + 1];
104+
105+
if (preg_match('/'.self::MIDDLE.'/', $parts[$i + 1])) {
99106
$validType .= ' ';
100107
}
101108
}

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ 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+
/** @method array(int=>string) truc */
4950

5051
/**
5152
* Avoid weird bug
@@ -56,3 +57,5 @@ echo ( float ) $a;
5657
* @method array<integer,
5758
* @method array|
5859
*/
60+
/** @method array<integer, */
61+
/** @method array<integer, $truc */

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ 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+
/** @method array(int => string) truc */
4950

5051
/**
5152
* Avoid weird bug
@@ -56,3 +57,5 @@ echo ( float ) $a;
5657
* @method array<int,
5758
* @method array|
5859
*/
60+
/** @method array<int, */
61+
/** @method array<int, $truc */

SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ protected function getErrorList(): array
3838
46 => 1,
3939
47 => 1,
4040
48 => 1,
41-
54 => 1,
42-
56 => 1,
41+
49 => 1,
42+
55 => 1,
43+
57 => 1,
44+
60 => 1,
45+
61 => 1,
4346
];
4447
}
4548

SymfonyCustom/ruleset.xml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
<!-- Include the PSR-12 (so PSR-1) standard without the file length limit -->
1414
<rule ref="PSR12">
1515
<exclude name="Generic.Files.LineLength"/>
16-
17-
<!-- TODO -->
1816
<exclude name="PSR12.ControlStructures.ControlStructureSpacing"/>
1917
<exclude name="PSR12.Operators.OperatorSpacing"/>
20-
<exclude name="Squiz.ControlStructures.ControlSignature"/>
2118
</rule>
19+
<!-- Instead of PSR12.ControlStructures.ControlStructureSpacing -->
20+
<rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>
21+
<!-- Instead of PSR12.Operators.OperatorSpacing-->
22+
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
23+
<properties>
24+
<property name="ignoreNewlines" value="true"/>
25+
</properties>
26+
</rule>
27+
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
28+
29+
<!-- Change warning to error -->
2230
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
2331
<type>error</type>
2432
</rule>
@@ -65,17 +73,11 @@
6573
<property name="spacingAfterLast" value="0"/>
6674
</properties>
6775
</rule>
68-
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
6976
<rule ref="Squiz.WhiteSpace.MemberVarSpacing">
7077
<properties>
7178
<property name="spacingBeforeFirst" value="0"/>
7279
</properties>
7380
</rule>
74-
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
75-
<properties>
76-
<property name="ignoreNewlines" value="true"/>
77-
</properties>
78-
</rule>
7981
<rule ref="Squiz.WhiteSpace.ObjectOperatorSpacing">
8082
<properties>
8183
<property name="ignoreNewlines" value="true"/>

0 commit comments

Comments
 (0)