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\Concerns; |
15
|
|
|
|
16
|
|
|
use Mockery; |
17
|
|
|
use PHPStan\Reflection\ClassReflection; |
18
|
|
|
use PHPStan\Reflection\MethodReflection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @internal |
22
|
|
|
*/ |
23
|
|
|
trait HasScope |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
29
|
|
|
{ |
30
|
|
|
return parent::hasMethod($classReflection, $this->getScopeMethodName($methodName)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
37
|
|
|
{ |
38
|
|
|
$methodReflection = parent::getMethod($classReflection, $this->getScopeMethodName($methodName)); |
39
|
|
|
|
40
|
|
|
return $this->getScopeMethodReflection($methodReflection, $methodName); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $originalMethod |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getScopeMethodName(string $originalMethod): string |
49
|
|
|
{ |
50
|
|
|
return 'scope'.ucfirst($originalMethod);; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \PHPStan\Reflection\MethodReflection $methodReflection |
55
|
|
|
* |
56
|
|
|
* @return \PHPStan\Reflection\MethodReflection |
57
|
|
|
*/ |
58
|
|
|
public function getScopeMethodReflection(MethodReflection $methodReflection): MethodReflection |
59
|
|
|
{ |
60
|
|
|
/** @var \PHPStan\Reflection\FunctionVariantWithPhpDocs $variant */ |
61
|
|
|
$variant = $methodReflection->getVariants()[0]; |
62
|
|
|
$parameters = $variant->getParameters(); |
63
|
|
|
unset($parameters[0]); // The query argument. |
64
|
|
|
|
65
|
|
|
$variant = Mockery::mock($variant); |
66
|
|
|
$variant->shouldReceive('getParameters') |
67
|
|
|
->andReturn($parameters); |
68
|
|
|
|
69
|
|
|
$methodReflection->shouldReceive('getVariants') |
|
|
|
|
70
|
|
|
->andReturn([$variant]); |
71
|
|
|
|
72
|
|
|
return $methodReflection; |
73
|
|
|
} |
74
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.