Completed
Push — master ( de8b3e...66b528 )
by Dan
03:08
created

Reference   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 10
c 7
b 2
f 0
lcom 1
cbo 7
dl 0
loc 133
ccs 38
cts 38
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B parse() 0 61 8
A build() 0 8 1
1
<?php
2
3
/**
4
 * `REFERENCES` keyword parser.
5
 *
6
 * @package    SqlParser
7
 * @subpackage Components
8
 */
9
namespace SqlParser\Components;
10
11
use SqlParser\Context;
12
use SqlParser\Component;
13
use SqlParser\Parser;
14
use SqlParser\Token;
15
use SqlParser\TokensList;
16
17
/**
18
 * `REFERENCES` keyword parser.
19
 *
20
 * @category   Keywords
21
 * @package    SqlParser
22
 * @subpackage Components
23
 * @author     Dan Ungureanu <[email protected]>
24
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
25
 */
26
class Reference extends Component
0 ignored issues
show
Coding Style introduced by
The property $REFERENCES_OPTIONS is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
27
{
28
29
    /**
30
     * All references options.
31
     *
32
     * @var array
33
     */
34
    public static $REFERENCES_OPTIONS = array(
35
        'MATCH'                         => array(1, 'var'),
36
        'ON DELETE'                     => array(2, 'var'),
37
        'ON UPDATE'                     => array(3, 'var'),
38
    );
39
40
    /**
41
     * The referenced table.
42
     *
43
     * @var Expression
44
     */
45
    public $table;
46
47
    /**
48
     * The referenced columns.
49
     *
50
     * @var array
51
     */
52
    public $columns;
53
54
    /**
55
     * The options of the referencing.
56
     *
57
     * @var OptionsArray
58
     */
59
    public $options;
60
61
    /**
62
     * Constructor.
63
     *
64
     * @param Expression   $table   The name of the table referenced.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $table not be Expression|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param array        $columns The columns referenced.
66
     * @param OptionsArray $options The options.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be OptionsArray|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
     */
68 7
    public function __construct($table = null, array $columns = array(), $options = null)
69
    {
70 7
        $this->table = $table;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
71 7
        $this->columns = $columns;
72 7
        $this->options = $options;
73 7
    }
74
75
    /**
76
     * @param Parser     $parser  The parser that serves as context.
77
     * @param TokensList $list    The list of tokens that are being parsed.
78
     * @param array      $options Parameters for parsing.
79
     *
80
     * @return Reference
81
     */
82 6
    public static function parse(Parser $parser, TokensList $list, array $options = array())
83
    {
84 6
        $ret = new Reference();
85
86
        /**
87
         * The state of the parser.
88
         *
89
         * Below are the states of the parser.
90
         *
91
         *      0 ----------------------[ table ]---------------------> 1
92
         *
93
         *      1 ---------------------[ columns ]--------------------> 2
94
         *
95
         *      2 ---------------------[ options ]--------------------> (END)
96
         *
97
         * @var int $state
98
         */
99 6
        $state = 0;
100
101 6
        for (; $list->idx < $list->count; ++$list->idx) {
102
            /**
103
             * Token parsed at this moment.
104
             *
105
             * @var Token $token
106
             */
107 6
            $token = $list->tokens[$list->idx];
108
109
            // End of statement.
110 6
            if ($token->type === Token::TYPE_DELIMITER) {
111 1
                break;
112
            }
113
114
            // Skipping whitespaces and comments.
115 6
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
116 5
                continue;
117
            }
118
119 6
            if ($state === 0) {
120 6
                $ret->table = Expression::parse(
121 6
                    $parser,
122 6
                    $list,
123
                    array(
124 6
                        'parseField' => 'table',
125 6
                        'breakOnAlias' => true,
126
                    )
127 6
                );
128 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...
129 6
            } elseif ($state === 1) {
130 6
                $ret->columns = ArrayObj::parse($parser, $list)->values;
131 6
                $state = 2;
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...
132 6
            } elseif ($state === 2) {
133 5
                $ret->options = OptionsArray::parse($parser, $list, static::$REFERENCES_OPTIONS);
134 5
                ++$list->idx;
135 5
                break;
136
            }
137
138 6
        }
139
140 6
        --$list->idx;
141 6
        return $ret;
142
    }
143
144
    /**
145
     * @param Reference $component The component to be built.
146
     * @param array     $options   Parameters for building.
147
     *
148
     * @return string
149
     */
150 2
    public static function build($component, array $options = array())
151
    {
152 2
        return trim(
153 2
            $component->table
154 2
            . ' (' . implode(', ', Context::escape($component->columns)) . ') '
155 2
            . $component->options
156 2
        );
157
    }
158
}
159