Expression   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 146
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 70.4%

Importance

Changes 0
Metric Value
wmc 30
c 0
b 0
f 0
lcom 1
cbo 14
dl 11
loc 146
ccs 69
cts 98
cp 0.704
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F parse() 11 119 30

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
9
namespace Zicht\Tool\Script\Parser;
10
11
use Zicht\Tool\Script\Token;
12
use Zicht\Tool\Script\Node;
13
use Zicht\Tool\Script\Node\Expr\Op;
14
use Zicht\Tool\Script\AbstractParser;
15
use Zicht\Tool\Script\TokenStream;
16
17
/**
18
 * Expression parser
19
 */
20
class Expression extends AbstractParser
21
{
22
    /**
23
     * Unary prefix operators
24
     *
25
     * @var array
26
     */
27
    public static $PREFIX_UNARY = array('!', '-', '~');
28
29
    /**
30
     * Binary infix operators
31
     *
32
     * @var array
33
     */
34
    public static $INFIX_BINARY = array(
35
        '==', '!=', '<=', '>=', '<', '>', '&&', '||', '=~',
36
        '+', '*', '/', '-'
37
    );
38
39
40
    /**
41
     * Does a recursive descent parsing of the token stream and returns the resulting node.
42
     *
43
     * @param \Zicht\Tool\Script\TokenStream $stream
44
     * @return \Zicht\Tool\Script\Node\Node
45
     */
46 12
    public function parse(TokenStream $stream)
47
    {
48 12
        if ($stream->match(Token::OPERATOR, array('!', '-'))) {
0 ignored issues
show
Documentation introduced by
array('!', '-') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
            $value = $stream->current()->value;
50
            $stream->next();
51
            $ret = new Op\Unary($value, $this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\E...Op\Unary::__construct() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52 12
        } elseif ($stream->match(Token::KEYWORD, array('true', 'false'))) {
0 ignored issues
show
Documentation introduced by
array('true', 'false') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
            $ret = new Node\Expr\Literal($stream->current()->value === 'true');
54
            $stream->next();
55 12
        } elseif ($stream->match(Token::KEYWORD, 'null')) {
56
            $ret = new Node\Expr\Literal(null);
57
            $stream->next();
58 12
        } elseif ($stream->match(Token::IDENTIFIER)) {
59 9
            $name = $stream->current()->value;
60 9
            $stream->next();
61
62 9
            $ret = new Node\Expr\Variable($name);
63 12
        } elseif ($stream->match(Token::STRING)) {
64 2
            $ret = new Node\Expr\Str($stream->current()->value);
65 2
            $stream->next();
66 5
        } elseif ($stream->match(Token::NUMBER)) {
67 4
            $value = $stream->current()->value;
68 4
            if (strpos('.', $value) !== false) {
69
                $ret = new Node\Expr\Number((float)$value);
70
            } else {
71 4
                $ret = new Node\Expr\Number((int)$value);
72
            }
73 4
            $stream->next();
74 4
        } elseif ($stream->match(Token::OPERATOR, '(')) {
75
            $stream->next();
76
            $ret = new Node\Expr\Parens($this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\Expr\Parens::__construct() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77
            $stream->expect(Token::OPERATOR, ')');
78
        } elseif ($stream->match(Token::OPERATOR, '[')) {
79
            $stream->next();
80
            $ret = new Node\Expr\ListNode();
81
            if (!$stream->match(Token::OPERATOR, ']')) {
82
                $ret->append($this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\Branch::append() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
                while ($stream->match(',')) {
84
                    $stream->next();
85
                    $ret->append($this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\Branch::append() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
                }
87
            }
88
            $stream->expect(Token::OPERATOR, ']');
89
        } else {
90
            $this->err($stream);
91
            return null;
92
        }
93
94 12
        if ($stream->valid()) {
95 11
            while ($stream->valid() && $stream->match(Token::OPERATOR, array('(', '.', '['))) {
0 ignored issues
show
Documentation introduced by
array('(', '.', '[') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96 6
                $type = $stream->current();
97
98 6
                $stream->next();
99 6
                if ($type->value === '(') {
100 3
                    $ret = new Node\Expr\Call($ret);
101 3
                    if (!$stream->match(Token::OPERATOR, ')')) {
102
                        do {
103 2
                            $arg = $this->parse($stream);
104 2
                            $ret->append($arg);
0 ignored issues
show
Bug introduced by
It seems like $arg defined by $this->parse($stream) on line 103 can also be of type null; however, Zicht\Tool\Script\Node\Branch::append() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
105
106 2
                            if ($stream->match(',')) {
107 1
                                $stream->next();
108 1
                            } else {
109 2
                                break;
110
                            }
111 1
                        } while (true);
112 2
                    }
113 3
                    $stream->expect(Token::OPERATOR, ')');
114 3
                } else {
115 3
                    $ret = new Node\Expr\Subscript($ret);
116
117 3
                    if ($type->value === '.') {
118 1
                        $token = $stream->expect(Token::IDENTIFIER);
119 1
                        $ret->append(new Node\Expr\Str($token->value));
120 1
                    } else {
121 2
                        $ret->append($this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\Branch::append() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
122
                    }
123
124 3
                    switch ($type->value) {
125 3
                        case '[':
126 2
                            $stream->expect(Token::OPERATOR, ']');
127 2
                            break;
128 3
                    }
129
                }
130 6
            }
131 11
        }
132
133 12
        if ($stream->valid()) {
134 9
            if ($stream->match(Token::OPERATOR, self::$INFIX_BINARY) || $stream->match(Token::KEYWORD, 'in')) {
0 ignored issues
show
Documentation introduced by
self::$INFIX_BINARY is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
                $value = $stream->current()->value;
136
                $stream->next();
137
                $ret = new Op\Binary($value, $ret, $this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\E...p\Binary::__construct() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
138 9
            } elseif ($stream->match(Token::OPERATOR, '?')) {
139 3
                $stream->next();
140 3 View Code Duplication
                if ($stream->match(Token::OPERATOR, ':')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141 1
                    $then = null;
142 1
                } else {
143 2
                    $then = $this->parse($stream);
144
                }
145
146 3 View Code Duplication
                if ($stream->valid() && $stream->match(Token::OPERATOR, ':')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147 2
                    $stream->next();
148 2
                    $else = $this->parse($stream);
149 2
                } else {
150 1
                    $else = null;
151
                }
152 3
                $ret = new Op\Ternary('?', $ret, $then, $else);
0 ignored issues
show
Bug introduced by
It seems like $then can also be of type null; however, Zicht\Tool\Script\Node\E...\Ternary::__construct() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $else can also be of type null; however, Zicht\Tool\Script\Node\E...\Ternary::__construct() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
153 3
            }
154 9
        }
155
156
        // little syntactic sugar for function calls without parentheses:
157 12
        $allowInlineCallTokens = array(Token::IDENTIFIER, Token::STRING, Token::NUMBER);
158 12
        if ($stream->valid() && ($stream->match($allowInlineCallTokens))) {
0 ignored issues
show
Documentation introduced by
$allowInlineCallTokens is of type array<integer,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159 1
            $ret = new Node\Expr\Call($ret);
160 1
            $ret->append($this->parse($stream));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($stream) targeting Zicht\Tool\Script\Parser\Expression::parse() can also be of type null; however, Zicht\Tool\Script\Node\Branch::append() does only seem to accept object<Zicht\Tool\Script\Node\Node>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
161 1
        }
162
163 12
        return $ret;
164
    }
165
}
166