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\Broker\Broker; |
18
|
|
|
use Illuminate\Database\Eloquent\Model; |
19
|
|
|
use PHPStan\Reflection\ClassReflection; |
20
|
|
|
use PHPStan\Reflection\MethodReflection; |
21
|
|
|
use PHPStan\Reflection\BrokerAwareExtension; |
22
|
|
|
use PHPStan\Reflection\MethodsClassReflectionExtension; |
23
|
|
|
|
24
|
|
|
abstract class AbstractBuilderMethodExtension implements MethodsClassReflectionExtension, BrokerAwareExtension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \PHPStan\Broker\Broker |
28
|
|
|
*/ |
29
|
|
|
private $broker; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \PHPStan\Broker\Broker $broker |
33
|
|
|
*/ |
34
|
|
|
public function setBroker(Broker $broker): void |
35
|
|
|
{ |
36
|
|
|
$this->broker = $broker; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the builder class. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract public function getBuilderClass(): string; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
50
|
|
|
{ |
51
|
|
|
return $classReflection->isSubclassOf(Model::class) && $this->broker->getClass($this->getBuilderClass()) |
52
|
|
|
->hasNativeMethod($methodName); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
59
|
|
|
{ |
60
|
|
|
$methodReflection = $this->broker->getClass($this->getBuilderClass()) |
61
|
|
|
->getNativeMethod($methodName); |
62
|
|
|
|
63
|
|
|
$mock = Mockery::mock($methodReflection); |
64
|
|
|
$mock->shouldReceive('isStatic') |
65
|
|
|
->andReturn(true); |
66
|
|
|
|
67
|
|
|
return $mock; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|