Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<!-- <rule ref="Stefna.ControlStructures.TryCatchDeclaration"/> -->
<!-- <rule ref="Stefna.ControlStructures.ControlStructureSpacing"/> -->
<!-- needed special control structure check to not collide with ElseCatch -->
<!-- <rule ref="Stefna.ControlStructures.ControlSignature"/> -->
<rule ref="Stefna.ControlStructures.ControlSignature"/>
<!-- <rule ref="Stefna.ControlStructures.ScopeClosingBrace"/> -->
<!-- <rule ref="Stefna.Naming.CamelCapsMethodName"/> -->
<!---->
Expand Down
160 changes: 160 additions & 0 deletions src/Stefna/Sniffs/ControlStructures/ControlSignatureSniff.php
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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, ' ');
}
}
}
}
}
69 changes: 69 additions & 0 deletions src/Stefna/Utils/TokenCollection.php
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'];
}
}
37 changes: 37 additions & 0 deletions tests/Sniffs/ControlStructures/ControlSignatureSniffTest.php
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);
}
}
21 changes: 21 additions & 0 deletions tests/Sniffs/ControlStructures/data/ControlSignatureSniff.Bad.php
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;
38 changes: 38 additions & 0 deletions tests/Sniffs/ControlStructures/data/ControlSignatureSniff.OK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

if (false) {
} elseif (true) {
Copy link
Member

Choose a reason for hiding this comment

The 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) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just BAN the ":" syntax?

Copy link
Member

Choose a reason for hiding this comment

The 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;
25 changes: 1 addition & 24 deletions tests/Sniffs/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,12 @@ protected function checkFile(string $fileVairant): File
{

$codeSniffer = new Runner();
$codeSniffer->config = new Config(['-s']);
$codeSniffer->config = new Config(['-s', '--standard=phpcs.xml']);
$codeSniffer->init();

$sniffFqcn = static::getSniffFqcn();
$sniff = new $sniffFqcn();

$codeSniffer->ruleset->sniffs = [$sniffFqcn => $sniff];

$codeSniffer->ruleset->populateTokenListeners();

$filePath = static::getSniffDataVariantFilePath($fileVairant);

$file = new LocalFile($filePath, $codeSniffer->ruleset, $codeSniffer->config);
$file->process();

return $file;
}

protected function fixFile(string $fileVairant): File
{
$codeSniffer = new Runner();
$codeSniffer->config = new Config(['-s']);
$codeSniffer->init();

$sniffFqcn = static::getSniffFqcn();
$sniff = new $sniffFqcn();

$codeSniffer->ruleset->sniffs = [$sniffFqcn => $sniff];

$codeSniffer->ruleset->populateTokenListeners();

$filePath = static::getSniffDataVariantFilePath($fileVairant);
Expand Down