Giter Club home page Giter Club logo

koriym.attributes's Introduction

Koriym.Attributes

codecov Type Coverage Continuous Integration Static Analysis Coding Standards

A koriym/attributes dual reader implements doctrine/annotation Reader interface in order to read both doctrine/annotation and PHP 8 attributes.

Doctrine annotations are different by design than PHP core one. Not all attributes can be read by this reader (ex. parameters), and not all doctrine/annotations can be read by this reader either. (ex. nested annotations)

However, This reader help you to code forward compatible that supports both PHP 7.x annotations and 8.x attributes in certain senario.

Installation

composer require koriym/attributes

Usage

Create the reader instance.

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Koriym\Attributes\DualReader;
use Koriym\Attributes\AttributeReader;

$reader = new DualReader(
    new AnnotationReader(),
    new AttributeReader()
);
assert($reader instanceof Reader);

The reader can read both annotations and attributes.

Compatible Annotation

Existing doctrine annotations can be changed into annotations that work for both doctrine annotation and PHP8 attributes.

Add #[Attribute] attribute.

use Attribute;

/** @Annotation */
+#[Attribute]
final class Foo
{
}

Then add constructor when annotation has properties. Following example works with both PHP8 attribute and doctrine/annotations.

use Attribute;
+use Doctrine\Common\Annotations\NamedArgumentConstructor;

/**
 * @Annotation 
 * @Target("METHOD")
+* @NamedArgumentConstructor
 */
+#[Attribute(Attribute::TARGET_METHOD)]
final class Foo
{
    public string $bar;
    public int $baz;
+    public function __construct(string $bar = '', int $baz = 0)
+    {
+        $this->bar = $bar;
+        $this->baz = $baz;
+    }
}

See more about annotation compatible attribute at Constructors with Named Parameters

koriym.attributes's People

Contributors

koriym avatar tomasvotruba avatar naokitsuchiya avatar oskarstark avatar kenjis avatar

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.