Completed
Pull Request — master (#162)
by personal
05:46 queued 03:11
created

CallExtractor::extract()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 33
rs 5.3846
cc 8
eloc 26
nc 12
nop 2
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\Token\TokenCollection;
12
13
14
/**
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class CallExtractor implements ExtractorInterface {
18
19
    /**
20
     * @var Searcher
21
     */
22
    private $searcher;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param Searcher $searcher
28
     */
29
    public function __construct(Searcher $searcher)
30
    {
31
        $this->searcher = $searcher;
32
    }
33
34
    /**
35
     * Extract dependency from call
36
     *
37
     * @param $n
38
     * @param TokenCollection $tokens
39
     * @return string
40
     * @throws \LogicException
41
     */
42
    public function extract(&$n, TokenCollection $tokens)
43
    {
44
45
        $token = $tokens[$n];
46
        $call = null;
47
        switch($token->getType()) {
48
            case T_PAAMAYIM_NEKUDOTAYIM:
49
                $prev = $n - 1;
50
                $value = $this->searcher->getUnder(array('::'), $prev, $tokens);
51
                if ($value === 'parent') {
52
                    $extendPosition = $this->searcher->getExtendPostition($tokens);
53
                    $parentName = $this->searcher->getFollowingName($extendPosition, $tokens);
54
                    $call = $parentName;
55
                } else if ($value === 'self' || $value === 'static') {
56
                    $extendPosition = $this->searcher->getClassNamePosition($tokens);
57
                    $className = $this->searcher->getFollowingName($extendPosition, $tokens);
58
                    $call = $className;
59
                } else {
60
                    $call = $value;
61
                }
62
                break;
63
            case T_NEW:
64
                $call = $this->searcher->getFollowingName($n, $tokens);
65
                if(preg_match('!^(\w+)!', $call, $matches)) { // fixes PHP 5.4:    (new MyClass)->foo()
66
                    $call = $matches[1];
67
                }
68
                break;
69
        }
70
        if(null === $call) {
71
            throw new \LogicException('Classname of call not found');
72
        }
73
        return $call;
74
    }
75
};