Issues (4)

AnnotationReader.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Annotations;
13
14
use mindplay\annotations\AnnotationCache;
15
use mindplay\annotations\AnnotationException;
16
use mindplay\annotations\Annotations;
17
use mindplay\annotations\IAnnotation;
18
use ReflectionClass;
19
use ReflectionMethod;
20
use ReflectionProperty;
21
22
/**
23
 * Classe permettant de lire les differentes annotations
24
 */
25
class AnnotationReader
26
{
27
    /**
28
     * La seule instance d'utilisation de la classe
29
     *
30
     * @var object
31
     */
32
    protected static $_instance;
33
34
    /**
35
     * Vérifie, instancie et renvoie la seule instance de la classe appelée.
36
     *
37
     * @return static
38
     */
39
    public static function instance()
40
    {
41
        if (! (static::$_instance instanceof static)) {
42
            $params            = func_get_args();
43
            static::$_instance = new static(...$params);
0 ignored issues
show
The call to BlitzPHP\Annotations\Ann...onReader::__construct() has too many arguments starting with $params. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            static::$_instance = /** @scrutinizer ignore-call */ new static(...$params);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
44
        }
45
46
        return static::$_instance;
47
    }
48
49
    /**
50
     * Constructeur
51
     */
52
    protected function __construct()
53
    {
54
        $cacheDir = rtrim(sys_get_temp_dir(), '/\\') . DIRECTORY_SEPARATOR . 'blitz-php' . DIRECTORY_SEPARATOR . 'annotations';
55
        if (! is_dir($cacheDir)) {
56
            mkdir($cacheDir, 0o777, true);
57
        }
58
        Annotations::$config['cache'] = new AnnotationCache($cacheDir);
59
        AnnotationPackager::register(Annotations::getManager());
60
    }
61
62
    /**
63
     * Inspecte les annotations appliquées à une classe donnée
64
     *
65
     * @param object|ReflectionClass|string $class Un nom de classe, un objet ou une instance de ReflectionClass
66
     * @param string                        $type  Un nom de classe/interface d'annotation facultatif - si spécifié, seules les annotations du type donné sont renvoyées.
67
     *                                             Alternativement, le préfixe avec "@" invoque la résolution de nom (vous permettant d'interroger par nom d'annotation.)
68
     *
69
     * @return Annotation[] Instances d'annotation
70
     *
71
     * @throws AnnotationException si un nom de classe donné n'est pas défini
72
     */
73
    public static function fromClass($class, ?string $type = null)
74
    {
75
        return self::instance()->ofClass($class, $type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance()->ofClass($class, $type) returns the type mindplay\annotations\Annotation[] which is incompatible with the documented return type BlitzPHP\Annotations\Annotation[].
Loading history...
76
    }
77
78
    /**
79
     * Inspecte les annotations appliquées à une méthode donnée
80
     *
81
     * @param object|ReflectionClass|ReflectionMethod|string $class  Un nom de classe, un objet, une ReflectionClass ou une instance de ReflectionMethod
82
     * @param string|null                                    $method Le nom d'une méthode de la classe donnée (ou null, si le premier paramètre est une ReflectionMethod)
83
     * @param string                                         $type   Un nom facultatif de classe/d'interface d'annotation - si spécifié, seules les annotations du type donné sont renvoyées.
84
     *                                                               Alternativement, le préfixe avec "@" invoque la résolution de nom (vous permettant d'interroger par nom d'annotation.)
85
     *
86
     * @return IAnnotation[] liste des objets Annotation
87
     *
88
     * @throws AnnotationException pour une méthode ou un nom de classe non défini
89
     */
90
    public static function fromMethod($class, ?string $method, ?string $type = null)
91
    {
92
        return self::instance()->ofMethod($class, $method, $type);
93
    }
94
95
    /**
96
     * Inspecte les annotations appliquées à une propriété donnée
97
     *
98
     * @param object|ReflectionClass|ReflectionProperty|string $class    Un nom de classe, un objet, une ReflectionClass ou une instance de ReflectionProperty
99
     * @param string|null                                      $property Le nom d'une propriété définie de la classe donnée (ou null, si le premier paramètre est une ReflectionProperty)
100
     * @param string                                           $type     Un nom de classe/interface d'annotation facultatif - si spécifié, seules les annotations du type donné sont renvoyées.
101
     *                                                                   Alternativement, le préfixe avec "@" invoque la résolution de nom (vous permettant d'interroger par nom d'annotation.)
102
     *
103
     * @return IAnnotation[] liste des objets Annotation
104
     *
105
     * @throws AnnotationException pour un nom de classe non défini
106
     */
107
    public static function formProperty($class, ?string $property, ?string $type = null)
108
    {
109
        return self::instance()->ofProperty($class, $property, $type);
110
    }
111
112
    /**
113
     * Inspecte les annotations appliquées à une classe donnée
114
     *
115
     * @see self::fromClass()
116
     *
117
     * @param mixed      $class
118
     * @param mixed|null $type
119
     */
120
    private function ofClass($class, $type = null)
121
    {
122
        return Annotations::ofClass($class, $type);
123
    }
124
125
    /**
126
     * Inspecte les annotations appliquées à une méthode donnée
127
     *
128
     * @see self::fromMethod()
129
     *
130
     * @param mixed      $class
131
     * @param mixed|null $type
132
     */
133
    private function ofMethod($class, ?string $method, $type = null)
134
    {
135
        return Annotations::ofMethod($class, $method, $type);
136
    }
137
138
    /**
139
     * Inspecte les annotations appliquées à une proprieté donnée
140
     *
141
     * @see self::fromProperty()
142
     *
143
     * @param mixed      $class
144
     * @param mixed|null $type
145
     */
146
    private function ofProperty($class, ?string $property, $type = null)
147
    {
148
        return Annotations::ofProperty($class, $property, $type);
149
    }
150
}
151