Annotations   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 42
c 7
b 0
f 0
dl 0
loc 154
ccs 48
cts 50
cp 0.96
rs 10
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A tryInstantiateCache() 0 6 2
A enableCacheComponent() 0 6 2
A configurationCache() 0 4 1
A init() 0 5 1
A trySetCacheFileSuffix() 0 4 2
A getReader() 0 7 1
A getParser() 0 6 1
A getDefaultCache() 0 4 1
A trySetCachePath() 0 4 3
A registerLoader() 0 6 2
A enableNewReader() 0 5 2
1
<?php
2
3
namespace yii\annotations;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use Doctrine\Common\Annotations\DocParser;
9
use Exception;
10
use Yii;
11
use yii\base\Component;
12
use yii\caching\CacheInterface;
13
use yii\caching\FileCache;
14
use yii\di\Instance;
15
16
/**
17
 * Class Annotations
18
 * @property CacheInterface|object $cacheComponent
19
 * @property FileCache $defaultCache
20
 */
21
class Annotations extends Component implements AnnotationsInterface, ParserInterface
22
{
23
    /**
24
     * @var string|CacheInterface
25
     */
26
    public $cache = 'cache';
27
28
    /**
29
     * @var bool
30
     */
31
    public $debug = false;
32
33
    /**
34
     * @var string
35
     */
36
    public $path;
37
38
    /**
39
     * @var AnnotationReader
40
     */
41
    private $reader;
42
43
    /**
44
     * @var array
45
     */
46
    public $ignoreAnnotations = [];
47
48
    /**
49
     * @var CacheInterface
50
     */
51
    private $cacheComponent;
52
53
    /**
54
     * @var FileCache
55
     */
56
    private const DEFAULT_CACHE = FileCache::class;
57
58
    /**
59
     * @var string
60
     */
61
    protected const CACHE_PREFIX = '.annotations';
62
63
    /**
64
     */
65 19
    public function init(): void
66
    {
67 19
        parent::init();
68 19
        $this->registerLoader();
69 19
        $this->enableCacheComponent();
70 19
    }
71
72
    /** {@inheritDoc} */
73 11
    public function getReader(): AnnotationCacheReader
74
    {
75 11
        $this->enableNewReader();
76 11
        return new AnnotationCacheReader(
77 11
            $this->reader,
78 11
            new AnnotationCache($this->cacheComponent),
79 11
            $this->debug
80
        );
81
    }
82
83
    /**
84
     * @return void
85
     */
86 19
    private function enableCacheComponent(): void
87
    {
88 19
        $this->cacheComponent = (is_string($this->cache)
89 19
                ? $this->tryInstantiateCache()
90 19
                : $this->cache) ?? $this->getDefaultCache();
91 19
        $this->configurationCache();
92 19
    }
93
94
    /**
95
     * @return object|CacheInterface|null
96
     */
97 19
    private function tryInstantiateCache()
98
    {
99
        try {
100 19
            return Instance::ensure($this->cache);
101 8
        } catch (Exception $e) {
102 8
            return null;
103
        }
104
    }
105
106
    /**
107
     * Return default cache
108
     */
109 8
    private function getDefaultCache()
110
    {
111 8
        $defaultCache = static::DEFAULT_CACHE;
112 8
        return new $defaultCache();
113
    }
114
115
    /**
116
     * @return void
117
     */
118 19
    private function configurationCache(): void
119
    {
120 19
        $this->trySetCachePath();
121 19
        $this->trySetCacheFileSuffix();
122 19
    }
123
124
    /**
125
     * @return void
126
     */
127 19
    private function trySetCachePath(): void
128
    {
129 19
        if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) {
130
            $this->cacheComponent->cachePath = Yii::getAlias($this->path);
131
        }
132 19
    }
133
134
    /**
135
     * @return void
136
     */
137 19
    private function trySetCacheFileSuffix(): void
138
    {
139 19
        if (property_exists($this->cacheComponent, 'cacheFileSuffix')) {
140 10
            $this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
141
        }
142 19
    }
143
144
    /**
145
     *
146
     */
147 19
    private function registerLoader()
148
    {
149 19
        if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
150
            /** @scrutinizer ignore-deprecated */
151
            /** @noinspection PhpDeprecationInspection */
152 19
            AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

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

152
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
153
        }
154 19
    }
155
156
    /**
157
     * @return void
158
     * @throws AnnotationException
159
     */
160 11
    private function enableNewReader(): void
161
    {
162 11
        $this->reader = new AnnotationReader();
163 11
        foreach ($this->ignoreAnnotations as $annotation) {
164
            $this->reader::addGlobalIgnoredName($annotation);
165
        }
166 11
    }
167
168
    /** {@inheritDoc} */
169 2
    public function getParser(array $importAnnotations = []): DocParser
170
    {
171 2
        $parser = new DocParser();
172 2
        $parser->setImports($importAnnotations);
173 2
        $parser->setIgnoreNotImportedAnnotations(true);
174 2
        return $parser;
175
    }
176
}
177