1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
* |
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
13
|
|
|
* and is licensed under the MIT license. |
14
|
|
|
* |
15
|
|
|
* Copyright (c) 2015-2016 Yuuki Takezawa |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
namespace Ytake\LaravelAspect\Matcher; |
19
|
|
|
|
20
|
|
|
use Ray\Aop\AbstractMatcher; |
21
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class AnnotationScanMatcher |
25
|
|
|
*/ |
26
|
|
|
class AnnotationScanMatcher extends AbstractMatcher |
27
|
|
|
{ |
28
|
|
|
/** @var AnnotationReader */ |
29
|
|
|
private $reader; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ScanMatcher constructor. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
$this->arguments = func_get_args(); |
38
|
|
|
$this->reader = new AnnotationReader(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function matchesClass(\ReflectionClass $class, array $arguments) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$count = 0; |
47
|
|
|
$annotation = $arguments[0]; |
48
|
|
|
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
49
|
|
|
$match = $this->reader->getMethodAnnotation($reflectionMethod, $annotation); |
|
|
|
|
50
|
|
|
if ($match) { |
51
|
|
|
$count++; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
if ($count > 1) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function matchesMethod(\ReflectionMethod $method, array $arguments) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$count = 0; |
67
|
|
|
$annotation = $arguments[0]; |
68
|
|
|
$reflectionClass = new \ReflectionClass($method->class); |
69
|
|
|
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
70
|
|
|
$match = $this->reader->getMethodAnnotation($reflectionMethod, $annotation); |
|
|
|
|
71
|
|
|
if ($match) { |
72
|
|
|
$count++; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
if ($count > 1) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.