Skip to content

Conversation

@94noni
Copy link
Contributor

@94noni 94noni commented Sep 15, 2025

Warning

WIP, just to make stuff rolling on my side


close #375

this PR introduce a new rule to fix comment var comment declaration to the new types tag


for now I am trying mostly to understand the project itself and token parsing etc
feel free to comment nonetheless

Copy link
Owner

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Hope it helps

Feel free to ask anything about how twig-cs-fixer works

// Listen COMMENT_TEXT_TYPE https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Token/Token.php#L42
// If the value is not @var return

if (!$this->isTokenMatching($token, Token::COMMENT_START_TYPE, '@var')) {
Copy link
Owner

Choose a reason for hiding this comment

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

COMMENT_START_TYPE is always {#

if (!$this->isTokenMatching($token, Token::COMMENT_START_TYPE)) {
     return;
}

$nextText = $tokens->findNext([Token::COMMENT_TEXT_TYPE,  Token::COMMENT_END_TYPE], $tokenIndex);
Assert::notFalse($nextText, 'Closing tag cannot be null.');
$nextTextToken = $tokens->get($nextText);
if (!$nextTextToken->isMatching(Token::COMMENT_TEXT_TYPE, '@var') {
    return;
}

$fixer = $this->addFixableError(
            'Variable comment declaration must be used via types tag instead of comment.',
            $token
);
if (null === $fixer) {
      return;
}

// Then do the fix

$end = $tokens->findNext(Token::COMMENT_END_TYPE, $nextText);
Assert::notFalse($nextText, 'Closing tag cannot be null.');

// You might look for varName and varType before adding the fixable error because you don't want to report an error for something like
// `{# @var #}` or `{# @var foo #}`
$varName = ...;
$varType = ...;

// We will have to handle things like 
// `{# @var foo App\Foo\Bar some comments to explain things #}`
$comment = ...;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Swap var comment to types comment for variable type hinting

2 participants