Add AttributeGenerator and class-level attribute support - #218
Open
armenio wants to merge 1 commit into
Open
Conversation
Signed-off-by: Rafael Armenio <rafael.armenio@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Laminas\Code\Generatorcurrently has no way to emit PHP attributes, so any class generatedthrough this library loses them. This is a long-standing gap: attributes have been part of the
language since PHP 8.0, and consumers such as proxy generators and ORM/DI tooling need to emit
them. See #144, and the earlier attempt in #145.
This PR adds
Laminas\Code\Generator\AttributeGeneratorplus attribute support onClassGenerator(and, by inheritance,InterfaceGeneratorandTraitGenerator). It targets4.18.xas a new feature, and is fully backwards compatible: no existing behaviour changes, andno existing output changes.
How the code is used
Attributes on existing symbols can be read back through reflection:
Public API
AttributeGenerator::fromName(string $name, array $arguments = []): selfAttributeGenerator::fromReflectionAttribute(ReflectionAttribute $attribute): selfAttributeGenerator::fromReflector(ReflectionClass|ReflectionClassConstant|ReflectionFunction|ReflectionMethod|ReflectionParameter|ReflectionProperty $reflector): list<self>AttributeGenerator#generate(): string,#getName(),#getArguments()ClassGenerator#setAttributes(list<AttributeGenerator>),#addAttribute(),#getAttributes()Everything else is
private. The class isfinal,@psalm-immutable, implementsGeneratorInterfaceonly, and is notStringable.Design notes
This is a fresh implementation rather than a rebase of #145, written against the design direction
requested there by @Ocramius: a single class with a thin
publicAPI and no separateassembler/builder/factory layer.
::fromArray(), per Deprecating all::fromArray()methods in in the package #153.AttributeBuilder/AttributeAssembler/AttributeAssemblerFactory/AttributePart.Since the assembler layer is gone, the open "should
AttributePartbecome an enum" question fromSupport for PHP 8.0 class attributes generation #145 no longer applies: there is no part type left to model.
#[\Foo\Bar]), so output is valid regardless of thesurrounding
usestatements. A leading\in the input is accepted and normalised away.ValueGenerator. Attributearguments are constant expressions with different rules than property defaults, and
ValueGeneratorrenders1.0as1, silently changing the argument's type. Supported values:null,bool,int,float,string,array(recursive, list and hash) and enum cases.Anything else throws
InvalidArgumentException.malformed argument names, and positional arguments following named ones are all rejected by
fromName().ClassGenerator::fromReflection()deliberately does not populate attributes. Doing so wouldsilently change the output for every existing consumer that generates classes from reflection —
proxy generators in particular. Attributes are opt-in via
AttributeGenerator::fromReflector().Scope
Class-level attributes only, matching the issue. Attributes on methods, properties, constants and
parameters are a natural follow-up and reuse
AttributeGeneratorunchanged — happy to add them ina separate PR once the shape of this one is agreed.
Documentation is included in
docs/book/generator/examples.md.composer cs-check,composer testandcomposer static-analysisall pass locally.Fixes #144