PHPackages                             bulton-fr/annotation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. bulton-fr/annotation

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

bulton-fr/annotation
====================

Annotation parser system

1.0.x-dev(6y ago)010[2 issues](https://github.com/bulton-fr/annotation/issues)LGPL-3.0PHPPHP ^7.2.0

Since Jun 15Pushed 6y agoCompare

[ Source](https://github.com/bulton-fr/annotation)[ Packagist](https://packagist.org/packages/bulton-fr/annotation)[ Docs](https://github.com/bulton-fr/annotation)[ RSS](/packages/bulton-fr-annotation/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (4)Versions (3)Used By (0)

Annotations reader system
=========================

[](#annotations-reader-system)

[![Build Status](https://camo.githubusercontent.com/5b43c2e577f4a922e80eaeda0de96e1fdb847c3cdd2a8ef7589f6defd134a616/68747470733a2f2f7472617669732d63692e6f72672f62756c746f6e2d66722f616e6e6f746174696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bulton-fr/annotation) [![Code Coverage](https://camo.githubusercontent.com/90fea36e9d9a8c556fa4fa226054d8cd51dce9bb2493bf4570904b84ca54b8d5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62756c746f6e2d66722f616e6e6f746174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/bulton-fr/annotation/?branch=master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/93f84b661daf7d50835a66ed84c906bfc1f34b0f4c5d3a0c78cece8333a9c714/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62756c746f6e2d66722f616e6e6f746174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/bulton-fr/annotation/?branch=master)

[![Latest Stable Version](https://camo.githubusercontent.com/6ade62e3db78cf89fa9c4fd28474a4e25f317870bc4bfe0bf8df4d90a3cd0c22/68747470733a2f2f706f7365722e707567782e6f72672f62756c746f6e2d66722f616e6e6f746174696f6e2f762f737461626c652e737667)](https://packagist.org/packages/bulton-fr/annotation) [![Latest Unstable Version](https://camo.githubusercontent.com/d91ff59e413c5c0161fa3c10c9d21c57f9bef862a0e592fc81d6982f586f3f57/68747470733a2f2f706f7365722e707567782e6f72672f62756c746f6e2d66722f616e6e6f746174696f6e2f762f756e737461626c652e737667)](https://packagist.org/packages/bulton-fr/annotation) [![License](https://camo.githubusercontent.com/6abbaf33a5b2bf0df3a6bee7e27b7b1b75a3dc2988f254225ff6768e949bb92d/68747470733a2f2f706f7365722e707567782e6f72672f62756c746f6e2d66722f616e6e6f746174696f6e2f6c6963656e73652e737667)](https://packagist.org/packages/bulton-fr/annotation)

Install it
----------

[](#install-it)

With composer `composer require bulton-fr/annotation`.

Use it
------

[](#use-it)

First, the system need to parse your class :

```
$annotations = new \BultonFr\Annotation\Reader(myClass::class);
$annotations->parse();
```

And to obtain all annotations find, you have methods :

- `public function obtainClassAnnotList(): array`
- `public function obtainMethodsList(): Parser\AbstractManyParser`
- `public function obtainPropertiesList(): Parsers\AbstractManyParser`
- `public function obtainMethodAnnotList(string $methodName): array`
- `public function obtainPropertyAnnotList(string $propertyName): array`

The class `Parser\AbstractManyParser` implement the class `Iterator`, so you use a `foreach` on it. Each item will be an instance of `Parser\AbstractParser`.
To obtain the array which contain all annotation (like the array returned by `obtain*AnnotList`) from the `Parser\AbstractParser` class, you should use the method `getAnnotList()`.

The array returned by `obtain*AnnotList` has the format : `Annotations\AbstractAnnotation[][]`.

So for example :

```
/**
 * @Table(name="writing_list")
 * @Security(role="admin", fct="mySecurityCheck")
 * @AddEntity(ns="\BultonFr\Annotation\Test\Functional\Ref\Account")
 * @AddEntity(
 *  ns="\BultonFr\Annotation\Test\Functional\Ref\Category",
 *  alias="Ref\Category"
 * )
 */
```

The annotations list will have the format :

```
array(3) {
    "Table" => array(1) {
        0 => object \BultonFr\Annotation\Test\Functional\Annotations\Table
    },
    "Security" => array(1) {
        0 => object \BultonFr\Annotation\Test\Functional\Annotations\Security
    },
    "AddEntity" => array(2) {
        0 => object \BultonFr\Annotation\Test\Functional\Annotations\AddEntity,
        1 => object \BultonFr\Annotation\Test\Functional\Annotations\AddEntity,
    }
}

```

### All annotations objects

[](#all-annotations-objects)

When an annotation is found by the parser, an instance of `Parsers\Annotations\Info` is instanced, which will contain all info about the annotation. There are:

- the "name" (the part just after `@`)
- the value (the part after the name, trimed).
    - the property `valueStr` contain the string value (like into the docblock), without line-break.
    - the property `values` is an array of all values.
        if the valueStr contain attribute/value format, the attribute will be the array key; else the array key will be numeric.

With info into this object, a new object is instanced. When an annotation is declared, a class name is present. This class will be instanced each time this annotation is found.
It's this object (not the Info object) you will obtain by methods `obtain*AnnotList`.

The class used for the annotation must extends `Annotations\AbstractAnnotation`. Into it, you have access to the Reader, the Info object, you can detect if a key has been declared and obtain the value for a key.

You can find many example how to create a new class dedicated to an annotation :

- the class `/src/Annotations/AddNS`
- all classes in `/test/Functional/Annotations/`

### Import namespace

[](#import-namespace)

Many annotations systems use classes declared for the class (with php `use` keyword). I make the choice not to use it, mainly to not read the disk each time. So all annotation you will use need to be "imported".
To import an annotation, two choice :

- Declare it on the parser
- Use the annotation `@AddNS`

To declare a new annotation, you need two things :

- The complete class name which will be instanced each time this annotation is found.
- The alias (the part after `@`) to use (by default the class name)

#### Declare the namespace on the parser

[](#declare-the-namespace-on-the-parser)

To do that, we use the method `addImportedNS` on the `ParserManager`.

```
/**
 * Add a new imported namespace (obtain by AddNS or manually)
 *
 * @param string $name The class path (with ns) of the class which will be
 *  instancied when the annotation defined by $alias will be found.
 * @param string $alias The annotation key
 *
 * @return void
 */
public function addImportedNS(string $name, string $alias)
```

```
$annotations = new \BultonFr\Annotation\Reader(myClass::class);
$annotations->getParserManager()->addImportedNS(
    \myApp\myCustomAnnotation::class
    'CustomAnnot'
);
$annotations->parse();
```

Now, into the myClass (annotation in class, methods and properties docblocks), I can use the annotation `@CustomAnnot`.

#### With the annotation `AddNS`

[](#with-the-annotation-addns)

This annotation can be added only on the class docblock. If this annotation is present on properties or methods, they will be no effect.

```
/**
 * My class description
 *
 * @AddNS(ns="\myApp\myCustomAnnotation", alias="CustomAnnot")
 * @CustomAnnot(...)
 */
class myClass
```

Of course, you can use the imported annotation directly. When the system have all `Info` object, it read all AddNS first, and after it read all other annotations (so the order in docblock is not important).

### Example

[](#example)

```
$annotReader = new \BultonFr\Annotation\Reader(
    \BultonFr\Annotation\Test\Functional\Ref\Account
);

$annotReader
    ->getParserManager()
        ->addImportedNS('\BultonFr\Annotation\Test\Functional\Annotations\Column')
        ->addImportedNS('\BultonFr\Annotation\Test\Functional\Annotations\Route')
;

$annotReader->parse();

//string "int"
var_dump($annotReader->obtainPropertyAnnotList('id')['Column'][0]->getType());
```

Ignore an annotation
--------------------

[](#ignore-an-annotation)

Some annotations are ignored by the system. For example, all annotations used by docblock like `@param` etc. The list of ignored annotation is in `Parsers\Annotations\Reader::ignoredAnnotations`.
The list is based on the "tag reference" in the [phpDocumentor documentation](https://docs.phpdoc.org/references/phpdoc/tags/index.html).
If you see missing annotation on the list, you can create an issue or a pull-request ;)

You can add a new annotation to ignore with `Parsers\Annotations\Reader::addIgnoredAnnotation`.

The property `ignoredAnnotations` and associated methods (getter and add) are static. So you need to declare it once for all instances.

```
/**
 * Add a new annotation to ignore
 *
 * @param string $annotationName The annotation's name to ignore
 *  It's the part after the @.
 *
 * @return void
 */
public static function addIgnoredAnnotation(string $annotationName)
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2521d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8878310e46e073ed70452f0f16ebb5bf177a484811e2b19b2f48d8e2035279a8?d=identicon)[bulton-fr](/maintainers/bulton-fr)

---

Top Contributors

[![bulton-fr](https://avatars.githubusercontent.com/u/2345417?v=4)](https://github.com/bulton-fr "bulton-fr (7 commits)")

### Embed Badge

![Health badge](/badges/bulton-fr-annotation/health.svg)

```
[![Health](https://phpackages.com/badges/bulton-fr-annotation/health.svg)](https://phpackages.com/packages/bulton-fr-annotation)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M283](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M344](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
