Skip to content
Open
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
78 changes: 78 additions & 0 deletions src/Rules/Comment/CommentTypeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules\Comment;

use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;

/**
* Change a variable comment "{# @var var_name type" with "{% types var_name: 'type' %}".
*/
final class CommentTypeRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void

Check failure on line 15 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #2 $tokens (array) of method TwigCsFixer\Rules\Comment\CommentTypeRule::process() is not contravariant with parameter #2 $tokens (TwigCsFixer\Token\Tokens) of method TwigCsFixer\Rules\AbstractRule::process().

Check failure on line 15 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method TwigCsFixer\Rules\Comment\CommentTypeRule::process() has parameter $tokens with no value type specified in iterable type array.
{
var_dump($tokenPosition, $tokens);
$token = $tokens[$tokenPosition];
var_dump($token);

// 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')) {

Check failure on line 24 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method TwigCsFixer\Rules\Comment\CommentTypeRule::isTokenMatching().
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 = ...;

return;
}

// is warning the proper violation "level" here?
$fixer = $this->addFixableWarning(
'Variable comment declaration must be used via types tag instead of comment.',
$token

Check failure on line 31 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #2 $token of method TwigCsFixer\Rules\AbstractFixableRule::addFixableWarning() expects TwigCsFixer\Token\Token, mixed given.
);
if (null === $fixer) {
return;
}

$tokenValue = $token->getValue();

Check failure on line 37 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method getValue() on mixed.
var_dump($tokenValue);

$firstTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition);

Check failure on line 40 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method TwigCsFixer\Rules\Comment\CommentTypeRule::findNext().
var_dump($firstTextType);
$secondTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition);

Check failure on line 42 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method TwigCsFixer\Rules\Comment\CommentTypeRule::findNext().
var_dump($secondTextType);
$thirdTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition);

Check failure on line 44 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method TwigCsFixer\Rules\Comment\CommentTypeRule::findNext().
var_dump($thirdTextType);

// TODO

/*

Else parse the whole comment, looking for COMMENT_START_TYPE before and COMMENT_END_TYPE

COMMENT_START_TYPE
COMMENT_WHITESPACE_TYPE
COMMENT_TEXT_TYPE
COMMENT_WHITESPACE_TYPE
COMMENT_TEXT_TYPE
COMMENT_WHITESPACE_TYPE
COMMENT_TEXT_TYPE
COMMENT_WHITESPACE_TYPE
COMMENT_END_TYPE

Rewrite it

{% types foo: 'bar' %}
where foo is the 2nd COMMENT_TEXT_TYPE and bar the third.

*/

$variableName = $secondTextType;
$variableType = $thirdTextType;

$fixer->replaceToken(
$tokenPosition,
\sprintf("{% types %s: '%s'", $variableName, $variableType)

Check failure on line 75 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #3 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 75 in src/Rules/Comment/CommentTypeRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% types foo_bar: 'boolean' %}
{% types bar_baz: 'integer|null' %}
{% types admin: '\App\Entity\Admin' %}
{% types user: '\App\Entity\User|null' %}
{% types users: '\App\Entity\User[]' %}
28 changes: 28 additions & 0 deletions tests/Rules/Comment/CommentType/CommentTypeRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Comment\CommentType;

use TwigCsFixer\Rules\Comment\CommentTypeRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class CommentTypeRuleTest extends AbstractRuleTestCase
{
public function testRule(): void
{
$this->checkRule(
[
new CommentTypeRule(),
],
[
// TODO: compute proper file lines/elements
'CommentType.Error:1:4' => 'Variable comment declaration must be used via types tag instead of comment.',
'CommentType.Error:2:4' => 'Variable comment declaration must be used via types tag instead of comment.',
'CommentType.Error:3:4' => 'Variable comment declaration must be used via types tag instead of comment.',
'CommentType.Error:4:4' => 'Variable comment declaration must be used via types tag instead of comment.',
'CommentType.Error:5:4' => 'Variable comment declaration must be used via types tag instead of comment.',
]
);
}
}
5 changes: 5 additions & 0 deletions tests/Rules/Comment/CommentType/CommentTypeRuleTest.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{# @var foo_bar boolean #}
{# @var bar_baz integer|null #}
{# @var admin \App\Entity\Admin #}
{# @var user \App\Entity\User|null #}
{# @var users \App\Entity\User[] #}
Loading