Completed
Pull Request — 2.x (#371)
by Alexander
02:19
created

FunctionPointcut   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 92
ccs 9
cts 19
cp 0.4737
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B matches() 0 12 5
A getKind() 0 4 1
A getClassFilter() 0 4 1
A setNamespaceFilter() 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 ReflectionFunction;
14
use Go\Aop\Pointcut;
15
use Go\Aop\PointFilter;
16
17
/**
18
 * Signature method pointcut checks method signature (modifiers and name) to match it
19
 */
20
class FunctionPointcut implements Pointcut
21
{
22
    /**
23
     * @var PointFilter
24
     */
25
    protected $nsFilter;
26
27
    /**
28
     * Function name to match, can contain wildcards *,?
29
     *
30
     * @var string
31
     */
32
    protected $functionName = '';
33
34
    /**
35
     * Regular expression for matching
36
     *
37
     * @var string
38
     */
39
    protected $regexp;
40
41
    /**
42
     * Additional return type filter (if present)
43
     *
44
     * @var PointFilter|null
45
     */
46
    protected $returnTypeFilter;
47
48
    /**
49
     * Function matcher constructor
50
     *
51
     * @param string $functionName Name of the function to match or glob pattern
52
     * @param PointFilter|null $returnTypeFilter Additional return type filter
53
     */
54 4
    public function __construct($functionName, PointFilter $returnTypeFilter = null)
55
    {
56 4
        $this->functionName     = $functionName;
57 4
        $this->returnTypeFilter = $returnTypeFilter;
58 4
        $this->regexp           = strtr(preg_quote($this->functionName, '/'), [
59 4
            '\\*' => '.*?',
60
            '\\?' => '.'
61
        ]);
62 4
    }
63
64
    /**
65
     * Performs matching of point of code
66
     *
67
     * @param mixed $function Specific part of code, can be any Reflection class
68
     * @param mixed $context Related context, can be class or namespace
69
     * @param null|string|object $instance Invocation instance or string for static calls
70
     * @param null|array $arguments Dynamic arguments for method
71
     *
72
     * @return bool
73
     */
74
    public function matches($function, $context = null, $instance = null, array $arguments = null)
75
    {
76
        if (!$function instanceof ReflectionFunction) {
77
            return false;
78
        }
79
80
        if (($this->returnTypeFilter !== null) && !$this->returnTypeFilter->matches($function, $context)) {
81
            return false;
82
        }
83
84
        return ($function->name === $this->functionName) || (bool) preg_match("/^{$this->regexp}$/", $function->name);
85
    }
86
87
    /**
88
     * Returns the kind of point filter
89
     *
90
     * @return integer
91
     */
92
    public function getKind()
93
    {
94
        return self::KIND_FUNCTION;
95
    }
96
97
    /**
98
     * Return the class filter for this pointcut.
99
     *
100
     * @return PointFilter
101
     */
102
    public function getClassFilter()
103
    {
104
        return $this->nsFilter;
105
    }
106
107 4
    public function setNamespaceFilter($nsFilter)
108
    {
109 4
        $this->nsFilter = $nsFilter;
110 4
    }
111
}
112