Passed
Pull Request — master (#50)
by Alex
03:38
created

FindVocabularyAnnotations()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 21
c 1
b 0
f 0
nc 7
nop 4
dl 0
loc 41
rs 6.6166

How to fix   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
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 22/07/20
6
 * Time: 10:26 PM
7
 */
8
9
namespace AlgoWeb\ODataMetadata\Helpers;
10
11
use AlgoWeb\ODataMetadata\EdmUtil;
12
use AlgoWeb\ODataMetadata\Interfaces\Annotations\IVocabularyAnnotation;
13
use AlgoWeb\ODataMetadata\Interfaces\IModel;
14
use AlgoWeb\ODataMetadata\Interfaces\IStructuredType;
15
use AlgoWeb\ODataMetadata\Interfaces\ITerm;
16
use AlgoWeb\ODataMetadata\Interfaces\IVocabularyAnnotatable;
17
18
trait ModelHelpersVocabularyAnnotation
19
{
20
    /**
21
     * Gets an annotatable element's vocabulary annotations that bind a particular term.
22
     *
23
     * @param IVocabularyAnnotatable $element  Element to check for annotations.
24
     * @param ITerm|string|null $term Term to search for. OR Name of the term to search for.
25
     * @param string|null $qualifier Qualifier to apply.
26
     * @param string|null $type Type of the annotation being returned.
27
     * @return iterable|IVocabularyAnnotation[] Annotations attached to the element by this model or by models
28
     * referenced by this model that bind the term with the given qualifier.
29
     */
30
    public function FindVocabularyAnnotations(
31
        IVocabularyAnnotatable $element,
32
        $term = null,
33
        string $qualifier = null,
34
        string $type = null
35
    ): iterable {
36
        assert(
37
            null === $term || $term instanceof ITerm || is_string($term),
38
               '$term should be a string or instanceof iTerm'
39
        );
40
        if (null === $term) {
41
            return $this->processNullVocabularyAnnotationTerm($element);
42
        }
43
        if (is_string($term)) {
44
            $termName = $term;
45
            // Look up annotations on the element by name. There's no particular advantage in searching for a term first.
46
            $name = null;
47
            $namespaceName = null;
48
49
            if (EdmUtil::TryGetNamespaceNameFromQualifiedName($termName, $namespaceName, $name)) {
50
                /**
51
                 * @var IVocabularyAnnotation $annotation
52
                 */
53
                foreach ($this->FindVocabularyAnnotations($element) as $annotation) {
54
                    if (null !== $type && !is_a($annotation, $type)) {
55
                        continue;
56
                    }
57
                    $annotationTerm = $annotation->getTerm();
58
                    if ($annotationTerm->getNamespace() === $namespaceName &&
59
                        $annotationTerm->getName() === $name &&
60
                        (
61
                            null === $qualifier ||
62
                            $qualifier == $annotation->getQualifier()
63
                        )
64
                    ) {
65
                        yield $annotation;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $annotation returns the type Generator which is incompatible with the documented return type AlgoWeb\ODataMetadata\In...ryAnnotation[]|iterable.
Loading history...
66
                    }
67
                }
68
            }
69
        } else {
70
            return $this->processTermVocabularyAnnotationTerm($element, $term, $qualifier, $type);
71
        }
72
    }
73
74
    /**
75
     * Gets an annotatable element's vocabulary annotations defined in a specific model and models referenced by
76
     * that model.
77
     * @param IVocabularyAnnotatable $element Element to check for annotations.
78
     * @return IVocabularyAnnotation[] Annotations attached to the element (or, if the element is a type, to its base
79
     * types) by this model or by models referenced by this model.
80
     */
81
    public function FindVocabularyAnnotationsIncludingInheritedAnnotations(IVocabularyAnnotatable $element): array
82
    {
83
        /**
84
         * @var IVocabularyAnnotation[] $result
85
         */
86
        $result = $this->FindDeclaredVocabularyAnnotations($element);
0 ignored issues
show
Bug introduced by
It seems like FindDeclaredVocabularyAnnotations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
        /** @scrutinizer ignore-call */ 
87
        $result = $this->FindDeclaredVocabularyAnnotations($element);
Loading history...
87
88
        if ($element instanceof IStructuredType) {
89
            $typeElement = $element;
90
            assert($typeElement instanceof IStructuredType);
91
            $typeElement = $typeElement->getBaseType();
92
            while (null !== $typeElement) {
93
                if ($typeElement instanceof IVocabularyAnnotatable) {
94
                    $result = array_merge($result, $this->FindDeclaredVocabularyAnnotations($typeElement));
95
                }
96
97
                $typeElement = $typeElement->getBaseType();
98
            }
99
        }
100
101
        return $result;
102
    }
103
104
    /**
105
     * @param IVocabularyAnnotatable $element
106
     * @return IVocabularyAnnotation[]|array
107
     */
108
    protected function processNullVocabularyAnnotationTerm(
109
        IVocabularyAnnotatable $element
110
    ): array {
111
        $result = $this->FindVocabularyAnnotationsIncludingInheritedAnnotations($element);
112
        foreach ($this->getReferencedModels() as $referencedModel) {
113
            $result = array_merge(
114
                $result,
115
                $referencedModel->FindVocabularyAnnotationsIncludingInheritedAnnotations($element)
116
            );
117
        }
118
        return $result;
119
    }
120
121
    /**
122
     * @param IVocabularyAnnotatable $element
123
     * @param ITerm $term
124
     * @param string $qualifier
125
     * @param string $type
126
     * @return array
127
     */
128
    protected function processTermVocabularyAnnotationTerm(
129
        IVocabularyAnnotatable $element,
130
        ITerm $term,
131
        string $qualifier = null,
132
        string $type = null
133
    ): array {
134
        $result = [];
135
        /**
136
         * @var IVocabularyAnnotation $annotation
137
         */
138
        foreach ($this->FindVocabularyAnnotations($element) as $annotation) {
139
            if (null !== $type && !is_a($annotation, $type)) {
140
                continue;
141
            }
142
143
            if ($annotation->getTerm() == $term && (null === $qualifier || $qualifier == $annotation->getQualifier())) {
144
                $result[] = $annotation;
145
            }
146
        }
147
148
        return $result;
149
    }
150
151
    /**
152
     * @return IModel[] gets the collection of models referred to by this model
153
     */
154
    abstract public function getReferencedModels(): array;
155
}
156