Passed
Push — master ( 71f272...88fb30 )
by Nuno
02:24
created

HasScope::hasMethod()   A

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, $methodName);
0 ignored issues
show
Unused Code introduced by
The call to NunoMaduro\LaravelCodeAn...ScopeMethodReflection() has too many arguments starting with $methodName. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return $this->/** @scrutinizer ignore-call */ getScopeMethodReflection($methodReflection, $methodName);

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.

Loading history...
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')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on PHPStan\Reflection\MethodReflection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        $methodReflection->/** @scrutinizer ignore-call */ 
70
                           shouldReceive('getVariants')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            ->andReturn([$variant]);
71
72
        return $methodReflection;
73
    }
74
}