Passed
Push — master ( 6b81ad...6f63e3 )
by Nuno
02:17
created

AbstractExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMethod() 0 6 2
A getMethod() 0 14 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\Extensions;
15
16
use Mockery;
17
use PHPStan\Reflection\ClassReflection;
18
use PHPStan\Reflection\MethodReflection;
19
use PHPStan\Reflection\BrokerAwareExtension;
20
use PHPStan\Reflection\MethodsClassReflectionExtension;
21
22
/**
23
 * @internal
24
 */
25
abstract class AbstractExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
26
{
27
    use Concerns\HasBroker;
28
29
    /**
30
     * Contains the Class Reflection.
31
     *
32
     * @var \PHPStan\Reflection\ClassReflection
33
     */
34
    protected $classReflection;
35
36
    /**
37
     * Returns the class under analyse.
38
     *
39
     * @return string
40
     */
41
    abstract protected function subject(): string;
42
43
    /**
44
     * Returns the class where the native method should be search for.
45
     *
46
     * @return string
47
     */
48
    abstract protected function searchIn(): string;
49
50
    /**
51
     * Whether the methods can be accessed statically
52
     */
53
    protected $staticAccess = false;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function hasMethod(ClassReflection $classReflection, string $methodName): bool
59
    {
60
        $this->classReflection = $classReflection;
61
62
        return $classReflection->isSubclassOf($this->subject()) && $this->broker->getClass($this->searchIn())
63
                ->hasNativeMethod($methodName);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
70
    {
71
        $this->classReflection = $classReflection;
72
73
        $methodReflection = $this->broker->getClass($this->searchIn())
74
            ->getNativeMethod($methodName);
75
76
        if ($this->staticAccess) {
77
            $methodReflection = Mockery::mock($methodReflection);
78
            $methodReflection->shouldReceive('isStatic')
79
                ->andReturn(true);
80
        }
81
82
        return $methodReflection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $methodReflection could return the type Mockery\MockInterface which is incompatible with the type-hinted return PHPStan\Reflection\MethodReflection. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
}
85