Coder Social home page Coder Social logo

attributeutils's People

Contributors

bwaidelich avatar crell avatar jdreesen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

attributeutils's Issues

PHP >8.1 support

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?

FromReflectionMethod binds the class declaring the method but gives no information about the class on which the method was invoked on

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.

Detailed description

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.

Context

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.

Possible implementation

Add an interface that takes the analysed class as a parameter similar to FromReflectionClass

Your environment

not relevant

To replicate:

#[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"
// }

No support for Attribute inheritance

Detailed description

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.

Context

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.

Possible implementation

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.

Your environment

not relevant

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.