Completed
Pull Request — 1.x (#286)
by Alexander
02:43
created

SignaturePointcut::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
crap 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 Go\Aop\Pointcut;
14
use Go\Aop\PointFilter;
15
16
/**
17
 * Signature pointcut checks element signature (modifiers and name) to match it
18
 */
19
class SignaturePointcut implements Pointcut
20
{
21
    use PointcutClassFilterTrait;
22
23
    /**
24
     * Element name to match, can contain wildcards **,*,?,|
25
     *
26
     * @var string
27
     */
28
    protected $name = '';
29
30
    /**
31
     * Regular expression for pattern matching
32
     *
33
     * @var string
34
     */
35
    protected $regexp;
36
37
    /**
38
     * Modifier filter for element
39
     *
40
     * @var PointFilter
41
     */
42
    protected $modifierFilter;
43
44
    /**
45
     * Filter kind
46
     *
47
     * @var int
48
     */
49
    protected $filterKind = 0;
50
51
    /**
52
     * Signature matcher constructor
53
     *
54
     * @param integer $filterKind Kind of filter, e.g. KIND_CLASS
55
     * @param string $name Name of the entity to match or glob pattern
56
     * @param PointFilter $modifierFilter Method modifier filter
57
     */
58 24
    public function __construct($filterKind, $name, PointFilter $modifierFilter)
59
    {
60 24
        $this->filterKind = $filterKind;
61 24
        $this->name       = $name;
62 24
        $this->regexp     = strtr(preg_quote($this->name, '/'), array(
63 24
            '\\*'    => '[^\\\\]+?',
64
            '\\*\\*' => '.+?',
65
            '\\?'    => '.',
66
            '\\|'    => '|'
67
        ));
68 24
        $this->modifierFilter = $modifierFilter;
69 24
    }
70
71
    /**
72
     * Performs matching of point of code
73
     *
74
     * @param mixed $point Specific part of code, can be any Reflection class
75
     * @param null|mixed $context Related context, can be class or namespace
76
     * @param null|string|object $instance Invocation instance or string for static calls
77
     * @param null|array $arguments Dynamic arguments for method
78
     *
79
     * @return bool
80
     */
81 6
    public function matches($point, $context = null, $instance = null, array $arguments = null)
82
    {
83 6
        if (!$this->modifierFilter->matches($point, $context)) {
84 1
            return false;
85
        }
86
87 5
        return ($point->name === $this->name) || (bool) preg_match("/^(?:{$this->regexp})$/", $point->name);
88
    }
89
90
    /**
91
     * Returns the kind of point filter
92
     *
93
     * @return integer
94
     */
95 5
    public function getKind()
96
    {
97 5
        return $this->filterKind;
98
    }
99
}
100