Completed
Push — master ( 91343d...22debc )
by Madhura
03:18
created

JoinKeyword   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 165
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 87.93%

Importance

Changes 11
Bugs 3 Features 2
Metric Value
wmc 20
c 11
b 3
f 2
lcom 1
cbo 6
dl 9
loc 165
ccs 51
cts 58
cp 0.8793
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D parse() 9 92 17
A build() 0 11 3

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
/**
4
 * `JOIN` keyword parser.
5
 *
6
 * @package    SqlParser
7
 * @subpackage Components
8
 */
9
namespace SqlParser\Components;
10
11
use SqlParser\Component;
12
use SqlParser\Parser;
13
use SqlParser\Token;
14
use SqlParser\TokensList;
15
16
/**
17
 * `JOIN` keyword parser.
18
 *
19
 * @category   Keywords
20
 * @package    SqlParser
21
 * @subpackage Components
22
 * @author     Dan Ungureanu <[email protected]>
23
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
24
 */
25
class JoinKeyword extends Component
26
{
27
28
    /**
29
     * Types of join.
30
     *
31
     * @var array
32
     */
33
    public static $JOINS = array(
34
        'FULL JOIN'                     => 'FULL',
35
        'INNER JOIN'                    => 'INNER',
36
        'JOIN'                          => 'JOIN',
37
        'LEFT JOIN'                     => 'LEFT',
38
        'LEFT OUTER JOIN'               => 'LEFT',
39
        'RIGHT JOIN'                    => 'RIGHT',
40
        'RIGHT OUTER JOIN'              => 'RIGHT',
41
        'STRAIGHT_JOIN'                 => 'STRAIGHT',
42
    );
43
44
    /**
45
     * Type of this join.
46
     *
47
     * @see static::$JOINS
48
     * @var string
49
     */
50
    public $type;
51
52
    /**
53
     * Join expression.
54
     *
55
     * @var Expression
56
     */
57
    public $expr;
58
59
    /**
60
     * Join conditions.
61
     *
62
     * @var Condition[]
63
     */
64
    public $on;
65
    /**
66
     * Columns in Using clause
67
     *
68
     * @var ArrayObj
69
     */
70
    public $using;
71
72
    /**
73
     * @param Parser     $parser  The parser that serves as context.
74
     * @param TokensList $list    The list of tokens that are being parsed.
75
     * @param array      $options Parameters for parsing.
76
     *
77
     * @return JoinKeyword[]
78
     */
79 6
    public static function parse(Parser $parser, TokensList $list, array $options = array())
80
    {
81 6
        $ret = array();
82
83 6
        $expr = new JoinKeyword();
84
85
        /**
86
         * The state of the parser.
87
         *
88
         * Below are the states of the parser.
89
         *
90
         *      0 -----------------------[ JOIN ]----------------------> 1
91
         *
92
         *      1 -----------------------[ expr ]----------------------> 2
93
         *
94
         *      2 ------------------------[ ON ]-----------------------> 3
95
         *      2 -----------------------[ USING ]---------------------> 4
96
         *
97
         *      3 --------------------[ conditions ]-------------------> 0
98
         *
99
         *      4 ----------------------[ columns ]--------------------> 0
100
         *
101
         * @var int $state
102
         */
103 6
        $state = 0;
104
105
        // By design, the parser will parse first token after the keyword.
106
        // In this case, the keyword must be analyzed too, in order to determine
107
        // the type of this join.
108 6
        if ($list->idx > 0) {
109 4
            --$list->idx;
110 4
        }
111
112 6
        for (; $list->idx < $list->count; ++$list->idx) {
113
            /**
114
             * Token parsed at this moment.
115
             *
116
             * @var Token $token
117
             */
118 6
            $token = $list->tokens[$list->idx];
119
120
            // End of statement.
121 6
            if ($token->type === Token::TYPE_DELIMITER) {
122 5
                break;
123
            }
124
125
            // Skipping whitespaces and comments.
126 6
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
127 6
                continue;
128
            }
129
130 6
            if ($state === 0) {
131 6
                if (($token->type === Token::TYPE_KEYWORD)
132 6
                    && (!empty(static::$JOINS[$token->value]))
133 6
                ) {
134 6
                    $expr->type = static::$JOINS[$token->value];
135 6
                    $state = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
136 6
                } else {
137 1
                    break;
138
                }
139 6
            } elseif ($state === 1) {
140 6
                $expr->expr = Expression::parse($parser, $list, array('field' => 'table'));
141 6
                $state = 2;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
142 6 View Code Duplication
            } elseif ($state === 2) {
1 ignored issue
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...
143 5
                if ($token->type === Token::TYPE_KEYWORD) {
144 5
                    if ($token->value === 'ON') {
145 5
                        $state = 3;
146 5
                    } elseif ($token->value === 'USING') {
147
                        $state = 4;
148
                    }
149 5
                }
150 5
            } elseif ($state === 3) {
151 5
                $expr->on = Condition::parse($parser, $list);
152 5
                $ret[] = $expr;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
153 5
                $expr = new JoinKeyword();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
154 5
                $state = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
155 5
            } elseif ($state === 4) {
156
                $expr->using = ArrayObj::parse($parser, $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like \SqlParser\Components\Ar...::parse($parser, $list) can also be of type array. However, the property $using is declared as type object<SqlParser\Components\ArrayObj>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
157
                $ret[] = $expr;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
158
                $expr = new JoinKeyword();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
159
                $state = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
160
            }
161
162 6
        }
163
164 6
        if (!empty($expr->type)) {
165 1
            $ret[] = $expr;
166 1
        }
167
168 6
        --$list->idx;
169 6
        return $ret;
170
    }
171
172
    /**
173
     * @param JoinKeyword[] $component The component to be built.
174
     * @param array         $options   Parameters for building.
175
     *
176
     * @return string
177
     */
178 2
    public static function build($component, array $options = array())
179
    {
180 2
        $ret = array();
181 2
        foreach ($component as $c) {
182 2
            $ret[] = array_search($c->type, static::$JOINS) . ' ' . $c->expr
183 2
                . (! empty($c->on)
184 2
                    ? ' ON ' . Condition::build($c->on)
185 2
                    : ' USING ' . ArrayObj::build($c->using));
186 2
        }
187 2
        return implode(' ', $ret);
188
    }
189
}
190