HasScope   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 51
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMethod() 0 3 1
A getScopeMethodName() 0 3 1
A getMethod() 0 5 1
A getScopeMethodReflection() 0 16 1
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
}