Completed
Pull Request — master (#221)
by personal
04:09
created

CallsParser::parse()   C

Complexity

Conditions 15
Paths 22

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 15
eloc 39
nc 22
nop 1
dl 0
loc 67
rs 5.7992
c 3
b 0
f 0

How to fix   Long Method    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\Parser\CodeParser;
11
12
use Hal\Component\Parser\Exception\IncorrectSyntaxException;
13
use Hal\Component\Reflected\Call;
14
use Hal\Component\Reflected\Klass;
15
use Hal\Component\Parser\Resolver\NamespaceResolver;
16
use Hal\Component\Parser\Searcher;
17
use Hal\Component\Token\Token;
18
19
class CallsParser
20
{
21
22
    /**
23
     * @var Searcher
24
     */
25
    private $searcher;
26
27
    /**
28
     * @var NamespaceResolver
29
     */
30
    private $namespaceResolver;
31
32
    /**
33
     * CodeParser constructor.
34
     * @param Searcher $searcher
35
     * @param NamespaceResolver $namespaceResolver
36
     */
37
    public function __construct(Searcher $searcher, NamespaceResolver $namespaceResolver)
38
    {
39
        $this->searcher = $searcher;
40
        $this->namespaceResolver = $namespaceResolver;
41
    }
42
43
    /**
44
     * @param $tokens
45
     * @return Klass[]
46
     */
47
    public function parse($tokens)
48
    {
49
        $calls = array();
50
        $len = sizeof($tokens);
51
52
        for ($i = 0; $i < $len; $i++) {
53
            $token = $tokens[$i];
54
55
            // T_PAAMAYIM_NEKUDOTAYIM
56
            if (preg_match('!(.*)::(.*)!', $token, $matches)) {
57
                list(, $className, $methodName) = $matches;
58
                $call = new Call($this->namespaceResolver->resolve($className), $methodName);
59
                $call->setIsStatic(true);
60
61
                // call on itself
62
                if (preg_match('!(self|static)!i', $className)) {
63
                    $call->setIsItself(true);
64
                }
65
66
                // call parent
67
                if (preg_match('!parent!i', $className)) {
68
                    $call->setIsParent(true);
69
                }
70
71
                array_push($calls, $call);
72
                continue;
73
            }
74
75
            // $instance->foo();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
            if (Token::T_NEW == $token) {
77
                if(!isset($tokens[$i + 1]) || Token::T_BRACE_CLOSE === $tokens[$i + 1]) {
78
                    throw new IncorrectSyntaxException('"new" is not followed by classname');
79
                }
80
81
                // followed by ")->"
82
                // (new Foo)->bar()
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
                $next = $tokens[$i + 1];
84
                $nextAfter = $i + 2 <= $len ? $tokens[$i + 2] : null;
85
                $nextAfterAfter = $i + 3 <= $len - 1 ? $tokens[$i + 3] : null;
86
                if (Token::T_PARENTHESIS_CLOSE === $nextAfter && preg_match('!^\-\>(.*)!', $nextAfterAfter, $matches)) {
87
                    $className = $next;
88
                    $methodName = $matches[1];
89
                    $call = new Call($this->namespaceResolver->resolve($className), $methodName);
90
                    array_push($calls, $call);
91
                    $i = $i + 2;
92
                    continue;
93
                }
94
95
                $className = $next;
96
                $methodName = 'unknown';
97
                $call = new Call($this->namespaceResolver->resolve($className), $methodName);
98
                array_push($calls, $call);
99
            }
100
101
            // $this->foo();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
            if(preg_match('!^\$this\->(.+)!', $token, $matches)) {
103
                list(, $methodName) = $matches;
104
                // next token should be "("
105
                if(isset($tokens[$i + 1]) &&Token::T_PARENTHESIS_OPEN === $tokens[$i + 1]) {
106
                    $call = new Call(null, $methodName);
107
                    $call->setIsItself(true);
108
                    array_push($calls, $call);
109
                }
110
            }
111
        }
112
        return $calls;
113
    }
114
}