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

CodeParser::parse()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 36
nc 12
nop 1
dl 0
loc 59
rs 7.5346
c 1
b 0
f 0

How to fix   Long Method   

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;
11
12
use Hal\Component\Parser\CodeParser\ClassParser;
13
use Hal\Component\Parser\CodeParser\MethodParser;
14
use Hal\Component\Parser\Resolver\NamespaceResolver;
15
use Hal\Component\Token\Token;
16
17
class CodeParser
18
{
19
20
    /**
21
     * @var Searcher
22
     */
23
    private $searcher;
24
25
    /**
26
     * @var NamespaceResolver
27
     */
28
    private $namespaceResolver;
29
30
    /**
31
     * CodeParser constructor.
32
     * @param Searcher $searcher
33
     * @param NamespaceResolver $namespaceResolver
34
     */
35
    public function __construct(Searcher $searcher, NamespaceResolver $namespaceResolver)
36
    {
37
        $this->searcher = $searcher;
38
        $this->namespaceResolver = $namespaceResolver;
39
    }
40
41
    /**
42
     * @param $tokens
43
     * @return Result
44
     */
45
    public function parse(array $tokens)
46
    {
47
48
        $classes = array();
49
        $functions = array();
50
        $anotherTokens = array();
51
52
        // store initial values
53
        $isAbstract = false;
54
55
        // we group tokens by class
56
        $len = sizeof($tokens);
57
        for ($i = 0; $i < $len; $i++) {
58
            $token = $tokens[$i];
59
60
            if (Token::T_ABSTRACT === $token) {
61
                $isAbstract = true;
62
                continue;
63
            }
64
65
            if (Token::T_CLASS === $token || Token::T_INTERFACE === $token) {
66
                $classStart = $i;
67
                $classEnd = $this->searcher->getPositionOfClosingBrace($tokens, $i);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $classEnd is correct as $this->searcher->getPosi...osingBrace($tokens, $i) (which targets Hal\Component\Parser\Sea...ositionOfClosingBrace()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
                // we need to start tokens from previous token because of php7 anonymous classes
69
                // example: "new class{"
70
                $tokensOfClass = array_slice($tokens, max(0, $classStart - 1), ($classEnd - $classStart) + 1);
71
72
                $parser = new ClassParser($this->searcher, $this->namespaceResolver);
73
                $class = $parser->parse($tokensOfClass);
74
                $class->setIsAbstract($isAbstract);
75
                array_push($classes, $class);
76
77
                $i = $classEnd;
78
                $isAbstract = false;
79
                continue;
80
            }
81
            array_push($anotherTokens, $tokens[$i]);
82
        }
83
84
        // another code (functions, direct calls...
85
        $len = sizeof($anotherTokens);
86
        for ($i = 0; $i < $len; $i++) {
87
            $token = $anotherTokens[$i];
88
            if (Token::T_FUNCTION === $token) {
89
                $functionStart = $i;
90
                $functionEnd = $this->searcher->getPositionOfClosingBrace($anotherTokens, $i);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $functionEnd is correct as $this->searcher->getPosi...ace($anotherTokens, $i) (which targets Hal\Component\Parser\Sea...ositionOfClosingBrace()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
                $parser = new MethodParser($this->searcher, $this->namespaceResolver);
92
                $function = $parser->parse(array_slice($anotherTokens, $functionStart, ($functionEnd - $functionStart) + 1));
93
                array_push($functions, $function);
94
95
                continue;
96
            }
97
        }
98
99
        $result = new Result;
100
        $result->setClasses($classes)->setFunctions($functions);
101
102
        return $result;
103
    }
104
}