1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
* |
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
14
|
|
|
* and is licensed under the MIT license. |
15
|
|
|
* |
16
|
|
|
* Copyright (c) 2015-2020 Yuuki Takezawa |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Ytake\LaravelAspect\Matcher; |
21
|
|
|
|
22
|
|
|
use Ray\Aop\AbstractMatcher; |
23
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
24
|
|
|
|
25
|
|
|
use function func_get_args; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class AnnotationScanMatcher |
29
|
|
|
*/ |
30
|
|
|
class AnnotationScanMatcher extends AbstractMatcher |
31
|
|
|
{ |
32
|
|
|
/** @var AnnotationReader */ |
33
|
|
|
private $reader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ScanMatcher constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
parent::__construct(); |
41
|
|
|
$this->arguments = func_get_args(); |
42
|
|
|
$this->reader = new AnnotationReader(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function matchesClass(\ReflectionClass $class, array $arguments) |
49
|
|
|
{ |
50
|
|
|
return $this->has($class, $arguments[0]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \ReflectionClass $class |
55
|
|
|
* @param $annotation |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
private function has(\ReflectionClass $class, $annotation): bool |
60
|
|
|
{ |
61
|
|
|
$count = 0; |
62
|
|
|
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
63
|
|
|
$match = $this->reader->getMethodAnnotation($reflectionMethod, $annotation); |
|
|
|
|
64
|
|
|
if ($match) { |
65
|
|
|
$count++; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
if ($count > 1) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \ReflectionMethod $method |
77
|
|
|
* @param array $arguments |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
* @throws \ReflectionException |
81
|
|
|
*/ |
82
|
|
|
public function matchesMethod(\ReflectionMethod $method, array $arguments): bool |
83
|
|
|
{ |
84
|
|
|
$class = new \ReflectionClass($method->class); |
85
|
|
|
|
86
|
|
|
return $this->has($class, $arguments[0]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.