Completed
Push — php7 ( 127d41 )
by personal
03:17
created

CallExtractor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 3 Features 0
Metric Value
wmc 8
c 3
b 3
f 0
lcom 1
cbo 1
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C extract() 0 30 7
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
        }
66
67
        if(null === $call) {
68
            throw new \LogicException('Classname of call not found');
69
        }
70
        return trim($call, '()'); // fixes PHP 5.4:    (new MyClass)->foo()
71
    }
72
};