Completed
Push — master ( 4e8869...451c37 )
by personal
15:58 queued 10:08
created

CallExtractor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
        switch($token->getType()) {
47
            case T_PAAMAYIM_NEKUDOTAYIM:
48
                $prev = $n - 1;
49
                $value = $this->searcher->getUnder(array('::'), $prev, $tokens);
50
                if ($value === 'parent') {
51
                    $extendPosition = $this->searcher->getExtendPostition($tokens);
52
                    $parentName = $this->searcher->getFollowingName($extendPosition, $tokens);
53
                    return $parentName;
54
                }
55
                if ($value === 'self') {
56
                    $extendPosition = $this->searcher->getClassNamePosition($tokens);
57
                    $className = $this->searcher->getFollowingName($extendPosition, $tokens);
58
                    return $className;
59
                }
60
                return $value;
61
            case T_NEW:
62
                return $this->searcher->getFollowingName($n, $tokens);
63
        }
64
        throw new \LogicException('Classname of call not found');
65
    }
66
};