Skip to content

Commit 5596f47

Browse files
Merge pull request #131 from VincentLanglet/controlStuctures
✨ Add PSR12 restictrions for controles stuctures
2 parents b266f4b + 7cbccff commit 5596f47

19 files changed

+95
-53
lines changed

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
287287
$nextToken = $tokens[$nextToken]['scope_closer'];
288288
continue 2;
289289
case T_OPEN_PARENTHESIS:
290-
if (!isset($tokens[$nextToken]['parenthesis_owner'])
290+
if (
291+
!isset($tokens[$nextToken]['parenthesis_owner'])
291292
|| $tokens[$nextToken]['parenthesis_owner'] !== $stackPtr
292293
) {
293294
$nextToken = $tokens[$nextToken]['parenthesis_closer'];
@@ -296,7 +297,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
296297
break;
297298
}
298299

299-
if (!in_array($tokens[$nextToken]['code'], [T_DOUBLE_ARROW, T_COMMA])
300+
if (
301+
!in_array($tokens[$nextToken]['code'], [T_DOUBLE_ARROW, T_COMMA])
300302
&& $nextToken !== $end - 1
301303
) {
302304
continue;
@@ -347,7 +349,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
347349
$indices[] = ['value' => $valueContent];
348350
}
349351

350-
if (T_COMMA === $tokens[$nextToken]['code']
352+
if (
353+
T_COMMA === $tokens[$nextToken]['code']
351354
&& T_WHITESPACE === $tokens[$nextToken - 1]['code']
352355
) {
353356
if ($tokens[$nextToken - 1]['content'] === $phpcsFile->eolChar) {

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
3131
{
3232
$tokens = $phpcsFile->getTokens();
3333

34-
if (!isset($tokens[$stackPtr]['comment_closer'])
34+
if (
35+
!isset($tokens[$stackPtr]['comment_closer'])
3536
|| ('' === $tokens[$tokens[$stackPtr]['comment_closer']]['content']
3637
&& $phpcsFile->numTokens - 1 === $tokens[$stackPtr]['comment_closer'])
3738
) {

SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void
3232
];
3333

3434
$commentEnd = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true);
35-
if (false === $commentEnd
35+
if (
36+
false === $commentEnd
3637
|| (T_DOC_COMMENT_CLOSE_TAG !== $tokens[$commentEnd]['code']
3738
&& T_COMMENT !== $tokens[$commentEnd]['code'])
3839
) {

SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function process(File $phpcsFile, $stackPtr): void
6767
if ($fix) {
6868
$phpcsFile->fixer->beginChangeset();
6969
$i = 1;
70-
while (T_WHITESPACE === $tokens[$stackPtr - $i]['code']
70+
while (
71+
T_WHITESPACE === $tokens[$stackPtr - $i]['code']
7172
|| $this->isComment($tokens[$stackPtr - $i])
7273
) {
7374
$i++;

SymfonyCustom/Sniffs/Formatting/ConditionalReturnOrThrowSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
3131
$tokens = $phpcsFile->getTokens();
3232
$opener = $phpcsFile->findPrevious([T_IF, T_CASE], $stackPtr);
3333

34-
if (false !== $opener
34+
if (
35+
false !== $opener
3536
&& isset($tokens[$opener]['scope_closer'])
3637
&& $stackPtr <= $tokens[$opener]['scope_closer']
3738
) {

SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ public function process(File $phpcsFile, $stackPtr): void
5454
}
5555

5656
// If this is a function call or something, we are OK.
57-
if (in_array(
58-
$tokens[$i]['code'],
59-
[T_CONSTANT_ENCAPSED_STRING, T_CLOSE_PARENTHESIS, T_OPEN_PARENTHESIS, T_RETURN],
60-
true
61-
)
57+
if (
58+
in_array(
59+
$tokens[$i]['code'],
60+
[T_CONSTANT_ENCAPSED_STRING, T_CLOSE_PARENTHESIS, T_OPEN_PARENTHESIS, T_RETURN],
61+
true
62+
)
6263
) {
6364
return;
6465
}

SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function process(File $phpcsFile, $stackPtr): void
6363
$tokens[$function]['parenthesis_opener']
6464
);
6565

66-
if ($scope
66+
if (
67+
$scope
6768
&& $name
6869
&& !in_array(
6970
$tokens[$name]['content'],

SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ private function getUseStatements(File $phpcsFile, int $scopePtr): array
9090

9191
$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
9292
while (false !== $use && T_USE === $tokens[$use]['code']) {
93-
if (!SniffHelper::isGlobalUse($phpcsFile, $use)
93+
if (
94+
!SniffHelper::isGlobalUse($phpcsFile, $use)
9495
|| (null !== $end
9596
&& (!isset($tokens[$use]['conditions'][$scopePtr])
9697
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function process(File $phpcsFile, $stackPtr): void
5555
}
5656

5757
$comma = $phpcsFile->findNext(T_COMMA, $from + 1, $to);
58-
if (false === $comma
58+
if (
59+
false === $comma
5960
|| !$phpcsFile->findNext(Tokens::$emptyTokens, $comma + 1, $to, true)
6061
) {
6162
$fix = $phpcsFile->addFixableError(
@@ -159,7 +160,8 @@ public function process(File $phpcsFile, $stackPtr): void
159160
$from = $stackPtr;
160161
} else {
161162
$from = $phpcsFile->findNext(Tokens::$emptyTokens, $prev + 1, null, true);
162-
if (T_STRING === $tokens[$from]['code']
163+
if (
164+
T_STRING === $tokens[$from]['code']
163165
&& in_array(strtolower($tokens[$from]['content']), ['const', 'function'], true)
164166
) {
165167
$from = $phpcsFile->findNext(Tokens::$emptyTokens, $from + 1, null, true);
@@ -192,7 +194,8 @@ private function removeUse(File $phpcsFile, int $from, int $to): void
192194
$phpcsFile->fixer->beginChangeset();
193195

194196
// Remote whitespaces before in the same line
195-
if (T_WHITESPACE === $tokens[$from - 1]['code']
197+
if (
198+
T_WHITESPACE === $tokens[$from - 1]['code']
196199
&& $tokens[$from - 1]['line'] === $tokens[$from]['line']
197200
&& $tokens[$from - 2]['line'] !== $tokens[$from]['line']
198201
) {
@@ -243,7 +246,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
243246

244247
$type = 'class';
245248
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $usePtr + 1, null, true);
246-
if (T_STRING === $tokens[$next]['code']
249+
if (
250+
T_STRING === $tokens[$next]['code']
247251
&& in_array(strtolower($tokens[$next]['content']), ['const', 'function'], true)
248252
) {
249253
$type = strtolower($tokens[$next]['content']);
@@ -309,7 +313,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
309313

310314
$match = null;
311315

312-
if (($isStringToken
316+
if (
317+
($isStringToken
313318
&& (('const' !== $type && strtolower($tokens[$classUsed]['content']) === $searchName)
314319
|| ('const' === $type && $tokens[$classUsed]['content'] === $searchName)))
315320
|| ('class' === $type
@@ -346,7 +351,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
346351
return true;
347352
}
348353
} elseif (T_DOC_COMMENT_STRING === $tokens[$classUsed]['code']) {
349-
if (T_DOC_COMMENT_TAG === $tokens[$beforeUsage]['code']
354+
if (
355+
T_DOC_COMMENT_TAG === $tokens[$beforeUsage]['code']
350356
&& in_array($tokens[$beforeUsage]['content'], SniffHelper::TAGS_WITH_TYPE, true)
351357
) {
352358
return true;
@@ -406,19 +412,23 @@ private function determineType(File $phpcsFile, int $beforePtr, int $ptr): ?stri
406412

407413
$beforeCode = $tokens[$beforePtr]['code'];
408414

409-
if (in_array(
410-
$beforeCode,
411-
[T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_CONST, T_AS, T_INSTEADOF],
412-
true
413-
)) {
415+
if (
416+
in_array(
417+
$beforeCode,
418+
[T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_CONST, T_AS, T_INSTEADOF],
419+
true
420+
)
421+
) {
414422
return null;
415423
}
416424

417-
if (in_array(
418-
$beforeCode,
419-
[T_NEW, T_NULLABLE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF],
420-
true
421-
)) {
425+
if (
426+
in_array(
427+
$beforeCode,
428+
[T_NEW, T_NULLABLE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF],
429+
true
430+
)
431+
) {
422432
return 'class';
423433
}
424434

@@ -459,17 +469,20 @@ private function determineType(File $phpcsFile, int $beforePtr, int $ptr): ?stri
459469
return 'function';
460470
}
461471

462-
if (in_array(
463-
$afterCode,
464-
[T_DOUBLE_COLON, T_VARIABLE, T_ELLIPSIS, T_NS_SEPARATOR, T_OPEN_CURLY_BRACKET],
465-
true
466-
)) {
472+
if (
473+
in_array(
474+
$afterCode,
475+
[T_DOUBLE_COLON, T_VARIABLE, T_ELLIPSIS, T_NS_SEPARATOR, T_OPEN_CURLY_BRACKET],
476+
true
477+
)
478+
) {
467479
return 'class';
468480
}
469481

470482
if (T_COLON === $beforeCode) {
471483
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $beforePtr - 1, null, true);
472-
if (false !== $prev
484+
if (
485+
false !== $prev
473486
&& T_CLOSE_PARENTHESIS === $tokens[$prev]['code']
474487
&& isset($tokens[$prev]['parenthesis_owner'])
475488
&& T_FUNCTION === $tokens[$tokens[$prev]['parenthesis_owner']]['code']

SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
3131
{
3232
$tokens = $phpcsFile->getTokens();
3333

34-
if (isset($tokens[$stackPtr - 1])
34+
if (
35+
isset($tokens[$stackPtr - 1])
3536
&& $tokens[$stackPtr]['line'] === $tokens[$stackPtr - 1]['line']
3637
) {
3738
if (T_DOC_COMMENT_WHITESPACE !== $tokens[$stackPtr - 1]['code']) {
@@ -66,7 +67,8 @@ public function process(File $phpcsFile, $stackPtr): void
6667
}
6768
}
6869

69-
if (isset($tokens[$stackPtr + 1])
70+
if (
71+
isset($tokens[$stackPtr + 1])
7072
&& $tokens[$stackPtr]['line'] === $tokens[$stackPtr + 1]['line']
7173
&& T_DOC_COMMENT_WHITESPACE === $tokens[$stackPtr + 1]['code']
7274
&& 1 < $tokens[$stackPtr + 1]['length']

SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
3131
$tokens = $phpcsFile->getTokens();
3232

3333
// Special case for the first line
34-
if (isset($tokens[$stackPtr - 1])
34+
if (
35+
isset($tokens[$stackPtr - 1])
3536
&& T_OPEN_TAG === $tokens[$stackPtr - 1]['code']
3637
&& $tokens[$stackPtr]['content'] === $phpcsFile->eolChar
3738
&& isset($tokens[$stackPtr + 1])
@@ -49,7 +50,8 @@ public function process(File $phpcsFile, $stackPtr): void
4950
}
5051

5152
// General case
52-
if ($tokens[$stackPtr]['content'] === $phpcsFile->eolChar
53+
if (
54+
$tokens[$stackPtr]['content'] === $phpcsFile->eolChar
5355
&& isset($tokens[$stackPtr + 1])
5456
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar
5557
&& isset($tokens[$stackPtr + 2])

SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
3131
{
3232
$tokens = $phpcsFile->getTokens();
3333

34-
if (isset($tokens[$stackPtr + 1])
34+
if (
35+
isset($tokens[$stackPtr + 1])
3536
&& T_WHITESPACE === $tokens[$stackPtr + 1]['code']
3637
&& false === strpos($tokens[$stackPtr + 1]['content'], $phpcsFile->eolChar)
3738
) {

SymfonyCustom/ruleset.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
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-
<exclude name="PSR12.ControlStructures.ControlStructureSpacing"/>
1716
<exclude name="PSR12.Operators.OperatorSpacing"/>
1817
</rule>
19-
<!-- Instead of PSR12.ControlStructures.ControlStructureSpacing -->
20-
<rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>
18+
19+
<!-- Restrict the placement of the boolean operator -->
20+
<rule ref="PSR12.ControlStructures.BooleanOperatorPlacement">
21+
<properties>
22+
<property name="allowOnly" value="first"/>
23+
</properties>
24+
</rule>
25+
2126
<!-- Instead of PSR12.Operators.OperatorSpacing -->
2227
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
2328
<properties>

TwigCS/src/Ruleset/Generic/BlankEOFSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function process(int $tokenPosition, array $tokens): void
2727

2828
if ($this->isTokenMatching($token, Token::EOF_TYPE)) {
2929
$i = 0;
30-
while (isset($tokens[$tokenPosition - ($i + 1)])
30+
while (
31+
isset($tokens[$tokenPosition - ($i + 1)])
3132
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
3233
) {
3334
$i++;

TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?in
2222
{
2323
$token = $tokens[$tokenPosition];
2424

25-
if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
25+
if (
26+
$this->isTokenMatching($token, Token::VAR_END_TYPE)
2627
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
2728
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
2829
) {
@@ -42,7 +43,8 @@ protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
4243
{
4344
$token = $tokens[$tokenPosition];
4445

45-
if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
46+
if (
47+
$this->isTokenMatching($token, Token::VAR_START_TYPE)
4648
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
4749
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
4850
) {

TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function process(int $tokenPosition, array $tokens): void
2727

2828
if ($this->isTokenMatching($token, Token::EOL_TYPE)) {
2929
$i = 0;
30-
while (isset($tokens[$tokenPosition - ($i + 1)])
30+
while (
31+
isset($tokens[$tokenPosition - ($i + 1)])
3132
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
3233
) {
3334
$i++;

TwigCS/src/Runner/Fixer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ public function replaceToken(int $tokenPosition, string $content): bool
275275
'loop' => $this->loops,
276276
];
277277
} else {
278-
if ($content === $this->oldTokenValues[$tokenPosition]['prev']
278+
if (
279+
$content === $this->oldTokenValues[$tokenPosition]['prev']
279280
&& ($this->loops - 1) === $this->oldTokenValues[$tokenPosition]['loop']
280281
) {
281282
if ($this->oldTokenValues[$tokenPosition]['loop'] >= ($this->loops - 1)) {

TwigCS/src/Token/TokenParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ private function hasBody(TokenStream $stream): bool
9494
return false;
9595
}
9696

97-
if ($token->getType() === Token::NAME_TYPE
97+
if (
98+
$token->getType() === Token::NAME_TYPE
9899
&& $token->getValue() === 'end'.$this->name
99100
) {
100101
return true;

TwigCS/src/Token/Tokenizer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ protected function preflightSource(string $code): void
283283
*/
284284
protected function getTokenPosition(int $offset = 0): ?array
285285
{
286-
if (count($this->tokenPositions) === 0
286+
if (
287+
count($this->tokenPositions) === 0
287288
|| !isset($this->tokenPositions[$this->currentPosition + $offset])
288289
) {
289290
return null;
@@ -433,7 +434,8 @@ protected function lexDqString(): void
433434
{
434435
if (preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) {
435436
$this->lexStartInterpolation();
436-
} elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor)
437+
} elseif (
438+
preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor)
437439
&& strlen($match[0]) > 0
438440
) {
439441
$this->pushToken(Token::STRING_TYPE, $match[0]);
@@ -462,7 +464,8 @@ protected function lexInterpolation(): void
462464
{
463465
$bracket = end($this->bracketsAndTernary);
464466

465-
if ($this->options['interpolation'][0] === $bracket[0]
467+
if (
468+
$this->options['interpolation'][0] === $bracket[0]
466469
&& preg_match($this->regexes['interpolation_end'], $this->code, $match, 0, $this->cursor)
467470
) {
468471
array_pop($this->bracketsAndTernary);

0 commit comments

Comments
 (0)