-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sniff to enforce short echo tag on single-line echoing
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace InpsydeTemplates\Sniffs\Formatting; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
/** | ||
* @psalm-type Token = array{ | ||
* type: string, | ||
* code: string|int, | ||
* line: int | ||
* } | ||
*/ | ||
final class ShortEchoTagSniff implements Sniff | ||
{ | ||
/** | ||
* @return list<int|string> | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_ECHO, | ||
]; | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* | ||
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr): void | ||
{ | ||
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration | ||
|
||
/** @var array<int, Token> $tokens */ | ||
$tokens = $phpcsFile->getTokens(); | ||
$currentLine = $tokens[$stackPtr]['line']; | ||
|
||
$prevPtr = $phpcsFile->findPrevious( | ||
Tokens::$emptyTokens, | ||
($stackPtr - 1), | ||
null, | ||
true | ||
); | ||
|
||
if (!is_int($prevPtr) || !isset($tokens[$prevPtr])) { | ||
return; | ||
} | ||
|
||
$prevToken = $tokens[$prevPtr]; | ||
|
||
if ($prevToken['line'] !== $currentLine) { | ||
return; | ||
} | ||
|
||
if ($prevToken['code'] !== T_OPEN_TAG) { | ||
return; | ||
} | ||
|
||
$closeTagPtr = $phpcsFile->findNext( | ||
T_CLOSE_TAG, | ||
($stackPtr + 1), | ||
); | ||
|
||
if ( | ||
!is_int($closeTagPtr) | ||
|| !isset($tokens[$closeTagPtr]) | ||
|| $tokens[$closeTagPtr]['line'] !== $currentLine | ||
) { | ||
return; | ||
} | ||
|
||
$message = sprintf( | ||
'Single line output on line %d' | ||
. ' should use short echo tag `<?= ` instead of `<?php echo`.', | ||
$currentLine | ||
); | ||
|
||
if ($phpcsFile->addFixableWarning($message, $stackPtr, 'Encouraged')) { | ||
$this->fix($prevPtr, $stackPtr, $phpcsFile); | ||
} | ||
} | ||
|
||
private function fix(int $openTagPtr, int $echoPtr, File $file): void | ||
{ | ||
$fixer = $file->fixer; | ||
$fixer->beginChangeset(); | ||
|
||
$fixer->replaceToken($echoPtr, ''); | ||
$fixer->replaceToken($openTagPtr, '<?='); | ||
|
||
$fixer->endChangeset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
// @phpcsSniff InpsydeTemplates.Formatting.ShortEchoTag | ||
|
||
echo 'content'; | ||
|
||
echo 'content', 'yet another'; | ||
|
||
echo ('content'), 'yet'; | ||
|
||
echo htmlentities('content'); | ||
|
||
echo $GLOBALS['flag'] ? 'yes' : 'no'; echo 'maybe', 'no'; ?> | ||
|
||
<?php echo 'content'; | ||
echo 'no' ?> | ||
|
||
<?php echo 'content' // @phpcsWarningOnThisLine ?> | ||
|
||
<?php echo 'content', 'yet another'; // @phpcsWarningOnThisLine ?> | ||
|
||
<?php echo ('content'), 'yet'; // @phpcsWarningOnThisLine ?> | ||
|
||
<?php echo htmlentities('content') // @phpcsWarningOnThisLine ?> | ||
|
||
<?php echo $GLOBALS['flag'] ? 'yes' : 'no'; echo 'maybe', 'no'; // @phpcsWarningOnThisLine ?> |