Completed
Branch feature/goaop-parser (08838c)
by Alexander
03:32
created

AnnotationPointcut   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 43.75%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 95
ccs 7
cts 16
cp 0.4375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A matches() 0 11 2
A getKind() 0 4 1
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Aop\Pointcut;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Go\Aop\Pointcut;
15
use ReflectionClass;
16
use ReflectionMethod;
17
use ReflectionProperty;
18
19
/**
20
 * Annotation property pointcut checks property annotation
21
 */
22
class AnnotationPointcut implements Pointcut
23
{
24
    use PointcutClassFilterTrait;
25
    /**
26
     * Annotation class to match
27
     *
28
     * @var string
29
     */
30
    protected $annotationName = '';
31
32
    /**
33
     * Annotation reader
34
     *
35
     * @var null|Reader
36
     */
37
    protected $annotationReader = null;
38
39
    /**
40
     * Kind of current filter, can be KIND_CLASS, KIND_METHOD, KIND_PROPERTY, KIND_TRAIT
41
     *
42
     * @var int
43
     */
44
    protected $filterKind = 0;
45
46
    /**
47
     * Specifies name of the expected class to receive
48
     *
49
     * @var string
50
     */
51
    protected $expectedClass = '';
52
53
    /**
54
     * Method to call for annotation reader
55
     *
56
     * @var string
57
     */
58
    protected $annotationMethod = '';
59
60
    /**
61
     * Static mappings of kind to expected class and method name
62
     *
63
     * @var array
64
     */
65
    protected static $mappings = [
66
        self::KIND_CLASS    => [ReflectionClass::class, 'getClassAnnotation'],
67
        self::KIND_TRAIT    => [ReflectionClass::class, 'getClassAnnotation'],
68
        self::KIND_METHOD   => [ReflectionMethod::class, 'getMethodAnnotation'],
69
        self::KIND_PROPERTY => [ReflectionProperty::class, 'getPropertyAnnotation']
70
    ];
71
72
    /**
73
     * Annotation matcher constructor
74
     *
75
     * @param integer $filterKind Kind of filter, e.g. KIND_CLASS
76
     * @param Reader $reader Reader of annotations
77
     * @param string $annotationName Annotation class name to match
78
     */
79 3
    public function __construct($filterKind, Reader $reader, $annotationName)
80
    {
81 3
        if (!isset(self::$mappings[$filterKind])) {
82
            throw new \InvalidArgumentException("Unsupported filter kind {$filterKind}");
83
        }
84 3
        $this->filterKind       = $filterKind;
85 3
        $this->annotationName   = $annotationName;
86 3
        $this->annotationReader = $reader;
87
88 3
        list($this->expectedClass, $this->annotationMethod) = self::$mappings[$filterKind];
89 3
    }
90
91
    /**
92
     * @param ReflectionClass|ReflectionMethod|ReflectionProperty $point
93
     * {@inheritdoc}
94
     */
95
    public function matches($point)
96
    {
97
        $expectedClass = $this->expectedClass;
98
        if (!$point instanceof $expectedClass) {
99
            return false;
100
        }
101
102
        $annotation = $this->annotationReader->{$this->annotationMethod}($point, $this->annotationName);
103
104
        return (bool) $annotation;
105
    }
106
107
    /**
108
     * Returns the kind of point filter
109
     *
110
     * @return integer
111
     */
112
    public function getKind()
113
    {
114
        return $this->filterKind;
115
    }
116
}
117