-
Notifications
You must be signed in to change notification settings - Fork 1
Sniff/control signature #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Stefna\Sniffs\ControlStructures; | ||
|
|
||
| use PHP_CodeSniffer\Files\File; | ||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
| use PHP_CodeSniffer\Util\Tokens; | ||
| use Stefna\Utils\TokenCollection; | ||
|
|
||
| class ControlSignatureSniff implements Sniff | ||
| { | ||
| public function register(): array | ||
| { | ||
| return [ | ||
| T_DO, | ||
| T_ELSE, | ||
| T_ELSEIF, | ||
| T_FOR, | ||
| T_FOREACH, | ||
| T_IF, | ||
| T_SWITCH, | ||
| T_WHILE, | ||
| ]; | ||
| } | ||
|
|
||
| public function process(File $phpcsFile, int $stackPtr): void | ||
| { | ||
| $tokens = new TokenCollection($phpcsFile->getTokens()); | ||
|
|
||
| if (!$tokens->has($stackPtr + 1)) { | ||
| return; | ||
| } | ||
|
|
||
| $isAlternative = false; | ||
| if ($tokens->hasScopeOpener($stackPtr) && $tokens->code($tokens->scopeOpener($stackPtr)) === T_COLON) { | ||
| $isAlternative = true; | ||
| } | ||
|
|
||
| // Single newline after opening brace. | ||
| if ($tokens->hasScopeOpener($stackPtr)) { | ||
| $opener = $tokens->scopeOpener($stackPtr); | ||
| for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) { | ||
| $code = $tokens->code($next); | ||
|
|
||
| if ($code === T_WHITESPACE || ($code === T_INLINE_HTML && trim($tokens->content($next)) === '')) { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip all empty tokens on the same line as the opener | ||
| if ($tokens->sameLine($next, $opener) && (isset(Tokens::EMPTY_TOKENS[$code]) || $code === T_CLOSE_TAG)) { | ||
| continue; | ||
| } | ||
|
|
||
| // We found the first bit of a code, or a comment on the following line | ||
| break; | ||
| } | ||
|
|
||
| if ($tokens->sameLine($next, $opener)) { | ||
| $error = 'Newline required after opening brace'; | ||
| $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace'); | ||
| if ($fix) { | ||
| $phpcsFile->fixer->beginChangeset(); | ||
| for ($i = $opener + 1; $i < $next; $i++) { | ||
| if (trim($tokens->content($i)) !== '') { | ||
| break; | ||
| } | ||
|
|
||
| $phpcsFile->fixer->replaceToken($i, ''); | ||
| } | ||
|
|
||
| $phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar); | ||
| $phpcsFile->fixer->endChangeset(); | ||
| } | ||
| } | ||
| } | ||
| elseif ($tokens->code($stackPtr) === T_WHILE) { | ||
| // Zero spaces after parenthesis closer. | ||
| $closer = $tokens->parenthesisCloser($stackPtr); | ||
| $found = 0; | ||
| if ($tokens->code($closer + 1) === T_WHITESPACE) { | ||
| if (strpos($tokens->content($closer + 1), $phpcsFile->eolChar) !== false) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace with str_contains |
||
| $found = 'newline'; | ||
| } | ||
| else { | ||
| $found = strlen($tokens->content($closer + 1)); | ||
| } | ||
| } | ||
|
|
||
| if ($found !== 0) { | ||
| $error = 'Expected 0 spaces before semicolon; %s found'; | ||
| $data = [$found]; | ||
| $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data); | ||
| if ($fix) { | ||
| $phpcsFile->fixer->replaceToken($closer + 1, ''); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Only want to check multi-keyword structures from here on. | ||
| if ($tokens->code($stackPtr) === T_WHILE) { | ||
| if (!$tokens->hasScopeCloser($stackPtr)) { | ||
| return; | ||
| } | ||
|
|
||
| $closer = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, $stackPtr - 1, exclude: true); | ||
| if ($closer === false || $tokens->code($closer) !== T_CLOSE_CURLY_BRACKET || $tokens->code($tokens->scopeCondition($closer)) !== T_DO) { | ||
| return; | ||
| } | ||
| } | ||
| elseif (in_array($tokens->code($stackPtr), [T_ELSE, T_ELSEIF, T_CATCH, T_FINALLY], true)) { | ||
| if ($tokens->hasScopeOpener($stackPtr) && $tokens->code($tokens->scopeOpener($stackPtr)) === T_COLON) { | ||
| // Special case for alternative syntax, where this token is actually | ||
| // the closer for the previous block, so there is no spacing to check. | ||
| return; | ||
| } | ||
|
|
||
| $closer = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, $stackPtr - 1, exclude: true); | ||
| if ($closer === false || $tokens->code($closer) !== T_CLOSE_CURLY_BRACKET) { | ||
| return; | ||
| } | ||
| } | ||
| else { | ||
| return; | ||
| } | ||
|
|
||
| // Single space after closing brace. | ||
| $found = 1; | ||
| if ($tokens->code($closer + 1) !== T_WHITESPACE) { | ||
| $found = 0; | ||
| } | ||
| elseif (!$tokens->sameLine($closer, $stackPtr)) { | ||
| // Custom to allow newline before else and catch | ||
| $found = 1; | ||
| } | ||
| elseif ($tokens->content($closer + 1) !== ' ') { | ||
| $found = strlen($tokens->content($closer + 1)); | ||
| } | ||
|
|
||
| if ($found !== 1) { | ||
| $error = 'Expected 1 space after closing brace; %s found'; | ||
| $data = [$found]; | ||
|
|
||
| if ($phpcsFile->findNext(Tokens::COMMENT_TOKENS, $closer + 1, $stackPtr) !== false) { | ||
| // Comment found between closing brace and keyword, don't auto-fix. | ||
| $phpcsFile->addError($error, $closer, 'SpaceAfterCloseBrace', $data); | ||
| return; | ||
| } | ||
|
|
||
| $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseBrace', $data); | ||
| if ($fix) { | ||
| if ($found === 0) { | ||
| $phpcsFile->fixer->addContent($closer, ' '); | ||
| } | ||
| else { | ||
| $phpcsFile->fixer->replaceToken($closer + 1, ' '); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Stefna\Utils; | ||
|
|
||
| class TokenCollection | ||
| { | ||
| /** | ||
| * @param array<int,mixed> $tokens | ||
| */ | ||
| public function __construct( | ||
| private array $tokens, | ||
| ) {} | ||
|
|
||
| public function code(int $stackPtr): int|string | ||
| { | ||
| return $this->tokens[$stackPtr]['code']; | ||
| } | ||
|
|
||
| public function content(int $stackPtr): string | ||
| { | ||
| return $this->tokens[$stackPtr]['content']; | ||
| } | ||
|
|
||
| public function line(int $stackPtr): int | ||
| { | ||
| return $this->tokens[$stackPtr]['line']; | ||
| } | ||
|
|
||
| public function parenthesisCloser(int $stackPtr): int | ||
| { | ||
| return $this->tokens[$stackPtr]['parenthesis_closer']; | ||
| } | ||
|
|
||
| public function scopeCondition(int $stackPtr): int | ||
| { | ||
| return $this->tokens[$stackPtr]['scope_condition']; | ||
| } | ||
|
|
||
| public function scopeOpener(int $stackPtr): int | ||
| { | ||
| return $this->tokens[$stackPtr]['scope_opener']; | ||
| } | ||
|
|
||
| public function has(int $stackPtr): bool | ||
| { | ||
| return isset($this->tokens[$stackPtr]); | ||
| } | ||
|
|
||
| public function hasScopeCloser(int $stackPtr): bool | ||
| { | ||
| return isset($this->tokens[$stackPtr]['scope_closer']); | ||
| } | ||
|
|
||
| public function hasScopeOpener(int $stackPtr): bool | ||
| { | ||
| return isset($this->tokens[$stackPtr]['scope_opener']); | ||
| } | ||
|
|
||
| public function hasParenthesisCloser(int $stackPtr): bool | ||
| { | ||
| return isset($this->tokens[$stackPtr]['parenthesis_closer']); | ||
| } | ||
|
|
||
|
|
||
| public function sameLine(int $firstPtr, int $secondPtr): bool | ||
| { | ||
| return $this->tokens[$firstPtr]['line'] === $this->tokens[$secondPtr]['line']; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace StefnaTest\Sniffs\ControlStructures; | ||
|
|
||
| use StefnaTest\Sniffs\TestCase; | ||
|
|
||
| class ControlSignatureSniffTest extends TestCase | ||
| { | ||
| public function testNoErrors(): void | ||
| { | ||
| $report = $this->checkFile('OK'); | ||
|
|
||
| self::assertNoSniffErrorInFile($report); | ||
| } | ||
|
|
||
| public function testFixErrors(): void | ||
| { | ||
| $report = $this->checkFile('Bad'); | ||
|
|
||
| self::assertSniffError($report, 3, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 3, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 3, 'SpaceAfterCloseBrace'); | ||
| self::assertSniffError($report, 5, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 7, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 9, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 11, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 13, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 13, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 13, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 15, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 17, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 19, 'NewlineAfterOpenBrace'); | ||
| self::assertSniffError($report, 21, 'NewlineAfterOpenBrace'); | ||
|
|
||
| self::assertAllFixedInFile($report); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| if (false){}elseif(true){}else{} | ||
|
|
||
| do{}while(0) ; | ||
|
|
||
| for(;;){} | ||
|
|
||
| foreach([] as $k => $v){} | ||
|
|
||
| switch(false){default:break;} | ||
|
|
||
| if(false):elseif(true):else:endif; | ||
|
|
||
| while(false):endwhile; | ||
|
|
||
| for(;;):endfor; | ||
|
|
||
| foreach([] as $k => $v):endforeach; | ||
|
|
||
| switch(false):default:break;endswitch; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| if (false) { | ||
| } elseif (true) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be ok |
||
| } else { | ||
| } | ||
|
|
||
| do { | ||
| }while (0); | ||
|
|
||
| for (;;) { | ||
| } | ||
|
|
||
| foreach ([] as $k => $v) { | ||
| } | ||
|
|
||
| switch (false) { | ||
| default: | ||
| break;} | ||
|
|
||
| if (false) : | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just BAN the ":" syntax?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the "old" syntax is supported to allow codestyle of template file. But I don't think we actually use it. And for eveything new we use twig also |
||
| elseif (true) : | ||
| else : | ||
| endif; | ||
|
|
||
| while (false) : | ||
| endwhile; | ||
|
|
||
| for (;;) : | ||
| endfor; | ||
|
|
||
| foreach ([] as $k => $v) : | ||
| endforeach; | ||
|
|
||
| switch (false) : | ||
| default: | ||
| break; | ||
| endswitch; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?