|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* parser-utils.php. |
|
5
|
|
|
* |
|
6
|
|
|
* These are utility functions for the PHPSQLParser. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2010-2012, Justin Swanhart |
|
9
|
|
|
* with contributions by André Rothe <[email protected], [email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* All rights reserved. |
|
12
|
|
|
* |
|
13
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
|
14
|
|
|
* are permitted provided that the following conditions are met: |
|
15
|
|
|
* |
|
16
|
|
|
* * Redistributions of source code must retain the above copyright notice, |
|
17
|
|
|
* this list of conditions and the following disclaimer. |
|
18
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, |
|
19
|
|
|
* this list of conditions and the following disclaimer in the documentation |
|
20
|
|
|
* and/or other materials provided with the distribution. |
|
21
|
|
|
* |
|
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
|
23
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
24
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
|
25
|
|
|
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
26
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|
27
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|
28
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
29
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
30
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
31
|
|
|
* DAMAGE. |
|
32
|
|
|
*/ |
|
33
|
|
|
namespace SQLParser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This class implements some helper functions. |
|
37
|
|
|
* |
|
38
|
|
|
* @author arothe |
|
39
|
|
|
*/ |
|
40
|
|
|
class PHPSQLParserUtils extends PHPSQLParserConstants |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* Prints an array only if debug mode is on. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $s |
|
|
|
|
|
|
46
|
|
|
* @param bool $return, if true, the formatted array is returned via return parameter |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
protected function preprint($arr, $return = false) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$x = '<pre>'; |
|
51
|
|
|
$x .= print_r($arr, 1); |
|
52
|
|
|
$x .= '</pre>'; |
|
53
|
|
|
if ($return) { |
|
54
|
|
|
return $x; |
|
55
|
|
|
} else { |
|
56
|
|
|
if (isset($_ENV['DEBUG'])) { |
|
57
|
|
|
print $x."\n"; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Ends the given string $haystack with the string $needle? |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $haystack |
|
66
|
|
|
* @param string $needle |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
protected function endsWith($haystack, $needle) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$length = strlen($needle); |
|
71
|
|
|
if ($length == 0) { |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$start = $length * -1; |
|
76
|
|
|
|
|
77
|
|
|
return (substr($haystack, $start) === $needle); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Revokes the escaping characters from an expression. |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function revokeEscaping($sql) |
|
84
|
|
|
{ |
|
85
|
|
|
$result = trim($sql); |
|
86
|
|
|
if (($result[0] === '`') && ($result[strlen($result) - 1] === '`')) { |
|
87
|
|
|
$result = substr($result, 1, -1); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return str_replace('``', '`', $result); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* This method removes parenthesis from start of the given string. |
|
95
|
|
|
* It removes also the associated closing parenthesis. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function removeParenthesisFromStart($token) |
|
98
|
|
|
{ |
|
99
|
|
|
$parenthesisRemoved = 0; |
|
100
|
|
|
|
|
101
|
|
|
$trim = trim($token); |
|
102
|
|
|
if ($trim !== '' && $trim[0] === '(') { // remove only one parenthesis pair now! |
|
103
|
|
|
++$parenthesisRemoved; |
|
104
|
|
|
$trim[0] = ' '; |
|
105
|
|
|
$trim = trim($trim); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$parenthesis = $parenthesisRemoved; |
|
109
|
|
|
$i = 0; |
|
110
|
|
|
$string = 0; |
|
111
|
|
|
while ($i < strlen($trim)) { |
|
112
|
|
|
if ($trim[$i] === '\\') { |
|
113
|
|
|
$i += 2; # an escape character, the next character is irrelevant |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($trim[$i] === "'" || $trim[$i] === '"') { |
|
118
|
|
|
++$string; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (($string % 2 === 0) && ($trim[$i] === '(')) { |
|
122
|
|
|
++$parenthesis; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (($string % 2 === 0) && ($trim[$i] === ')')) { |
|
126
|
|
|
if ($parenthesis == $parenthesisRemoved) { |
|
127
|
|
|
$trim[$i] = ' '; |
|
128
|
|
|
--$parenthesisRemoved; |
|
129
|
|
|
} |
|
130
|
|
|
--$parenthesis; |
|
131
|
|
|
} |
|
132
|
|
|
++$i; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return trim($trim); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function getLastOf($array) |
|
139
|
|
|
{ |
|
140
|
|
|
// $array is a copy of the original array, so we can change it without sideeffects |
|
141
|
|
|
if (!is_array($array)) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return array_pop($array); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* translates an array of objects into an associative array. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function toArray($tokenList) |
|
152
|
|
|
{ |
|
153
|
|
|
$expr = array(); |
|
154
|
|
|
foreach ($tokenList as $token) { |
|
155
|
|
|
$expr[] = $token->toArray(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return (empty($expr) ? false : $expr); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.