crell / attributeutils Goto Github PK
View Code? Open in Web Editor NEWUtilities to help ease parsing and manging of attributes
License: Other
Utilities to help ease parsing and manging of attributes
License: Other
I was evaluating this library as a potential building block of a new feature. I had spotted that it only supports PHP 8.1 at this moment, which I considered a warning sign for us - even though I like explicit PHP version support in packages - but I could live with this for now...
However when I used my attribute like #[Foo(Bar::BAZ->value)]
I immediately got a Enum field reference in constant expression is allowed only from PHP 8.2
warning from PHPStorm, which means we may need to leverage PHP 8.2 language features sooner than I expected, or I have to avoid usage constants and go with "magic strings" here and there.
So are there any plans for flagging PHP 8.2 (and PHP 8.3) support in this package and if yes, what is the expected timeline?
When using ParseMethods
together with an attribute implementing FromReflectionMethod
the fromReflection
method is called with a \ReflectionMethod
parameter that contains information about the declaring class but no information about the "bound" or "analysed" class.
I think it would be an enhancement to pass also information about the analysed class to the method attributes, as it is the class in the context of which the method is callable. This is also the behaviour that FromReflectionClass
has.
I am using attributes to mark methods to be traced by a tracing library, I need information about the object that is actually being instantiated as opposed to its class hierarchy.
Add an interface that takes the analysed class as a parameter similar to FromReflectionClass
not relevant
#[Attribute(Attribute::TARGET_CLASS)]
class classAttribute implements ParseMethods, Inheritable, FromReflectionClass
{
public function setMethods(array $methods): void
{
}
public function includeMethodsByDefault(): bool
{
return false;
}
public function methodAttribute(): string
{
return methodAttribute::class;
}
public function fromReflection(\ReflectionClass $subject): void
{
var_dump("REFLECTION CLASS");
var_dump($subject);
}
}
#[Attribute(Attribute::TARGET_METHOD)]
class methodAttribute implements FromReflectionMethod {
public function fromReflection(\ReflectionMethod $subject): void
{
var_dump("REFLECTION METHOD");
var_dump($subject);
}
}
#[classAttribute]
abstract class parentClass {
#[methodAttribute]
public function doSomething() {
return true;
}
}
class childClass extends parentClass {
}
$analyzer = new Analyzer();
$tracedClass = $analyzer->analyze(childClass::class, classAttribute::class);
// Outputs:
// string(16) "REFLECTION CLASS"
// object(ReflectionClass)#3671 (1) {
// ["name"]=>
// string(10) "childClass"
// }
// string(17) "REFLECTION METHOD"
// object(ReflectionMethod)#3711 (2) {
// ["name"]=>
// string(11) "doSomething"
// ["class"]=>
// string(11) "parentClass"
// }
While trying to use the library for managing attributes used within inheritance (a very useful feature I'd prefer not to implement myself) I discovered that the design choice of always returning an instance of the attribute makes it impossible to use inheritance for the attribute types themselves.
in my case I'm trying to build something that uses the following:
interface AttributeInterface {
public function isEnabled(): bool;
}
#[\Attribute]
class MyMethod implements AttributeInterface {
public function isEnabled(): bool
{
return true;
}
}
class Something {
#[MyMethod()]
public function myMethod(): bool
{
return true;
}
}
To handle this I'm currently using reflection with:
$reflectionClass->getAttributes(AttributeInterface::class, ReflectionAttribute::IS_INSTANCEOF);
But of course would prefer to use this library instead.
By using the attributes in this way I can create general handlers for certain kinds of attributes while allowing for custom implementations of the attributes.
A possible implementation would not return instances of the attribute class in the search but rather use ReflectionAttribute::IS_INSTANCEOF
to do the search and then return instances of the found attributes instead, and an empty list if none are found.
not relevant
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.