Passed
Push — master ( 818b3e...c1bcf5 )
by Thierry
02:23
created

AnnotationReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * AnnotationReader.php
5
 *
6
 * Jaxon annotation manager.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @copyright 2022 Thierry Feuzeu <[email protected]>
10
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
13
14
namespace Jaxon\Annotations;
15
16
use Jaxon\Annotations\Annotation\AbstractAnnotation;
17
use Jaxon\Annotations\Annotation\AfterAnnotation;
18
use Jaxon\Annotations\Annotation\BeforeAnnotation;
19
use Jaxon\Annotations\Annotation\DataBagAnnotation;
20
use Jaxon\Annotations\Annotation\ExcludeAnnotation;
21
use Jaxon\Annotations\Annotation\UploadAnnotation;
22
use mindplay\annotations\AnnotationException;
23
use mindplay\annotations\AnnotationManager;
24
25
use function array_filter;
26
use function count;
27
use function is_a;
28
29
class AnnotationReader
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class AnnotationReader
Loading history...
30
{
31
    /**
32
     * @var AnnotationManager
33
     */
34
    protected $xManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
35
36
    /**
37
     * The constructor
38
     *
39
     * @param AnnotationManager $xManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
40
     */
41
    public function __construct(AnnotationManager $xManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
42
    {
43
        $this->xManager = $xManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 22 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
        $this->xManager->registry['upload'] = UploadAnnotation::class;
45
        $this->xManager->registry['databag'] = DataBagAnnotation::class;
46
        $this->xManager->registry['exclude'] = ExcludeAnnotation::class;
47
        $this->xManager->registry['before'] = BeforeAnnotation::class;;
48
        $this->xManager->registry['after'] = AfterAnnotation::class;;
49
        // Missing standard annotations.
50
        // We need to define this, otherwise they throw an exception, and make the whole processing fail.
51
        $this->xManager->registry['const'] = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
        $this->xManager->registry['inheritDoc'] = false;
53
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
54
55
    /**
56
     * @param array $aAnnotations
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     *
58
     * @return AbstractAnnotation[]
59
     */
60
    private function filterAnnotations(array $aAnnotations): array
61
    {
62
        // Only keep the annotations declared in this package.
63
        $aAnnotations = array_filter($aAnnotations, function($xAnnotation) {
64
            return is_a($xAnnotation, AbstractAnnotation::class);
65
        });
66
67
        $aAttributes = [];
68
        foreach($aAnnotations as $xAnnotation)
69
        {
70
            $sName = $xAnnotation->getName();
71
            if(isset($aAttributes[$sName]))
72
            {
73
                $xAnnotation->setPrevValue($aAttributes[$sName]);
74
            }
75
            $xValue = $xAnnotation->getValue();
76
            if($sName === 'protected' && !$xValue)
77
            {
78
                continue; // Ignore annotation @exclude with value false
79
            }
80
            $aAttributes[$sName] = $xValue;
81
        }
82
        return $aAttributes;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Get the class attributes from its annotations
87
     *
88
     * @param string $sClass
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     * @param array $aMethods
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
90
     *
91
     * @return array
92
     * @throws AnnotationException
93
     */
94
    public function getAttributes(string $sClass, array $aMethods): array
95
    {
96
        // Class annotations
97
        $aClassAttrs = $this->filterAnnotations($this->xManager->getClassAnnotations($sClass));
98
        if(isset($aClassAttrs['protected']))
99
        {
100
            return [true, [], []]; // The entire class is not to be exported.
101
        }
102
103
        $aProtected = [];
104
        $aAttributes = [];
105
        if(count($aClassAttrs) > 0)
106
        {
107
            $aAttributes['*'] = $aClassAttrs;
108
        }
109
110
        // Methods annotations
111
        foreach($aMethods as $sMethod)
112
        {
113
            $aMethodAttrs = $this->filterAnnotations($this->xManager->getMethodAnnotations($sClass, $sMethod));
114
            if(isset($aMethodAttrs['protected']))
115
            {
116
                $aProtected[] = $sMethod; // The method is not to be exported.
117
            }
118
            elseif(count($aMethodAttrs) > 0)
119
            {
120
                $aAttributes[$sMethod] = $aMethodAttrs;
121
            }
122
        }
123
        return [false, $aAttributes, $aProtected];
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
125
}
126