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

Conversation

smnandre
Copy link
Contributor

Attempt to implement #140

Open to any feedback :)

Now defined the same way in @symfony RuleSet (https://cs.symfony.com/doc/rules/import/ordered_imports.html)

```
@symfony with config:

['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha']
```

```diff
-    'ordered_imports'                                  => [
-        'sort_algorithm' => 'alpha',
-        'imports_order'  => ['class', 'function', 'const'],
-    ],
```
@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b850eca) 100.00% compared to head (95a9b13) 100.00%.
Report is 3 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##                main      #176   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       566       586   +20     
===========================================
  Files             51        53    +2     
  Lines           1741      1805   +64     
===========================================
+ Hits            1741      1805   +64     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 70 to 75
if ($withoutContext) {
$endInclude = ', with_context = false'.$endInclude;
}
if ($ignoreMissing) {
$endInclude = ', ignore_missing = true'.$endInclude;
}
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure about named parameters. I didn't see them often used in twig.

So I wonder if we shouldn't try to have

include('template.html' , vars  , false)

rather than

include('template.html' , vars  , with_context = false)

and same for others options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest I have absolutely no idea wich of those two args (context / missing) is the first one / second one ..

Tried to follow "blindly" the doc : https://twig.symfony.com/doc/3.x/functions/include.html

I remove them and it can be the goal of another rule i guess later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh well.. we generate "include(..., false, true)" if there was only "ignore_missing" ? :|

function twig_include(Environment $env, $context, $template, $variables = [], $withContext = true, $ignoreMissing = false, $sandboxed = false)
{

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I had in mind to generate

Include ($template, [], false, true)

If the only option passed was "ignore missing".

I agree that "false" is unnecessary passed but I feel like people are not used to named parameter yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done !

}
$endInclude = ') '.str_replace('%}', '}}', $tokens[$closingTag]->getValue());
if ($ignoreMissing) {
$endInclude = sprintf(', %s', $ignoreMissing ? 'true' : 'false').$endInclude;
Copy link
Owner

Choose a reason for hiding this comment

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

The ternary condition here is always true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😶‍🌫️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, sorry

@VincentLanglet VincentLanglet merged commit 8636c7d into VincentLanglet:main Jan 28, 2024
14 checks passed
@VincentLanglet
Copy link
Owner

Thanks !

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

@94noni
Copy link
Contributor

94noni commented Jan 28, 2024

Waw nice one @smnandre and @VincentLanglet 👌🏼

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.

4 participants