-
Notifications
You must be signed in to change notification settings - Fork 235
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
Handle annotation default property #396
Comments
I stand for the second option, but with the name /**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class Foo implements NamedArgumentConstructorAnnotation
{
/**
* The field name.
*
* @Default
* @Required
*
* @var string
*/
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
} |
So, I did a bit of research, and here is a summary of how annotations handle the default property. If annotation doesn't have a constructorThe default property is the first declared public property Given:
With:
With:
If annotation has a constructorIf It doesn't implement NamedArgumentsConstructorThe values are passed as an array in the first argument of the constructor
With:
With:
With:
If It implements NamedArgumentsConstructorIf PHP >= 8 - Constructor is called directly with the array of values Given:
With:
With:
So we have two different ways to handle the problem.
With:
let me know what you think! |
This would be my personal favorite for the same reason you mentioned.
That's a bummer. I don't know if this kind of BC break is acceptable. What we could do is: We keep the current behavior when the marker interface is used and implement your suggestion for the At the same time, we deprecate the marker interface. That would create a nice migration path.
If we neither want to introduce a BC break nor create a different behavior for the annotation than for the marker interface, this option would be acceptable for me. |
I agree with you both, and am glad I hold back from releasing right after merging #391 😄 |
Fixed in #402 |
It would be great to be able to define the default property for annotation. At the moment, it always target the property named
value
. The ability to change this behaviour could lead to more explicit variables names and a better compatibility with PHP8 attributes.See the following example.
The supported syntax are:
but this one is not supported as it expects a
value
property.Note that this is already handled in the Doc Parser for annotation like
@Annotation\Attribute
, as the default property in this case isname
.We could support the attribute
default_property
in many ways:@Annotation
annotation, like@Annotation(defaultProperty="name")
. But as explained here Introduced annotation NamedArgumentConstructor #391 , this annotation is kind of special as it is detected throughstr_pos
.@DefaultProperty
, like@DefaultPropety("name")
NamedArgumentConstructorAnnotation
only.Another path would be to force the DocParser to respect argument constructor arguments order if there is a mix of named / not named arguments.
The text was updated successfully, but these errors were encountered: