Passed
Pull Request — master (#58)
by Daniel
07:54 queued 02:33
created

getConfigurationFromParentClasses()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\AnnotationReader;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
19
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 * @author Daniel West <[email protected]>
24
 */
25
abstract class AnnotationReader implements AnnotationReaderInterface
26
{
27
    use ClassMetadataTrait;
28
29
    protected Reader $reader;
30
31 9
    public function __construct(Reader $reader, ManagerRegistry $managerRegistry)
32
    {
33 9
        $this->reader = $reader;
34 9
        $this->initRegistry($managerRegistry);
35 9
    }
36
37
    abstract public function getConfiguration($class);
38
39
    /**
40
     * @param object|string $class
41
     */
42 9
    public function isConfigured($class): bool
43
    {
44
        try {
45 9
            $this->getConfiguration($class);
46 9
        } catch (InvalidArgumentException $e) {
47 9
            return false;
48
        }
49
50 9
        return true;
51
    }
52
53
    /**
54
     * @param object|string $class
55
     *
56
     * @throws \ReflectionException
57
     */
58 9
    protected function getClassAnnotationConfiguration($class, string $annotationClass)
59
    {
60 9
        if (null === $class || (\is_string($class) && !class_exists($class))) {
61
            throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__));
62
        }
63
64 9
        $reflection = new \ReflectionClass($class);
65 9
        $annotation = $this->reader->getClassAnnotation($reflection, $annotationClass);
66 9
        if (!$annotation) {
67 9
            $annotation = $this->getConfigurationFromParentClasses($reflection, $annotationClass);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $annotation is correct as $this->getConfigurationF...tion, $annotationClass) targeting Silverback\ApiComponents...tionFromParentClasses() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68 9
            if (!$annotation) {
0 ignored issues
show
introduced by
$annotation is of type null, thus it always evaluated to false.
Loading history...
69 9
                $annotation = $this->getConfigurationFromTraits($reflection, $annotationClass);
70 9
                if (!$annotation) {
71 9
                    throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass));
72
                }
73
            }
74
        }
75
76 9
        return $annotation;
77
    }
78
79 9
    private function getConfigurationFromParentClasses(\ReflectionClass $reflection, string $annotationClass)
80
    {
81 9
        $annotation = null;
82 9
        $parentReflection = $reflection->getParentClass();
83 9
        while ($parentReflection && !($annotation = $this->reader->getClassAnnotation($parentReflection, $annotationClass))) {
84 9
            $parentReflection = $parentReflection->getParentClass();
85
        }
86
87 9
        return $annotation;
88
    }
89
90 9
    private function getConfigurationFromTraits(\ReflectionClass $reflection, string $annotationClass)
91
    {
92 9
        $annotation = null;
93 9
        $traits = $reflection->getTraits();
94 9
        foreach ($traits as $trait) {
95 9
            $annotation = $this->reader->getClassAnnotation($trait, $annotationClass);
96 9
            if ($annotation) {
97
                break;
98
            }
99
        }
100
101 9
        return $annotation;
102
    }
103
}
104