-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Rule IncludeFunction #176
Merged
VincentLanglet
merged 10 commits into
VincentLanglet:main
from
smnandre:feat/include-function
Jan 28, 2024
+143
−4
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6197277
Remove 'ordered_imports' rule from .php-cs-fixer.php
smnandre 5d510b5
Add Rule IncludeFunction
smnandre 7863f68
Update src/Rules/Function/IncludeFunctionRule.php
smnandre 28755a2
Check fixer sooner
smnandre e464b3a
Check fixer sooner
smnandre 36ad44b
Fix second assert message
smnandre 72007b7
PR feedback
smnandre 0151751
CS
smnandre dbc9f4d
Remove name attributes
smnandre 95a9b13
Remove 100% useless ternary
smnandre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\Function; | ||
|
||
use TwigCsFixer\Rules\AbstractFixableRule; | ||
use TwigCsFixer\Token\Token; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Ensures that include function is used instead of function tag. | ||
*/ | ||
final class IncludeFunctionRule extends AbstractFixableRule | ||
{ | ||
protected function process(int $tokenPosition, array $tokens): void | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if (!$this->isTokenMatching($token, Token::BLOCK_NAME_TYPE, 'include')) { | ||
return; | ||
} | ||
|
||
$fixer = $this->addFixableError( | ||
'Include function must be used instead of include tag.', | ||
$token | ||
); | ||
|
||
if (null === $fixer) { | ||
return; | ||
} | ||
|
||
$openingTag = $this->findPrevious(Token::BLOCK_START_TYPE, $tokens, $tokenPosition); | ||
Assert::notFalse($openingTag, 'Opening tag cannot be null.'); | ||
|
||
$closingTag = $this->findNext(Token::BLOCK_END_TYPE, $tokens, $tokenPosition); | ||
Assert::notFalse($closingTag, 'Closing tag cannot be null.'); | ||
|
||
$fixer->beginChangeSet(); | ||
|
||
// Replace opening tag (and keep eventual whitespace modifiers) | ||
$fixer->replaceToken($openingTag, str_replace('{%', '{{', $tokens[$openingTag]->getValue())); | ||
$fixer->replaceToken($tokenPosition, 'include('); | ||
$ignoreMissing = false; | ||
$withoutContext = false; | ||
foreach (range($tokenPosition, $closingTag - 1) as $position) { | ||
$token = $tokens[$position]; | ||
if (Token::NAME_TYPE === $token->getType()) { | ||
switch ($token->getValue()) { | ||
case 'with': | ||
$fixer->replaceToken($position, ','); | ||
break; | ||
case 'only': | ||
$withoutContext = true; | ||
$fixer->replaceToken($position, ''); | ||
break; | ||
case 'ignore': | ||
$ignoreMissing = true; | ||
$fixer->replaceToken($position, ''); | ||
break; | ||
case 'missing': | ||
$fixer->replaceToken($position, ''); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
$endInclude = ') '.str_replace('%}', '}}', $tokens[$closingTag]->getValue()); | ||
if ($ignoreMissing) { | ||
$endInclude = ', true'.$endInclude; | ||
} | ||
if ($ignoreMissing || $withoutContext) { | ||
$endInclude = sprintf(', %s', $withoutContext ? 'false' : 'true').$endInclude; | ||
} | ||
$fixer->replaceToken($closingTag, $endInclude); | ||
$fixer->endChangeSet(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/Rules/Function/IncludeFunction/IncludeFunctionRuleTest.fixed.twig
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,16 @@ | ||
{{ include( 'template.html' ) }} | ||
{{ include( ['template_a.html', 'template_b.html'] , {'foo': 'bar'} , false, true) }} | ||
{{ include( 'template.html' , false) }} | ||
{{ include( 'template.html' , vars ) }} | ||
{{ include( 'template.html' , vars , false) }} | ||
{{ include( 'template.html' , {'foo': 'bar'} ) }} | ||
{{ include( 'template.html' , {'foo': 'bar'} , false) }} | ||
{{ include( some_var ) }} | ||
{{ include( ajax ? 'ajax.html' : 'not_ajax.html' ) }} | ||
{{ include( 'template.html' , true, true) }} | ||
{{ include( 'template.html' , {'foo': 'bar'} , true, true) }} | ||
{{ include( 'template.html' , false, true) }} | ||
{{ include( ['template_a.html', 'template_b.html'] ) }} | ||
{{ include( ['template_a.html', 'template_b.html'] , false) }} | ||
{{- include( 'template.html' ) ~}} | ||
{{~ include( 'template.html' ) -}} |
33 changes: 33 additions & 0 deletions
33
tests/Rules/Function/IncludeFunction/IncludeFunctionRuleTest.php
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rules\Function\IncludeFunction; | ||
|
||
use TwigCsFixer\Rules\Function\IncludeFunctionRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
final class IncludeFunctionRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testRule(): void | ||
{ | ||
$this->checkRule(new IncludeFunctionRule(), [ | ||
'IncludeFunction.Error:1:4', | ||
'IncludeFunction.Error:2:4', | ||
'IncludeFunction.Error:3:4', | ||
'IncludeFunction.Error:4:4', | ||
'IncludeFunction.Error:5:4', | ||
'IncludeFunction.Error:6:4', | ||
'IncludeFunction.Error:7:4', | ||
'IncludeFunction.Error:8:4', | ||
'IncludeFunction.Error:9:4', | ||
'IncludeFunction.Error:10:4', | ||
'IncludeFunction.Error:11:4', | ||
'IncludeFunction.Error:12:4', | ||
'IncludeFunction.Error:13:4', | ||
'IncludeFunction.Error:14:4', | ||
'IncludeFunction.Error:15:5', | ||
'IncludeFunction.Error:16:6', | ||
]); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/Rules/Function/IncludeFunction/IncludeFunctionRuleTest.twig
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,16 @@ | ||
{% include 'template.html' %} | ||
{% include ['template_a.html', 'template_b.html'] ignore missing with {'foo': 'bar'} only %} | ||
{% include 'template.html' only %} | ||
{% include 'template.html' with vars %} | ||
{% include 'template.html' with vars only %} | ||
{% include 'template.html' with {'foo': 'bar'} %} | ||
{% include 'template.html' with {'foo': 'bar'} only %} | ||
{% include some_var %} | ||
{% include ajax ? 'ajax.html' : 'not_ajax.html' %} | ||
{% include 'template.html' ignore missing %} | ||
{% include 'template.html' ignore missing with {'foo': 'bar'} %} | ||
{% include 'template.html' ignore missing only %} | ||
{% include ['template_a.html', 'template_b.html'] %} | ||
{% include ['template_a.html', 'template_b.html'] only %} | ||
{%- include 'template.html' ~%} | ||
{%~ include 'template.html' -%} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smnandre This is fixed in
{{ include( 'template.html' , false) }}
.I think it's wrong, and should be
{{ include( 'template.html', [], false) }}
Not sure if it's easy without named parameters...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved in #178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Damn really sorry to let so many mistakes :(