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

BeforeAnnotation::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * BeforeAnnotation.php
5
 *
6
 * Jaxon annotation for callbacks.
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\Annotation;
15
16
use mindplay\annotations\AnnotationException;
17
18
use function array_keys;
19
use function is_array;
20
use function is_string;
21
22
/**
23
 * Specifies a method to be called before the one targeted by a Jaxon request.
24
 *
25
 * @usage('class' => true, 'method'=>true, 'multiple'=>true, 'inherited'=>true)
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class BeforeAnnotation extends AbstractAnnotation
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $sMethodName = '';
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
33
34
    /**
35
     * @var array
36
     */
37
    protected $sMethodParams = [];
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $properties should have a doc-comment as per coding-style.
Loading history...
40
     * @inheritDoc
41
     * @throws AnnotationException
42
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
43
    public function initAnnotation(array $properties)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
44
    {
45
        if(!isset($properties['call']) || !is_string($properties['call']))
46
        {
47
            throw new AnnotationException('The @before annotation requires a property "call" of type string');
48
        }
49
        foreach(array_keys($properties) as $propName)
50
        {
51
            if($propName !== 'call' && $propName !== 'with')
52
            {
53
                throw new AnnotationException('Unknown property "' . $propName . '" in the @before annotation');
54
            }
55
        }
56
        if(isset($properties['with']))
57
        {
58
            if(!is_array($properties['with']))
59
            {
60
                throw new AnnotationException('The "with" property of the @before annotation must be of type array');
61
            }
62
            $this->sMethodParams = $properties['with'];
63
        }
64
        $this->sMethodName = $properties['call'];
65
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
66
67
    /**
68
     * @inheritDoc
69
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
    public function getName(): string
71
    {
72
        return '__before';
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    /**
76
     * @inheritDoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getValue()
79
    {
80
        if(is_array($this->xPrevValue))
81
        {
82
            // Add the current value to the array
83
            $this->xPrevValue[$this->sMethodName] = $this->sMethodParams;
84
            return $this->xPrevValue;
85
        }
86
        // Return the current value in an array
87
        return [$this->sMethodName => $this->sMethodParams];
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
89
}
90