Skip to content

Commit

Permalink
Merge pull request #131 from VincentLanglet/controlStuctures
Browse files Browse the repository at this point in the history
✨ Add PSR12 restictrions for controles stuctures
  • Loading branch information
VincentLanglet authored Jun 5, 2020
2 parents b266f4b + 7cbccff commit 5596f47
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 53 deletions.
9 changes: 6 additions & 3 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
$nextToken = $tokens[$nextToken]['scope_closer'];
continue 2;
case T_OPEN_PARENTHESIS:
if (!isset($tokens[$nextToken]['parenthesis_owner'])
if (
!isset($tokens[$nextToken]['parenthesis_owner'])
|| $tokens[$nextToken]['parenthesis_owner'] !== $stackPtr
) {
$nextToken = $tokens[$nextToken]['parenthesis_closer'];
Expand All @@ -296,7 +297,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
break;
}

if (!in_array($tokens[$nextToken]['code'], [T_DOUBLE_ARROW, T_COMMA])
if (
!in_array($tokens[$nextToken]['code'], [T_DOUBLE_ARROW, T_COMMA])
&& $nextToken !== $end - 1
) {
continue;
Expand Down Expand Up @@ -347,7 +349,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
$indices[] = ['value' => $valueContent];
}

if (T_COMMA === $tokens[$nextToken]['code']
if (
T_COMMA === $tokens[$nextToken]['code']
&& T_WHITESPACE === $tokens[$nextToken - 1]['code']
) {
if ($tokens[$nextToken - 1]['content'] === $phpcsFile->eolChar) {
Expand Down
3 changes: 2 additions & 1 deletion SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

if (!isset($tokens[$stackPtr]['comment_closer'])
if (
!isset($tokens[$stackPtr]['comment_closer'])
|| ('' === $tokens[$tokens[$stackPtr]['comment_closer']]['content']
&& $phpcsFile->numTokens - 1 === $tokens[$stackPtr]['comment_closer'])
) {
Expand Down
3 changes: 2 additions & 1 deletion SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function processMemberVar(File $phpcsFile, $stackPtr): void
];

$commentEnd = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true);
if (false === $commentEnd
if (
false === $commentEnd
|| (T_DOC_COMMENT_CLOSE_TAG !== $tokens[$commentEnd]['code']
&& T_COMMENT !== $tokens[$commentEnd]['code'])
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function process(File $phpcsFile, $stackPtr): void
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$i = 1;
while (T_WHITESPACE === $tokens[$stackPtr - $i]['code']
while (
T_WHITESPACE === $tokens[$stackPtr - $i]['code']
|| $this->isComment($tokens[$stackPtr - $i])
) {
$i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
$opener = $phpcsFile->findPrevious([T_IF, T_CASE], $stackPtr);

if (false !== $opener
if (
false !== $opener
&& isset($tokens[$opener]['scope_closer'])
&& $stackPtr <= $tokens[$opener]['scope_closer']
) {
Expand Down
11 changes: 6 additions & 5 deletions SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public function process(File $phpcsFile, $stackPtr): void
}

// If this is a function call or something, we are OK.
if (in_array(
$tokens[$i]['code'],
[T_CONSTANT_ENCAPSED_STRING, T_CLOSE_PARENTHESIS, T_OPEN_PARENTHESIS, T_RETURN],
true
)
if (
in_array(
$tokens[$i]['code'],
[T_CONSTANT_ENCAPSED_STRING, T_CLOSE_PARENTHESIS, T_OPEN_PARENTHESIS, T_RETURN],
true
)
) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens[$function]['parenthesis_opener']
);

if ($scope
if (
$scope
&& $name
&& !in_array(
$tokens[$name]['content'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ private function getUseStatements(File $phpcsFile, int $scopePtr): array

$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
while (false !== $use && T_USE === $tokens[$use]['code']) {
if (!SniffHelper::isGlobalUse($phpcsFile, $use)
if (
!SniffHelper::isGlobalUse($phpcsFile, $use)
|| (null !== $end
&& (!isset($tokens[$use]['conditions'][$scopePtr])
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))
Expand Down
57 changes: 35 additions & 22 deletions SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function process(File $phpcsFile, $stackPtr): void
}

$comma = $phpcsFile->findNext(T_COMMA, $from + 1, $to);
if (false === $comma
if (
false === $comma
|| !$phpcsFile->findNext(Tokens::$emptyTokens, $comma + 1, $to, true)
) {
$fix = $phpcsFile->addFixableError(
Expand Down Expand Up @@ -159,7 +160,8 @@ public function process(File $phpcsFile, $stackPtr): void
$from = $stackPtr;
} else {
$from = $phpcsFile->findNext(Tokens::$emptyTokens, $prev + 1, null, true);
if (T_STRING === $tokens[$from]['code']
if (
T_STRING === $tokens[$from]['code']
&& in_array(strtolower($tokens[$from]['content']), ['const', 'function'], true)
) {
$from = $phpcsFile->findNext(Tokens::$emptyTokens, $from + 1, null, true);
Expand Down Expand Up @@ -192,7 +194,8 @@ private function removeUse(File $phpcsFile, int $from, int $to): void
$phpcsFile->fixer->beginChangeset();

// Remote whitespaces before in the same line
if (T_WHITESPACE === $tokens[$from - 1]['code']
if (
T_WHITESPACE === $tokens[$from - 1]['code']
&& $tokens[$from - 1]['line'] === $tokens[$from]['line']
&& $tokens[$from - 2]['line'] !== $tokens[$from]['line']
) {
Expand Down Expand Up @@ -243,7 +246,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool

$type = 'class';
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $usePtr + 1, null, true);
if (T_STRING === $tokens[$next]['code']
if (
T_STRING === $tokens[$next]['code']
&& in_array(strtolower($tokens[$next]['content']), ['const', 'function'], true)
) {
$type = strtolower($tokens[$next]['content']);
Expand Down Expand Up @@ -309,7 +313,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool

$match = null;

if (($isStringToken
if (
($isStringToken
&& (('const' !== $type && strtolower($tokens[$classUsed]['content']) === $searchName)
|| ('const' === $type && $tokens[$classUsed]['content'] === $searchName)))
|| ('class' === $type
Expand Down Expand Up @@ -346,7 +351,8 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
return true;
}
} elseif (T_DOC_COMMENT_STRING === $tokens[$classUsed]['code']) {
if (T_DOC_COMMENT_TAG === $tokens[$beforeUsage]['code']
if (
T_DOC_COMMENT_TAG === $tokens[$beforeUsage]['code']
&& in_array($tokens[$beforeUsage]['content'], SniffHelper::TAGS_WITH_TYPE, true)
) {
return true;
Expand Down Expand Up @@ -406,19 +412,23 @@ private function determineType(File $phpcsFile, int $beforePtr, int $ptr): ?stri

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

if (in_array(
$beforeCode,
[T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_CONST, T_AS, T_INSTEADOF],
true
)) {
if (
in_array(
$beforeCode,
[T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_CONST, T_AS, T_INSTEADOF],
true
)
) {
return null;
}

if (in_array(
$beforeCode,
[T_NEW, T_NULLABLE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF],
true
)) {
if (
in_array(
$beforeCode,
[T_NEW, T_NULLABLE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF],
true
)
) {
return 'class';
}

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

if (in_array(
$afterCode,
[T_DOUBLE_COLON, T_VARIABLE, T_ELLIPSIS, T_NS_SEPARATOR, T_OPEN_CURLY_BRACKET],
true
)) {
if (
in_array(
$afterCode,
[T_DOUBLE_COLON, T_VARIABLE, T_ELLIPSIS, T_NS_SEPARATOR, T_OPEN_CURLY_BRACKET],
true
)
) {
return 'class';
}

if (T_COLON === $beforeCode) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $beforePtr - 1, null, true);
if (false !== $prev
if (
false !== $prev
&& T_CLOSE_PARENTHESIS === $tokens[$prev]['code']
&& isset($tokens[$prev]['parenthesis_owner'])
&& T_FUNCTION === $tokens[$tokens[$prev]['parenthesis_owner']]['code']
Expand Down
6 changes: 4 additions & 2 deletions SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr - 1])
if (
isset($tokens[$stackPtr - 1])
&& $tokens[$stackPtr]['line'] === $tokens[$stackPtr - 1]['line']
) {
if (T_DOC_COMMENT_WHITESPACE !== $tokens[$stackPtr - 1]['code']) {
Expand Down Expand Up @@ -66,7 +67,8 @@ public function process(File $phpcsFile, $stackPtr): void
}
}

if (isset($tokens[$stackPtr + 1])
if (
isset($tokens[$stackPtr + 1])
&& $tokens[$stackPtr]['line'] === $tokens[$stackPtr + 1]['line']
&& T_DOC_COMMENT_WHITESPACE === $tokens[$stackPtr + 1]['code']
&& 1 < $tokens[$stackPtr + 1]['length']
Expand Down
6 changes: 4 additions & 2 deletions SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();

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

// General case
if ($tokens[$stackPtr]['content'] === $phpcsFile->eolChar
if (
$tokens[$stackPtr]['content'] === $phpcsFile->eolChar
&& isset($tokens[$stackPtr + 1])
&& $tokens[$stackPtr + 1]['content'] === $phpcsFile->eolChar
&& isset($tokens[$stackPtr + 2])
Expand Down
3 changes: 2 additions & 1 deletion SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr + 1])
if (
isset($tokens[$stackPtr + 1])
&& T_WHITESPACE === $tokens[$stackPtr + 1]['code']
&& false === strpos($tokens[$stackPtr + 1]['content'], $phpcsFile->eolChar)
) {
Expand Down
11 changes: 8 additions & 3 deletions SymfonyCustom/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
<!-- Include the PSR-12 (so PSR-1) standard without the file length limit -->
<rule ref="PSR12">
<exclude name="Generic.Files.LineLength"/>
<exclude name="PSR12.ControlStructures.ControlStructureSpacing"/>
<exclude name="PSR12.Operators.OperatorSpacing"/>
</rule>
<!-- Instead of PSR12.ControlStructures.ControlStructureSpacing -->
<rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>

<!-- Restrict the placement of the boolean operator -->
<rule ref="PSR12.ControlStructures.BooleanOperatorPlacement">
<properties>
<property name="allowOnly" value="first"/>
</properties>
</rule>

<!-- Instead of PSR12.Operators.OperatorSpacing -->
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
Expand Down
3 changes: 2 additions & 1 deletion TwigCS/src/Ruleset/Generic/BlankEOFSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function process(int $tokenPosition, array $tokens): void

if ($this->isTokenMatching($token, Token::EOF_TYPE)) {
$i = 0;
while (isset($tokens[$tokenPosition - ($i + 1)])
while (
isset($tokens[$tokenPosition - ($i + 1)])
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
) {
$i++;
Expand Down
6 changes: 4 additions & 2 deletions TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?in
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
if (
$this->isTokenMatching($token, Token::VAR_END_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
) {
Expand All @@ -42,7 +43,8 @@ protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
if (
$this->isTokenMatching($token, Token::VAR_START_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
) {
Expand Down
3 changes: 2 additions & 1 deletion TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function process(int $tokenPosition, array $tokens): void

if ($this->isTokenMatching($token, Token::EOL_TYPE)) {
$i = 0;
while (isset($tokens[$tokenPosition - ($i + 1)])
while (
isset($tokens[$tokenPosition - ($i + 1)])
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
) {
$i++;
Expand Down
3 changes: 2 additions & 1 deletion TwigCS/src/Runner/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ public function replaceToken(int $tokenPosition, string $content): bool
'loop' => $this->loops,
];
} else {
if ($content === $this->oldTokenValues[$tokenPosition]['prev']
if (
$content === $this->oldTokenValues[$tokenPosition]['prev']
&& ($this->loops - 1) === $this->oldTokenValues[$tokenPosition]['loop']
) {
if ($this->oldTokenValues[$tokenPosition]['loop'] >= ($this->loops - 1)) {
Expand Down
3 changes: 2 additions & 1 deletion TwigCS/src/Token/TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private function hasBody(TokenStream $stream): bool
return false;
}

if ($token->getType() === Token::NAME_TYPE
if (
$token->getType() === Token::NAME_TYPE
&& $token->getValue() === 'end'.$this->name
) {
return true;
Expand Down
9 changes: 6 additions & 3 deletions TwigCS/src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ protected function preflightSource(string $code): void
*/
protected function getTokenPosition(int $offset = 0): ?array
{
if (count($this->tokenPositions) === 0
if (
count($this->tokenPositions) === 0
|| !isset($this->tokenPositions[$this->currentPosition + $offset])
) {
return null;
Expand Down Expand Up @@ -433,7 +434,8 @@ protected function lexDqString(): void
{
if (preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) {
$this->lexStartInterpolation();
} elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor)
} elseif (
preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor)
&& strlen($match[0]) > 0
) {
$this->pushToken(Token::STRING_TYPE, $match[0]);
Expand Down Expand Up @@ -462,7 +464,8 @@ protected function lexInterpolation(): void
{
$bracket = end($this->bracketsAndTernary);

if ($this->options['interpolation'][0] === $bracket[0]
if (
$this->options['interpolation'][0] === $bracket[0]
&& preg_match($this->regexes['interpolation_end'], $this->code, $match, 0, $this->cursor)
) {
array_pop($this->bracketsAndTernary);
Expand Down

0 comments on commit 5596f47

Please sign in to comment.