Passed
Pull Request — master (#1332)
by Asmir
02:29
created

SerializerBuilder::build()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 13.7025

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 10
eloc 38
c 5
b 0
f 0
nc 192
nop 0
dl 0
loc 59
rs 6.9
ccs 4
cts 6
cp 0.6667
crap 13.7025

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\CachedReader;
9
use Doctrine\Common\Annotations\PsrCachedReader;
10
use Doctrine\Common\Annotations\Reader;
11
use Doctrine\Common\Cache\FilesystemCache;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Cache\FilesystemCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use JMS\Serializer\Accessor\AccessorStrategyInterface;
13
use JMS\Serializer\Accessor\DefaultAccessorStrategy;
14
use JMS\Serializer\Builder\DefaultDriverFactory;
15
use JMS\Serializer\Builder\DocBlockDriverFactory;
16
use JMS\Serializer\Builder\DriverFactoryInterface;
17
use JMS\Serializer\Construction\ObjectConstructorInterface;
18
use JMS\Serializer\Construction\UnserializeObjectConstructor;
19
use JMS\Serializer\ContextFactory\CallableDeserializationContextFactory;
20
use JMS\Serializer\ContextFactory\CallableSerializationContextFactory;
21
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
22
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
23
use JMS\Serializer\EventDispatcher\EventDispatcher;
24
use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
25
use JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber;
26
use JMS\Serializer\Exception\InvalidArgumentException;
27
use JMS\Serializer\Exception\RuntimeException;
28
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
29
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
30
use JMS\Serializer\GraphNavigator\Factory\DeserializationGraphNavigatorFactory;
31
use JMS\Serializer\GraphNavigator\Factory\GraphNavigatorFactoryInterface;
32
use JMS\Serializer\GraphNavigator\Factory\SerializationGraphNavigatorFactory;
33
use JMS\Serializer\Handler\ArrayCollectionHandler;
34
use JMS\Serializer\Handler\DateHandler;
35
use JMS\Serializer\Handler\HandlerRegistry;
36
use JMS\Serializer\Handler\HandlerRegistryInterface;
37
use JMS\Serializer\Handler\IteratorHandler;
38
use JMS\Serializer\Handler\StdClassHandler;
39
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
40
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
41
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
42
use JMS\Serializer\Type\Parser;
43
use JMS\Serializer\Type\ParserInterface;
44
use JMS\Serializer\Visitor\Factory\DeserializationVisitorFactory;
45
use JMS\Serializer\Visitor\Factory\JsonDeserializationVisitorFactory;
46
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
47
use JMS\Serializer\Visitor\Factory\SerializationVisitorFactory;
48
use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory;
49
use JMS\Serializer\Visitor\Factory\XmlSerializationVisitorFactory;
50
use Metadata\Cache\CacheInterface;
51
use Metadata\Cache\FileCache;
52
use Metadata\MetadataFactory;
53
use Metadata\MetadataFactoryInterface;
54
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
55
56
/**
57
 * Builder for serializer instances.
58
 *
59
 * This object makes serializer construction a breeze for projects that do not use
60
 * any special dependency injection container.
61
 *
62
 * @author Johannes M. Schmitt <[email protected]>
63
 */
64
final class SerializerBuilder
65
{
66
    /**
67
     * @var string[]
68
     */
69
    private $metadataDirs = [];
70
71
    /**
72
     * @var HandlerRegistryInterface
73
     */
74
    private $handlerRegistry;
75
76
    /**
77
     * @var bool
78
     */
79
    private $handlersConfigured = false;
80
81
    /**
82
     * @var EventDispatcherInterface
83
     */
84
    private $eventDispatcher;
85
86
    /**
87
     * @var bool
88
     */
89
    private $listenersConfigured = false;
90
91
    /**
92
     * @var ObjectConstructorInterface
93
     */
94
    private $objectConstructor;
95
96 330
    /**
97
     * @var SerializationVisitorFactory[]
98 330
     */
99
    private $serializationVisitors;
100
101 330
    /**
102
     * @var DeserializationVisitorFactory[]
103 330
     */
104 330
    private $deserializationVisitors;
105 330
106 330
    /**
107 330
     * @var bool
108
     */
109 330
    private $visitorsAdded = false;
110 284
111
    /**
112 330
     * @var PropertyNamingStrategyInterface
113 284
     */
114
    private $propertyNamingStrategy;
115 330
116
    /**
117
     * @var bool
118
     */
119
    private $debug = false;
120
121
    /**
122
     * @var string
123 330
     */
124
    private $cacheDir;
125 330
126 330
    /**
127
     * @var AnnotationReader
128 330
     */
129
    private $annotationReader;
130
131 25
    /**
132
     * @var bool
133 25
     */
134
    private $includeInterfaceMetadata = false;
135 25
136
    /**
137
     * @var DriverFactoryInterface
138 1
     */
139
    private $driverFactory;
140 1
141
    /**
142 1
     * @var SerializationContextFactoryInterface
143
     */
144
    private $serializationContextFactory;
145
146
    /**
147
     * @var DeserializationContextFactoryInterface
148
     */
149
    private $deserializationContextFactory;
150
151
    /**
152
     * @var ParserInterface
153
     */
154
    private $typeParser;
155
156
    /**
157
     * @var ExpressionEvaluatorInterface
158
     */
159 1
    private $expressionEvaluator;
160
161 1
    /**
162 1
     * @var AccessorStrategyInterface
163
     */
164 1
    private $accessorStrategy;
165
166
    /**
167
     * @var CacheInterface
168 1
     */
169
    private $metadataCache;
170 1
171
    /**
172
     * @var bool
173 71
     */
174
    private $docBlockTyperResolver;
175 71
176 71
    /**
177 71
     * @param mixed ...$args
178 71
     *
179
     * @return SerializerBuilder
180 71
     */
181
    public static function create(...$args): self
182
    {
183 3
        return new static(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $handlerRegistry of JMS\Serializer\SerializerBuilder::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

183
        return new static(/** @scrutinizer ignore-type */ ...$args);
Loading history...
184
    }
185 3
186 3
    public function __construct(?HandlerRegistryInterface $handlerRegistry = null, ?EventDispatcherInterface $eventDispatcher = null)
187
    {
188 3
        $this->typeParser = new Parser();
189
        $this->handlerRegistry = $handlerRegistry ?: new HandlerRegistry();
190
        $this->eventDispatcher = $eventDispatcher ?: new EventDispatcher();
191 73
        $this->serializationVisitors = [];
192
        $this->deserializationVisitors = [];
193 73
194 73
        if ($handlerRegistry) {
195
            $this->handlersConfigured = true;
196 73
        }
197
198
        if ($eventDispatcher) {
199 1
            $this->listenersConfigured = true;
200
        }
201 1
    }
202 1
203
    public function setAccessorStrategy(AccessorStrategyInterface $accessorStrategy): self
204 1
    {
205
        $this->accessorStrategy = $accessorStrategy;
206
207 4
        return $this;
208
    }
209 4
210
    private function getAccessorStrategy(): AccessorStrategyInterface
211 4
    {
212
        if (!$this->accessorStrategy) {
213
            $this->accessorStrategy = new DefaultAccessorStrategy($this->expressionEvaluator);
214
        }
215
216
        return $this->accessorStrategy;
217
    }
218
219
    public function setExpressionEvaluator(ExpressionEvaluatorInterface $expressionEvaluator): self
220
    {
221 2
        $this->expressionEvaluator = $expressionEvaluator;
222
223 2
        return $this;
224 2
    }
225
226 2
    public function setTypeParser(ParserInterface $parser): self
227
    {
228
        $this->typeParser = $parser;
229
230
        return $this;
231
    }
232
233
    public function setAnnotationReader(Reader $reader): self
234
    {
235
        $this->annotationReader = $reader;
0 ignored issues
show
Documentation Bug introduced by
$reader is of type Doctrine\Common\Annotations\Reader, but the property $annotationReader was declared to be of type Doctrine\Common\Annotations\AnnotationReader. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
236
237 329
        return $this;
238
    }
239 329
240 329
    public function setDebug(bool $bool): self
241 329
    {
242 329
        $this->debug = $bool;
243
244
        return $this;
245 329
    }
246
247
    public function setCacheDir(string $dir): self
248 329
    {
249
        if (!is_dir($dir)) {
250 329
            $this->createDir($dir);
251 329
        }
252 329
253 329
        if (!is_writable($dir)) {
254
            throw new InvalidArgumentException(sprintf('The cache directory "%s" is not writable.', $dir));
255
        }
256 329
257
        $this->cacheDir = $dir;
258
259
        return $this;
260
    }
261
262
    public function addDefaultHandlers(): self
263
    {
264 1
        $this->handlersConfigured = true;
265
        $this->handlerRegistry->registerSubscribingHandler(new DateHandler());
266 1
        $this->handlerRegistry->registerSubscribingHandler(new StdClassHandler());
267
        $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler());
268 1
        $this->handlerRegistry->registerSubscribingHandler(new IteratorHandler());
269
270
        return $this;
271
    }
272
273
    public function configureHandlers(\Closure $closure): self
274
    {
275
        $this->handlersConfigured = true;
276
        $closure($this->handlerRegistry);
277
278
        return $this;
279
    }
280
281
    public function addDefaultListeners(): self
282 2
    {
283
        $this->listenersConfigured = true;
284 2
        $this->eventDispatcher->addSubscriber(new DoctrineProxySubscriber());
285 2
286 2
        return $this;
287
    }
288
289
    public function configureListeners(\Closure $closure): self
290 2
    {
291
        $this->listenersConfigured = true;
292 2
        $closure($this->eventDispatcher);
293
294
        return $this;
295
    }
296
297
    public function setObjectConstructor(ObjectConstructorInterface $constructor): self
298
    {
299
        $this->objectConstructor = $constructor;
300
301
        return $this;
302
    }
303
304
    public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy): self
305
    {
306
        $this->propertyNamingStrategy = $propertyNamingStrategy;
307
308
        return $this;
309
    }
310
311
    public function setSerializationVisitor(string $format, SerializationVisitorFactory $visitor): self
312
    {
313
        $this->visitorsAdded = true;
314
        $this->serializationVisitors[$format] = $visitor;
315
316
        return $this;
317
    }
318
319
    public function setDeserializationVisitor(string $format, DeserializationVisitorFactory $visitor): self
320
    {
321
        $this->visitorsAdded = true;
322
        $this->deserializationVisitors[$format] = $visitor;
323
324
        return $this;
325
    }
326
327
    public function addDefaultSerializationVisitors(): self
328
    {
329
        $this->visitorsAdded = true;
330
        $this->serializationVisitors = [
331
            'xml' => new XmlSerializationVisitorFactory(),
332
            'json' => new JsonSerializationVisitorFactory(),
333
        ];
334
335
        return $this;
336
    }
337
338
    public function addDefaultDeserializationVisitors(): self
339
    {
340
        $this->visitorsAdded = true;
341
        $this->deserializationVisitors = [
342
            'xml' => new XmlDeserializationVisitorFactory(),
343
            'json' => new JsonDeserializationVisitorFactory(),
344
        ];
345
346
        return $this;
347
    }
348
349
    /**
350
     * @param bool $include Whether to include the metadata from the interfaces
351
     *
352
     * @return SerializerBuilder
353
     */
354
    public function includeInterfaceMetadata(bool $include): self
355
    {
356
        $this->includeInterfaceMetadata = $include;
357
358
        return $this;
359
    }
360
361
    /**
362
     * Sets a map of namespace prefixes to directories.
363
     *
364
     * This method overrides any previously defined directories.
365
     *
366
     * @param array <string,string> $namespacePrefixToDirMap
0 ignored issues
show
Documentation Bug introduced by
The doc comment <string,string> at position 0 could not be parsed: Unknown type name '<' at position 0 in <string,string>.
Loading history...
367
     *
368
     * @return SerializerBuilder
369
     *
370
     * @throws InvalidArgumentException When a directory does not exist.
371
     */
372
    public function setMetadataDirs(array $namespacePrefixToDirMap): self
373
    {
374
        foreach ($namespacePrefixToDirMap as $dir) {
375
            if (!is_dir($dir)) {
376
                throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
377
            }
378 13
        }
379
380 13
        $this->metadataDirs = $namespacePrefixToDirMap;
381
382 13
        return $this;
383
    }
384
385
    /**
386
     * Adds a directory where the serializer will look for class metadata.
387
     *
388
     * The namespace prefix will make the names of the actual metadata files a bit shorter. For example, let's assume
389
     * that you have a directory where you only store metadata files for the ``MyApplication\Entity`` namespace.
390 5
     *
391
     * If you use an empty prefix, your metadata files would need to look like:
392 5
     *
393 3
     * ``my-dir/MyApplication.Entity.SomeObject.yml``
394 2
     * ``my-dir/MyApplication.Entity.OtherObject.xml``
395 2
     *
396 2
     * If you use ``MyApplication\Entity`` as prefix, your metadata files would need to look like:
397
     *
398
     * ``my-dir/SomeObject.yml``
399
     * ``my-dir/OtherObject.yml``
400
     *
401
     * Please keep in mind that you currently may only have one directory per namespace prefix.
402 5
     *
403
     * @param string $dir             The directory where metadata files are located.
404
     * @param string $namespacePrefix An optional prefix if you only store metadata for specific namespaces in this directory.
405
     *
406
     * @return SerializerBuilder
407
     *
408
     * @throws InvalidArgumentException When a directory does not exist.
409
     * @throws InvalidArgumentException When a directory has already been registered.
410 3
     */
411
    public function addMetadataDir(string $dir, string $namespacePrefix = ''): self
412 3
    {
413 3
        if (!is_dir($dir)) {
414
            throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
415
        }
416
417
        if (isset($this->metadataDirs[$namespacePrefix])) {
418
            throw new InvalidArgumentException(sprintf('There is already a directory configured for the namespace prefix "%s". Please use replaceMetadataDir() to override directories.', $namespacePrefix));
419
        }
420
421
        $this->metadataDirs[$namespacePrefix] = $dir;
422 3
423
        return $this;
424
    }
425
426
    /**
427
     * Adds a map of namespace prefixes to directories.
428
     *
429
     * @param array <string,string> $namespacePrefixToDirMap
0 ignored issues
show
Documentation Bug introduced by
The doc comment <string,string> at position 0 could not be parsed: Unknown type name '<' at position 0 in <string,string>.
Loading history...
430
     *
431 330
     * @return SerializerBuilder
432
     */
433 330
    public function addMetadataDirs(array $namespacePrefixToDirMap): self
434 330
    {
435 330
        foreach ($namespacePrefixToDirMap as $prefix => $dir) {
436
            $this->addMetadataDir($dir, $prefix);
437 330
        }
438 1
439 1
        return $this;
440 1
    }
441
442
    /**
443
     * Similar to addMetadataDir(), but overrides an existing entry.
444 330
     *
445 318
     * @return SerializerBuilder
446 318
     *
447
     * @throws InvalidArgumentException When a directory does not exist.
448
     * @throws InvalidArgumentException When no directory is configured for the ns prefix.
449 330
     */
450 330
    public function replaceMetadataDir(string $dir, string $namespacePrefix = ''): self
451
    {
452 330
        if (!is_dir($dir)) {
453
            throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
454 330
        }
455
456 330
        if (!isset($this->metadataDirs[$namespacePrefix])) {
457 1
            throw new InvalidArgumentException(sprintf('There is no directory configured for namespace prefix "%s". Please use addMetadataDir() for adding new directories.', $namespacePrefix));
458 1
        }
459
460
        $this->metadataDirs[$namespacePrefix] = $dir;
461 330
462 71
        return $this;
463
    }
464
465 330
    public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory): self
466 73
    {
467
        $this->driverFactory = $driverFactory;
468
469 330
        return $this;
470 329
    }
471 329
472
    /**
473
     * @param SerializationContextFactoryInterface|callable $serializationContextFactory
474 330
     */
475 330
    public function setSerializationContextFactory($serializationContextFactory): self
476
    {
477
        if ($serializationContextFactory instanceof SerializationContextFactoryInterface) {
478 330
            $this->serializationContextFactory = $serializationContextFactory;
479 330
        } elseif (is_callable($serializationContextFactory)) {
480 330
            $this->serializationContextFactory = new CallableSerializationContextFactory(
481 330
                $serializationContextFactory
482 330
            );
483 330
        } else {
484 330
            throw new InvalidArgumentException('expected SerializationContextFactoryInterface or callable.');
485 330
        }
486
487
        return $this;
488 330
    }
489
490
    /**
491 330
     * @param DeserializationContextFactoryInterface|callable $deserializationContextFactory
492
     */
493 330
    public function setDeserializationContextFactory($deserializationContextFactory): self
494 330
    {
495 330
        if ($deserializationContextFactory instanceof DeserializationContextFactoryInterface) {
496 330
            $this->deserializationContextFactory = $deserializationContextFactory;
497 330
        } elseif (is_callable($deserializationContextFactory)) {
498 330
            $this->deserializationContextFactory = new CallableDeserializationContextFactory(
499
                $deserializationContextFactory
500
            );
501
        } else {
502 330
            throw new InvalidArgumentException('expected DeserializationContextFactoryInterface or callable.');
503
        }
504 330
505 330
        return $this;
506 330
    }
507 330
508 330
    public function setMetadataCache(CacheInterface $cache): self
509 330
    {
510 330
        $this->metadataCache = $cache;
511
512
        return $this;
513
    }
514 318
515
    public function setDocBlockTypeResolver(bool $docBlockTypeResolver): self
516 318
    {
517
        $this->docBlockTyperResolver = $docBlockTypeResolver;
518
519
        return $this;
520 318
    }
521 318
522
    public function build(): Serializer
523 1
    {
524
        $annotationReader = $this->annotationReader;
525 1
        if (null === $annotationReader) {
526
            $annotationReader = new AnnotationReader();
527
            $annotationReader = $this->decorateAnnotationReader($annotationReader);
528
        }
529 1
530
        if (null === $this->driverFactory) {
531
            $this->initializePropertyNamingStrategy();
532 1
            $this->driverFactory = new DefaultDriverFactory(
533
                $this->propertyNamingStrategy,
534
                $this->typeParser,
535
                $this->expressionEvaluator instanceof CompilableExpressionEvaluatorInterface ? $this->expressionEvaluator : null
536
            );
537
        }
538
539
        if ($this->docBlockTyperResolver) {
540
            $this->driverFactory = new DocBlockDriverFactory($this->driverFactory, $this->typeParser);
541
        }
542
543
        $metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader);
544
        $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
545
546
        $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
547
548
        if (null !== $this->metadataCache) {
549
            $metadataFactory->setCache($this->metadataCache);
550
        } elseif (null !== $this->cacheDir) {
551
            $this->createDir($this->cacheDir . '/metadata');
552
            $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
553
        }
554
555
        if (!$this->handlersConfigured) {
556
            $this->addDefaultHandlers();
557
        }
558
559
        if (!$this->listenersConfigured) {
560
            $this->addDefaultListeners();
561
        }
562
563
        if (!$this->visitorsAdded) {
564
            $this->addDefaultSerializationVisitors();
565
            $this->addDefaultDeserializationVisitors();
566
        }
567
568
        $navigatorFactories = [
569
            GraphNavigatorInterface::DIRECTION_SERIALIZATION => $this->getSerializationNavigatorFactory($metadataFactory),
570
            GraphNavigatorInterface::DIRECTION_DESERIALIZATION => $this->getDeserializationNavigatorFactory($metadataFactory),
571
        ];
572
573
        return new Serializer(
574
            $metadataFactory,
575
            $navigatorFactories,
576
            $this->serializationVisitors,
577
            $this->deserializationVisitors,
578
            $this->serializationContextFactory,
579
            $this->deserializationContextFactory,
580
            $this->typeParser
581
        );
582
    }
583
584
    private function getSerializationNavigatorFactory(MetadataFactoryInterface $metadataFactory): GraphNavigatorFactoryInterface
585
    {
586
        return new SerializationGraphNavigatorFactory(
587
            $metadataFactory,
588
            $this->handlerRegistry,
589
            $this->getAccessorStrategy(),
590
            $this->eventDispatcher,
591
            $this->expressionEvaluator
592
        );
593
    }
594
595
    private function getDeserializationNavigatorFactory(MetadataFactoryInterface $metadataFactory): GraphNavigatorFactoryInterface
596
    {
597
        return new DeserializationGraphNavigatorFactory(
598
            $metadataFactory,
599
            $this->handlerRegistry,
600
            $this->objectConstructor ?: new UnserializeObjectConstructor(),
601
            $this->getAccessorStrategy(),
602
            $this->eventDispatcher,
603
            $this->expressionEvaluator
604
        );
605
    }
606
607
    private function initializePropertyNamingStrategy(): void
608
    {
609
        if (null !== $this->propertyNamingStrategy) {
610
            return;
611
        }
612
613
        $this->propertyNamingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
614
    }
615
616
    private function createDir(string $dir): void
617
    {
618
        if (is_dir($dir)) {
619
            return;
620
        }
621
622
        if (false === @mkdir($dir, 0777, true) && false === is_dir($dir)) {
623
            throw new RuntimeException(sprintf('Could not create directory "%s".', $dir));
624
        }
625
    }
626
627
    private function decorateAnnotationReader(Reader $annotationReader): Reader
628
    {
629
        if (null !== $this->cacheDir) {
630
            $this->createDir($this->cacheDir . '/annotations');
631
            if (class_exists(FilesystemAdapter::class)) {
632
                $annotationsCache = new FilesystemAdapter('', 0, $this->cacheDir . '/annotations');
633
                $annotationReader = new PsrCachedReader($annotationReader, $annotationsCache, $this->debug);
634
            } else {
635
                $annotationsCache = new FilesystemCache($this->cacheDir . '/annotations');
636
                $annotationReader = new CachedReader($annotationReader, $annotationsCache, $this->debug);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Annotations\CachedReader has been deprecated: the CachedReader is deprecated and will be removed in version 2.0.0 of doctrine/annotations. Please use the {@see \Doctrine\Common\Annotations\PsrCachedReader} instead. ( Ignorable by Annotation )

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

636
                $annotationReader = /** @scrutinizer ignore-deprecated */ new CachedReader($annotationReader, $annotationsCache, $this->debug);
Loading history...
637
            }
638
        }
639
640
        return $annotationReader;
641
    }
642
}
643