Skip to content

[PropertyInfo] Add PropertyAttributesExtractor #57601

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

Open
wants to merge 8 commits into
base: 7.4
Choose a base branch
from

Conversation

alyamovsky
Copy link

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues no
License MIT

While the PropertyInfo component works great for extracting property annotations, in its current state it does not support extracting attributes. This means there's a necessity to use ReflectionClass on the target class to extract attributes, and that it's not possible to leverage PropertyInfoCacheExtractor to cache the results.

This PR adds a separate PropertyAttributesExtractorInterface interface that does just that, which is implemented by the existing ReflectionExtractor class.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.2 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@alyamovsky alyamovsky force-pushed the property-info-attributes branch from 9562951 to e0199a8 Compare June 29, 2024 17:06
@alyamovsky alyamovsky force-pushed the property-info-attributes branch from e55b199 to 20631f8 Compare July 1, 2024 20:50
@alyamovsky alyamovsky force-pushed the property-info-attributes branch from 20631f8 to ceafe5d Compare July 12, 2024 23:32
@OskarStark OskarStark changed the title [PropertyInfo] Add property attributes extractor [PropertyInfo] Add PropertyAttributesExtractor Jul 13, 2024
@alyamovsky
Copy link
Author

@OskarStark Hi, thanks for the review! Since this is my first PR, can you point out any potential issues that might be preventing feedback, or is it just the high number of PRs for the maintainers? The tests are failing, but it seems unrelated to my changes.

Comment on lines +23 to +24
* - name: The fully-qualified class name of the attribute
* - arguments: An associative array of attribute arguments if present
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use @param annotation to describe these parameters

Copy link
Author

Choose a reason for hiding this comment

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

Since the @param annotation is used for describing method's arguments, are you suggesting using it for the string $class, string $property, array $context = [] arguments?

* - name: The fully-qualified class name of the attribute
* - arguments: An associative array of attribute arguments if present
*
* @return array<int, array{name: string, arguments: array<array-key, mixed>}>|null
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @return array<int, array{name: string, arguments: array<array-key, mixed>}>|null
* @return list<array{name: string, arguments: array<array-key, mixed>}>|null

Comment on lines +61 to +67
public function getAttributes($class, $property, array $context = []): ?array
{
$this->assertIsString($class);
$this->assertIsString($property);

return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function getAttributes($class, $property, array $context = []): ?array
{
$this->assertIsString($class);
$this->assertIsString($property);
return null;
}
public function getAttributes(string $class, string $property, array $context = []): ?array
{
return null;
}

I think that is enough, thanks to modern PHP typing system.

@@ -45,6 +47,7 @@
->tag('property_info.type_extractor', ['priority' => -1002])
->tag('property_info.access_extractor', ['priority' => -1000])
->tag('property_info.initializable_extractor', ['priority' => -1000])
->tag('property_info.attributes_extractor', ['priority' => -1000])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
->tag('property_info.attributes_extractor', ['priority' => -1000])
->tag('property_info.attribute_extractor', ['priority' => -1000])

To be consistent? (same in FrameworkExtension and PropertyInfoPass)

/**
* @author Andrew Alyamovsky <andrew.alyamovsky@gmail.com>
*/
interface PropertyAttributesExtractorInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
interface PropertyAttributesExtractorInterface
interface PropertyAttributeExtractorInterface

To be consistent with other extractors. For example, even if getTypes is plural, PropertyTypeExtractorInterface is singular.

Comment on lines +525 to +532
foreach ($reflProperty->getAttributes() as $attribute) {
$attributes[] = [
'name' => $attribute->getName(),
'arguments' => $attribute->getArguments(),
];
}

return $attributes;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
foreach ($reflProperty->getAttributes() as $attribute) {
$attributes[] = [
'name' => $attribute->getName(),
'arguments' => $attribute->getArguments(),
];
}
return $attributes;
return $reflProperty->getAttributes();

Why not returning attributes themselves? So that we can have all the attribute data (repeated, etc) and use the newInstance method.

If it was because you want to decouple getAttributes from \ReflectionXXX, I think that we shouldn't try to decouple as it's part of the PHP language itself.

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants