-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] Improve error when autoconfiguring a class with both ExtensionInterface
and Twig callable attribute
#60415
New i 8000 ssue
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
Conversation
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php
Outdated
Show resolved
Hide resolved
a05c57b
to
83f0f07
Compare
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.
Thanks a lot!
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.
just minor cs stuff
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php
Outdated
Show resolved
Hide resolved
83f0f07
to
2abcc87
Compare
Fixed. |
$class = $reflector->getDeclaringClass(); | ||
if ($class->implementsInterface(ExtensionInterface::class)) { | ||
if ($class->isSubclassOf(AbstractExtension::class)) { | ||
throw new LogicException(\sprintf('The class "%s" cannot both extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name)); |
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.
throw new LogicException(\sprintf('The class "%s" cannot both extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name)); | |
throw new LogicException(\sprintf('The class "%s" cannot extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name)); |
if ($class->isSubclassOf(AbstractExtension::class)) { | ||
throw new LogicException(\sprintf('The class "%s" cannot both extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name)); | ||
} | ||
throw new LogicException(\sprintf('The class "%s" cannot both implement "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, ExtensionInterface::class, $attribute::class, $reflector->name)); |
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.
throw new LogicException(\sprintf('The class "%s" cannot both implement "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, ExtensionInterface::class, $attribute::class, $reflector->name)); | |
throw new LogicException(\sprintf('The class "%s" cannot implement "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, ExtensionInterface::class, $attribute::class, $reflector->name)); |
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.
suggested it in #60415 (comment) :) what's wrong with it?
use Twig\Extension\AttributeExtension; | ||
|
||
class AttributeExtensionTest extends TestCase | ||
{ | ||
public function testExtensionWithAttributes() | ||
/** @beforeClass */ |
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.
Please also add the PHPUnit attribute, to be compatible with PHPUnit 11
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.
Good idea, attribute added.
I've also seen the pre condition attributes, but none of them can check a composer package version or if a class exists: https://docs.phpunit.de/en/10.5/writing-tests-for-phpunit.html#skipping-tests-using-attributes
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.
Looks weird that they don't have a RequiresClass
attribute (and probably RequiresInterface
). It would look consistent with the other attributes they have.
…ensionInterface and Twig callable attribute
2abcc87
to
752b41d
Compare
Thank you @GromNaN. |
Just came here while researching an error on
Where class Anon
{
private int $anonTagIndex = 0;
public function __construct(
private readonly Client $client,
) {
}
#[AsTwigFilter(name: 'anon')]
public function anon(?string $string = '', string $type = ''): string
{
// snip
}
} Running BETA2 in production ;-) I cant replicate it locally in dev |
@PhilETaylor it looks like a cache issue. If you updated the class without rebuilding the container, this error is expected. On production, the cache is not automatically rebuilt when a file is modified. You should run |
prod is a docker container built from scratch each deploy, the build folder should be empty on deployment. I think my issue might be a shared redis cache... ignore me, I know Im at the bleeding edge here, and not looking for support, but hoping by testing the beta's to find real issues for you : ) |
Reported by Javier:
The ExtensionSet exception is thrown because the same an extension is registered twice:
twig.extension
tag autoconfigured because it implementsTwig\Extension\ExtensionInterface
AttributeExtension
is registered for this class because it as one of the extension attributes (since [TwigBundle] Enable#[AsTwigFilter]
,#[AsTwigFunction]
and#[AsTwigTest]
attributes to configure runtime extensions #52748)Previous exception when
twig
service is instanciated:New exception during container compilation:
Checking
AbstractExtension
makes the error message more specific to the code, as most extensions extend this class and doesn't implement the interface directly.