Skip to content
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
merged 10 commits into from
Jan 28, 2024
Merged
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
4 changes: 0 additions & 4 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
'no_useless_else' => true,
'nullable_type_declaration_for_default_null_value' => true,
'operator_linebreak' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],
'php_unit_test_case_static_method_calls' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_no_empty_return' => true,
Expand Down
78 changes: 78 additions & 0 deletions src/Rules/Function/IncludeFunctionRule.php
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();
}
}
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 tests/Rules/Function/IncludeFunction/IncludeFunctionRuleTest.php
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 tests/Rules/Function/IncludeFunction/IncludeFunctionRuleTest.twig
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 %}
Copy link
Owner

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...

Copy link
Owner

Choose a reason for hiding this comment

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

Solved in #178

Copy link
Contributor Author

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 :(

{% 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' -%}
Loading