1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Laravel Code Analyse. |
7
|
|
|
* |
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace NunoMaduro\LaravelCodeAnalyse\Extensions; |
15
|
|
|
|
16
|
|
|
use Mockery; |
17
|
|
|
use PHPStan\Reflection\ClassReflection; |
18
|
|
|
use PHPStan\Reflection\MethodReflection; |
19
|
|
|
use PHPStan\Reflection\BrokerAwareExtension; |
20
|
|
|
use PHPStan\Reflection\MethodsClassReflectionExtension; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractExtension implements MethodsClassReflectionExtension, BrokerAwareExtension |
26
|
|
|
{ |
27
|
|
|
use Concerns\HasBroker; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Contains the Class Reflection. |
31
|
|
|
* |
32
|
|
|
* @var \PHPStan\Reflection\ClassReflection |
33
|
|
|
*/ |
34
|
|
|
protected $classReflection; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the class under analyse. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
abstract protected function subject(): string; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the class where the native method should be search for. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
abstract protected function searchIn(): string; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Whether the methods can be accessed statically |
52
|
|
|
*/ |
53
|
|
|
protected $staticAccess = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
59
|
|
|
{ |
60
|
|
|
$this->classReflection = $classReflection; |
61
|
|
|
|
62
|
|
|
return $classReflection->isSubclassOf($this->subject()) && $this->broker->getClass($this->searchIn()) |
63
|
|
|
->hasNativeMethod($methodName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
70
|
|
|
{ |
71
|
|
|
$this->classReflection = $classReflection; |
72
|
|
|
|
73
|
|
|
$methodReflection = $this->broker->getClass($this->searchIn()) |
74
|
|
|
->getNativeMethod($methodName); |
75
|
|
|
|
76
|
|
|
if ($this->staticAccess) { |
77
|
|
|
$methodReflection = Mockery::mock($methodReflection); |
78
|
|
|
$methodReflection->shouldReceive('isStatic') |
79
|
|
|
->andReturn(true); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $methodReflection; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|