Completed
Push — master ( 7595d7...36684b )
by Alexander
04:15
created

FunctionPointcut::matches()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 4
nop 4
crap 30
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Aop\Pointcut;
13
14
use ReflectionFunction;
15
use Go\Aop\Pointcut;
16
use Go\Aop\PointFilter;
17
18
/**
19
 * Signature method pointcut checks method signature (modifiers and name) to match it
20
 */
21
class FunctionPointcut implements Pointcut
22
{
23
    /**
24
     * @var PointFilter
25
     */
26
    protected $nsFilter;
27
28
    /**
29
     * Function name to match, can contain wildcards *,?
30
     *
31
     * @var string
32
     */
33
    protected $functionName = '';
34
35
    /**
36
     * Regular expression for matching
37
     *
38
     * @var string
39
     */
40
    protected $regexp;
41
42
    /**
43
     * Additional return type filter (if present)
44
     *
45
     * @var PointFilter|null
46
     */
47
    protected $returnTypeFilter;
48
49
    /**
50
     * Function matcher constructor
51
     *
52
     * @param string $functionName Name of the function to match or glob pattern
53
     * @param PointFilter|null $returnTypeFilter Additional return type filter
54
     */
55 4
    public function __construct(string $functionName, PointFilter $returnTypeFilter = null)
56
    {
57 4
        $this->functionName     = $functionName;
58 4
        $this->returnTypeFilter = $returnTypeFilter;
59 4
        $this->regexp           = strtr(preg_quote($this->functionName, '/'), [
60 4
            '\\*' => '.*?',
61
            '\\?' => '.'
62
        ]);
63 4
    }
64
65
    /**
66
     * Performs matching of point of code
67
     *
68
     * @param mixed $function Specific part of code, can be any Reflection class
69
     * @param mixed $context Related context, can be class or namespace
70
     * @param null|string|object $instance Invocation instance or string for static calls
71
     * @param null|array $arguments Dynamic arguments for method
72
     *
73
     * @return bool
74
     */
75
    public function matches($function, $context = null, $instance = null, array $arguments = null): bool
76
    {
77
        if (!$function instanceof ReflectionFunction) {
78
            return false;
79
        }
80
81
        if (($this->returnTypeFilter !== null) && !$this->returnTypeFilter->matches($function, $context)) {
82
            return false;
83
        }
84
85
        return ($function->name === $this->functionName) || (bool) preg_match("/^{$this->regexp}$/", $function->name);
86
    }
87
88
    /**
89
     * Returns the kind of point filter
90
     */
91
    public function getKind(): int
92
    {
93
        return self::KIND_FUNCTION;
94
    }
95
96
    /**
97
     * Return the class filter for this pointcut.
98
     */
99
    public function getClassFilter(): PointFilter
100
    {
101
        return $this->nsFilter;
102
    }
103
104 4
    public function setNamespaceFilter($nsFilter)
105
    {
106 4
        $this->nsFilter = $nsFilter;
107 4
    }
108
}
109