nunomaduro /
laravel-code-analyse
| 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); |
||
| 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 | /** @var \Mockery\MockInterface $methodReflection */ |
||
| 70 | $methodReflection->shouldReceive('getVariants') |
||
| 71 | ->andReturn([$variant]); |
||
| 72 | |||
| 73 | return $methodReflection; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 74 | } |
||
| 75 | } |