HookAnnotation   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAnnotation() 0 9 2
B initAnnotation() 0 26 8
1
<?php
2
3
/**
4
 * HookAnnotation.php
5
 *
6
 * Common base class for before and after annotations.
7
 *
8
 * @package jaxon-annotations
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-annotations
13
 */
14
15
namespace Jaxon\Annotations\Annotation;
16
17
use mindplay\annotations\AnnotationException;
18
19
use function array_key_exists;
20
use function array_keys;
21
use function count;
22
use function is_array;
23
use function is_string;
24
use function json_decode;
25
use function preg_split;
26
use function rtrim;
27
28
abstract class HookAnnotation extends AbstractAnnotation
29
{
30
    /**
31
     * @var string
32
     */
33
    protected $sMethod = '';
34
35
    /**
36
     * @var array
37
     */
38
    protected $aParams = [];
39
40
    /**
41
     *
42
     */
43
    abstract protected static function getType(): string;
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public static function parseAnnotation($value)
49
    {
50
        $aParams = preg_split('/[\s]+/', $value, 2);
51
        if(count($aParams) === 1)
52
        {
53
            return ['call' => rtrim($aParams[0])];
54
        }
55
        // The second parameter must be an array of callback parameter in json format.
56
        return ['call' => rtrim($aParams[0]), 'with' => json_decode($aParams[1], false)];
57
    }
58
59
    /**
60
     * @inheritDoc
61
     * @throws AnnotationException
62
     */
63
    public function initAnnotation(array $properties)
64
    {
65
        if(!isset($properties['call']) || !is_string($properties['call']))
66
        {
67
            throw new AnnotationException('The @' . $this->getType() .
68
                ' annotation requires a property "call" of type string');
69
        }
70
        foreach(array_keys($properties) as $propName)
71
        {
72
            if($propName !== 'call' && $propName !== 'with')
73
            {
74
                throw new AnnotationException('Unknown property "' . $propName .
75
                    '" in the @' . $this->getType() . ' annotation');
76
            }
77
        }
78
        // Cannot use isset here, because it will return false in case $properties['with'] === null
79
        if(array_key_exists('with', $properties))
80
        {
81
            if(!is_array($properties['with']))
82
            {
83
                throw new AnnotationException('The "with" property of the @' .
84
                    $this->getType() . ' annotation must be of type array');
85
            }
86
            $this->aParams = $properties['with'];
87
        }
88
        $this->sMethod = $properties['call'];
89
    }
90
}
91