Skip to content

Commit

Permalink
feat: add sniff to enforce short echo tag on single-line echoing
Browse files Browse the repository at this point in the history
  • Loading branch information
shvlv committed Aug 15, 2024
1 parent dacb6d6 commit 8ebb223
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
99 changes: 99 additions & 0 deletions InpsydeTemplates/Sniffs/Formatting/ShortEchoTagSniff.php
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();
}
}
28 changes: 28 additions & 0 deletions tests/unit/fixtures/short-echo-tag.php
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 ?>

0 comments on commit 8ebb223

Please sign in to comment.