Completed
Push — master ( 802733...f10121 )
by Dan
03:16
created

JoinKeyword::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
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
    /**
67
     * Columns in Using clause
68
     *
69
     * @var ArrayObj
70
     */
71
    public $using;
72
73
    /**
74
     * @param Parser     $parser  The parser that serves as context.
75
     * @param TokensList $list    The list of tokens that are being parsed.
76
     * @param array      $options Parameters for parsing.
77
     *
78
     * @return JoinKeyword[]
79
     */
80 7
    public static function parse(Parser $parser, TokensList $list, array $options = array())
81
    {
82 7
        $ret = array();
83
84 7
        $expr = new JoinKeyword();
85
86
        /**
87
         * The state of the parser.
88
         *
89
         * Below are the states of the parser.
90
         *
91
         *      0 -----------------------[ JOIN ]----------------------> 1
92
         *
93
         *      1 -----------------------[ expr ]----------------------> 2
94
         *
95
         *      2 ------------------------[ ON ]-----------------------> 3
96
         *      2 -----------------------[ USING ]---------------------> 4
97
         *
98
         *      3 --------------------[ conditions ]-------------------> 0
99
         *
100
         *      4 ----------------------[ columns ]--------------------> 0
101
         *
102
         * @var int $state
103
         */
104 7
        $state = 0;
105
106
        // By design, the parser will parse first token after the keyword.
107
        // In this case, the keyword must be analyzed too, in order to determine
108
        // the type of this join.
109 7
        if ($list->idx > 0) {
110 4
            --$list->idx;
111 4
        }
112
113 7
        for (; $list->idx < $list->count; ++$list->idx) {
114
            /**
115
             * Token parsed at this moment.
116
             *
117
             * @var Token $token
118
             */
119 7
            $token = $list->tokens[$list->idx];
120
121
            // End of statement.
122 7
            if ($token->type === Token::TYPE_DELIMITER) {
123 6
                break;
124
            }
125
126
            // Skipping whitespaces and comments.
127 7
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
128 7
                continue;
129
            }
130
131 7
            if ($state === 0) {
132 7
                if (($token->type === Token::TYPE_KEYWORD)
133 7
                    && (!empty(static::$JOINS[$token->value]))
134 7
                ) {
135 7
                    $expr->type = static::$JOINS[$token->value];
136 7
                    $state = 1;
137 7
                } else {
138 1
                    break;
139
                }
140 7
            } elseif ($state === 1) {
141 7
                $expr->expr = Expression::parse($parser, $list, array('field' => 'table'));
142 7
                $state = 2;
143 7 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...
144 6
                if ($token->type === Token::TYPE_KEYWORD) {
145 6
                    if ($token->value === 'ON') {
146 5
                        $state = 3;
147 6
                    } elseif ($token->value === 'USING') {
148 1
                        $state = 4;
149 1
                    }
150 6
                }
151 6
            } elseif ($state === 3) {
152 5
                $expr->on = Condition::parse($parser, $list);
153 5
                $ret[] = $expr;
154 5
                $expr = new JoinKeyword();
155 5
                $state = 0;
156 6
            } elseif ($state === 4) {
157 1
                $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...
158 1
                $ret[] = $expr;
159 1
                $expr = new JoinKeyword();
160 1
                $state = 0;
161 1
            }
162
163 7
        }
164
165 7
        if (!empty($expr->type)) {
166 1
            $ret[] = $expr;
167 1
        }
168
169 7
        --$list->idx;
170 7
        return $ret;
171
    }
172
173
    /**
174
     * @param JoinKeyword[] $component The component to be built.
175
     * @param array         $options   Parameters for building.
176
     *
177
     * @return string
178
     */
179 2
    public static function build($component, array $options = array())
180
    {
181 2
        $ret = array();
182 2
        foreach ($component as $c) {
183 2
            $ret[] = array_search($c->type, static::$JOINS) . ' ' . $c->expr
184 2
                . (!empty($c->on)
185 2
                    ? ' ON ' . Condition::build($c->on)
186 2
                    : ' USING ' . ArrayObj::build($c->using));
187 2
        }
188 2
        return implode(' ', $ret);
189
    }
190
}
191