Completed
Push — master ( 02285f...915210 )
by personal
05:27
created

CallExtractor::extract()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 4 Features 0
Metric Value
c 5
b 4
f 0
dl 0
loc 42
rs 5.1234
cc 13
eloc 28
nc 11
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\OOP\Extractor;
11
use Hal\Component\OOP\Reflected\ReflectedClass;
12
use Hal\Component\Token\TokenCollection;
13
14
15
/**
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class CallExtractor implements ExtractorInterface {
19
20
    /**
21
     * @var Searcher
22
     */
23
    private $searcher;
24
25
    /**
26
     * @var ReflectedClass
27
     */
28
    private $currentClass;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param Searcher $searcher
34
     * @param ReflectedClass $currentClass
35
     */
36
    public function __construct(Searcher $searcher, ReflectedClass $currentClass = null)
37
    {
38
        $this->searcher = $searcher;
39
        $this->currentClass = $currentClass;
40
    }
41
42
    /**
43
     * Extract dependency from call
44
     *
45
     * @param $n
46
     * @param TokenCollection $tokens
47
     * @return string
48
     * @throws \LogicException
49
     */
50
    public function extract(&$n, TokenCollection $tokens)
51
    {
52
53
        $token = $tokens[$n];
54
        $call = null;
55
        switch($token->getType()) {
56
            case T_PAAMAYIM_NEKUDOTAYIM:
57
                $prev = $n - 1;
58
                $value = $this->searcher->getUnder(array('::'), $prev, $tokens);
59
60
                switch(true) {
61
                    case $value == 'parent' && $this->currentClass:
62
                        return $this->currentClass->getParent();
63
64
                    case $value == 'parent':
65
                        // we try to get the name of the parent class
66
                        $extendPosition = $this->searcher->getExtendPosition($tokens, $n);
67
                        return $this->searcher->getFollowingName($extendPosition, $tokens);
68
69
                    case ($value == 'static' ||$value === 'self')&& $this->currentClass:
70
                        return $this->currentClass->getFullname();
71
72
                    case ($value == 'static' ||$value === 'self'):
73
                        $extendPosition = $this->searcher->getClassNamePosition($tokens);
74
                        return $this->searcher->getFollowingName($extendPosition, $tokens);
75
76
                    default:
77
                        return $value;
78
                }
79
80
            case T_NEW:
81
                $call = $this->searcher->getFollowingName($n, $tokens);
82
                if(preg_match('!^(\w+)!', $call, $matches)) { // fixes PHP 5.4:    (new MyClass)->foo()
83
                    $call = $matches[1];
84
                }
85
                break;
86
        }
87
        if(null === $call) {
88
            throw new \LogicException('Classname of call not found');
89
        }
90
        return $call;
91
    }
92
};