Completed
Pull Request — master (#298)
by Alexander
04:10
created

FunctionPointcut   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 79
ccs 7
cts 15
cp 0.4667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A matches() 0 8 3
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
     * Function matcher constructor
43
     *
44
     * @param string $functionName Name of the function to match or glob pattern
45
     */
46
    public function __construct($functionName)
47 3
    {
48
        $this->functionName = $functionName;
49 3
        $this->regexp       = strtr(preg_quote($this->functionName, '/'), array(
50 3
            '\\*' => '.*?',
51 3
            '\\?' => '.'
52
        ));
53
    }
54 3
55
    /**
56
     * Performs matching of point of code
57
     *
58
     * @param mixed $function Specific part of code, can be any Reflection class
59
     * @param mixed $context Related context, can be class or namespace
60
     * @param null|string|object $instance Invocation instance or string for static calls
61
     * @param null|array $arguments Dynamic arguments for method
62
     *
63
     * @return bool
64
     */
65
    public function matches($function, $context = null, $instance = null, array $arguments = null)
66
    {
67
        if (!$function instanceof ReflectionFunction) {
68
            return false;
69
        }
70
71
        return ($function->name === $this->functionName) || (bool) preg_match("/^{$this->regexp}$/", $function->name);
72
    }
73
74
    /**
75
     * Returns the kind of point filter
76
     *
77
     * @return integer
78
     */
79
    public function getKind()
80
    {
81
        return self::KIND_FUNCTION;
82
    }
83
84
    /**
85
     * Return the class filter for this pointcut.
86
     *
87
     * @return PointFilter
88
     */
89
    public function getClassFilter()
90
    {
91
        return $this->nsFilter;
92
    }
93
94
    public function setNamespaceFilter($nsFilter)
95
    {
96 3
        $this->nsFilter = $nsFilter;
97
    }
98
}
99