-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Use namespaced Twig #22962
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
Use namespaced Twig #22962
Conversation
nicolas-grekas
commented
May 30, 2017
Q | A |
---|---|
Branch? | 2.7 |
Bug fix? | yes |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | needs twigphp/Twig#2490 and twigphp/Twig#2491 |
Fixed tickets | - |
License | MIT |
Doc PR | - |
use Twig\Markup; | ||
use Twig\Profiler\Dumper\HtmlDumper; | ||
use Twig\Profiler\Profile; | ||
use Twig\Source; |
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.
@keradus just wondering: this use
is not used in the class - is it a php-cs-fixer bug that fabbot is still green?
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.
The NoUnusedImportsFixer
is quite permissive (not removing an unsed import in better than removing an used one) particularly it check if the class in not referenced in a comment.
In this case it think it's the "source" word in the license header comment that make the fixer think that the class is still used.
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.
that's exactly the case here.
Imported class name could be used in comment, even in different casing, which later is utilized by Doctrine or some annotation-parsers. that behaviour was added after fixer broke some code.
I would not focus here is it header comment or other comment actually.
One could bring configuration to fixer to ignore comments or non-docblock comments / be strict about casing !
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.
Is there a way to make it more strict so that I can cleanup locally?
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.
@nicolas-grekas Not currently, see PHP-CS-Fixer/PHP-CS-Fixer#2814.
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.
go ahead @nicolas-grekas and contribute ;)
*/ | ||
public function setTwigEnvironment(\Twig_Environment $twig) | ||
public function setTwigEnvironment(Environment $twig) |
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.
Reading this, I would rename Twig\Environment
to Twig\Twig
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.
Is it possible to name it just Twig
to avoid the Twig\Twig
repetition?
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.
Yep but you will have to import Twig\Twig anyway
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.
We cannot use Twig
only as all classes need to be under a namespace. That's PSR-4.
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.
@javiereguiluz PSR-4 doesn't allow top-level classes. In §2.2.1, "The fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”.
http://www.php-fig.org/psr/psr-4/#specification
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.
renamed to Twig\Twig
6a2452b
to
4cfb449
Compare
Just fixed a few potential BC breaks related to the use of aliases type hints. |
@@ -165,7 +161,7 @@ private function getMetadata($type, $entity) | ||
return false; | ||
} | ||
|
||
return !$param->getClass() || $param->getClass()->getName() !== 'Twig_Environment'; | ||
return !$param->getClass() || !in_array($param->getClass()->getName(), array('Twig_Environment', 'Twig\Twig')); |
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.
Using class_alias
, the ReflectionClass
refers to the original class. Checking against Twig\Twig
is not necessary.
https://3v4l.org/QH2KQ
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.
True, until Twig moves to namespaces-first, which is why this code is here.
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.
Actually, this logic could be improved to avoid relying on typehints or param names (btw, the check on param name is wrong, as the name is not relevant). I will send a PR.
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.
here you go: #22994
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.
@nicolas-grekas please revert this change to avoid useless conflicts with my PR removing this code entirely 😄
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.
reverted, thanks
* | ||
* @return array An array with the contexts the URL is safe | ||
* | ||
* To be made @final in 3.4, and the type-hint be changed to "\Twig\Node\Node" in 4.0. |
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.
Wouldn't this trigger the warning already ? Or does DebugClassLoader check for beginning of lines ?
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.
* | ||
* @return array An array with the contexts the URL is safe | ||
* | ||
* To be made @final in 3.4, and the type-hint be changed to "\Twig\Node\Node" in 4.0. | ||
*/ | ||
public function isUrlGenerationSafe(\Twig_Node $argsNode) |
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.
Can't we already change the typehint here (with the same class_exists
call) ?
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.
We can, but that means loading Node, which in turn loads Compiler.
I preferred not, thus the current code.
223b126
to
029377d
Compare
now green |
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.
You just need to revert the Twig\Environment
=> Twig\Twig
change. Sorry about that :)
renaming done, now green |
This PR was merged into the 2.7 branch. Discussion ---------- Use namespaced Twig | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | needs twigphp/Twig#2490 and twigphp/Twig#2491 | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- c3d1262 Use namespaced Twig