diff --git a/src/Rules/Comment/CommentTypeRule.php b/src/Rules/Comment/CommentTypeRule.php new file mode 100644 index 00000000..66a716d6 --- /dev/null +++ b/src/Rules/Comment/CommentTypeRule.php @@ -0,0 +1,78 @@ +isTokenMatching($token, Token::COMMENT_START_TYPE, '@var')) { + return; + } + + // is warning the proper violation "level" here? + $fixer = $this->addFixableWarning( + 'Variable comment declaration must be used via types tag instead of comment.', + $token + ); + if (null === $fixer) { + return; + } + + $tokenValue = $token->getValue(); + var_dump($tokenValue); + + $firstTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition); + var_dump($firstTextType); + $secondTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition); + var_dump($secondTextType); + $thirdTextType = $this->findNext(Token::COMMENT_TEXT_TYPE, $tokens, $tokenPosition); + 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) + ); + } +} diff --git a/tests/Rules/Comment/CommentType/CommentTypeRuleTest.fixed.twig b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.fixed.twig new file mode 100644 index 00000000..512988e2 --- /dev/null +++ b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.fixed.twig @@ -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[]' %} diff --git a/tests/Rules/Comment/CommentType/CommentTypeRuleTest.php b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.php new file mode 100644 index 00000000..55d2d3f8 --- /dev/null +++ b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.php @@ -0,0 +1,28 @@ +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.', + ] + ); + } +} diff --git a/tests/Rules/Comment/CommentType/CommentTypeRuleTest.twig b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.twig new file mode 100644 index 00000000..756e489c --- /dev/null +++ b/tests/Rules/Comment/CommentType/CommentTypeRuleTest.twig @@ -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[] #}