HasScope::hasMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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
The expression return $methodReflection returns the type Mockery\MockInterface which is incompatible with the type-hinted return PHPStan\Reflection\MethodReflection.
Loading history...
74
    }
75
}