PluginProbe
Code Snippets / 2.14.1
Code Snippets v2.14.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / class-validator.php

class-validator.php in Code Snippets 2.14.1, at php/class-validator.php

248 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Validates code prior to execution.
5 *
6 * @package Code_Snippets
7 */
8 class Code_Snippets_Validator {
9
10 /**
11 * @var string
12 */
13 private $code;
14
15 /**
16 * @var array
17 */
18 private $tokens;
19
20 /**
21 * The index of the token currently being examined.
22 *
23 * @var int
24 */
25 private $current;
26
27 /**
28 * The total number of tokens.
29 *
30 * @var int
31 */
32 private $length;
33
34 /**
35 * Array to keep track of the various function, class and interface identifiers which have been defined.
36 *
37 * @var array
38 */
39 private $defined_identifiers = array();
40
41 /**
42 * Exclude certain tokens from being checked.
43 *
44 * @var array
45 */
46 private $exceptions = array();
47
48 /**
49 * Class constructor.
50 *
51 * @param string $code Snippet code for parsing.
52 */
53 public function __construct( $code ) {
54 $this->code = $code;
55 $this->tokens = token_get_all( "<?php\n" . $this->code );
56 $this->length = count( $this->tokens );
57 $this->current = 0;
58 }
59
60 /**
61 * Determine whether the parser has reached the end of the list of tokens.
62 *
63 * @return bool
64 */
65 private function end() {
66 return $this->current === $this->length;
67 }
68
69 /**
70 * Retrieve the next token without moving the pointer
71 *
72 * @return string|array|null The current token if the list has not been expended, null otherwise.
73 */
74 private function peek() {
75 return $this->end() ? null : $this->tokens[ $this->current ];
76 }
77
78 /**
79 * Move the pointer to the next token, if there is one
80 *
81 * If the first argument is provided, only move the pointer if the tokens match
82 *
83 * @return bool Whether the pointer was advanced.
84 */
85 private function next() {
86 if ( $this->end() ) {
87 return false;
88 }
89
90 $this->current++;
91 return true;
92 }
93
94 /**
95 * Check whether a particular identifier has been used previously.
96 *
97 * @param string $type Which type of identifier this is. Supports T_FUNCTION, T_CLASS and T_INTERFACE.
98 * @param string $identifier The name of the identifier itself.
99 *
100 * @return bool true if the identifier is not unique.
101 */
102 private function check_duplicate_identifier( $type, $identifier ) {
103
104 if ( ! isset( $this->defined_identifiers[ $type ] ) ) {
105 switch ( $type ) {
106 case T_FUNCTION:
107 $defined_functions = get_defined_functions();
108 $this->defined_identifiers[ T_FUNCTION ] = array_merge( $defined_functions['internal'], $defined_functions['user'] );
109 break;
110
111 case T_CLASS:
112 $this->defined_identifiers[ T_CLASS ] = get_declared_classes();
113 break;
114
115 case T_INTERFACE:
116 $this->defined_identifiers[ T_INTERFACE ] = get_declared_interfaces();
117 break;
118
119 default:
120 return false;
121 }
122 }
123
124 $duplicate = in_array( $identifier, $this->defined_identifiers[ $type ], true );
125 array_unshift( $this->defined_identifiers[ $type ], $identifier );
126 return $duplicate && ! ( isset( $this->exceptions[ $type ] ) && in_array( $identifier, $this->exceptions[ $type ], true ) );
127 }
128
129 /**
130 * Validate the given PHP code and return the result.
131 * @return array|bool Array containing message if an error was encountered, false if validation was successful.
132 */
133 public function validate() {
134
135 while ( ! $this->end() ) {
136 $token = $this->peek();
137 $this->next();
138
139 if ( ! is_array( $token ) ) {
140 continue;
141 }
142
143 // if this is a function or class exists check, then allow this function or class to be defined
144 if ( T_STRING === $token[0] && 'function_exists' === $token[1] || 'class_exists' === $token[1] ) {
145 $type = 'function_exists' === $token[1] ? T_FUNCTION : T_CLASS;
146
147 // eat tokens until we find the function or class name
148 while ( ! $this->end() && T_CONSTANT_ENCAPSED_STRING !== $token[0] ) {
149 $token = $this->peek();
150 $this->next();
151 }
152
153 // add the identifier to the list of exceptions
154 $this->exceptions[ $type ] = isset( $this->exceptions[ $type ] ) ? $this->exceptions[ $type ] : array();
155 $this->exceptions[ $type ][] = trim( $token[1], '\'"' );
156 continue;
157 }
158
159 // only look for class and function declaration tokens
160 if ( T_CLASS !== $token[0] && T_FUNCTION !== $token[0] ) {
161 continue;
162 }
163
164 $structure_type = $token[0];
165
166 // continue eating tokens until we find the name of the class or function
167 while ( ! $this->end() && T_STRING !== $token[0] &&
168 ( T_FUNCTION !== $structure_type || '(' !== $token ) && ( T_CLASS !== $structure_type || '{' !== $token ) ) {
169 $token = $this->peek();
170 $this->next();
171 }
172
173 // if we've eaten all of the tokens without discovering a name, then there must be a syntax error, so return appropriately
174 if ( $this->end() ) {
175 return array(
176 'message' => __( 'Parse error: syntax error, unexpected end of snippet.', 'code-snippets' ),
177 'line' => $token[2],
178 );
179 }
180
181 // if the function or class is anonymous, with no name, then no need to check
182 if ( ! ( T_FUNCTION === $structure_type && '(' === $token ) && ! ( T_CLASS === $structure_type && '{' === $token ) ) {
183
184 // check whether the name has already been defined
185 if ( $this->check_duplicate_identifier( $structure_type, $token[1] ) ) {
186 switch ( $structure_type ) {
187 case T_FUNCTION:
188 /* translators: %s: PHP function name */
189 $message = __( 'Cannot redeclare function %s.', 'code-snippets' );
190 break;
191 case T_CLASS:
192 /* translators: %s: PHP class name */
193 $message = __( 'Cannot redeclare class %s.', 'code-snippets' );
194 break;
195 case T_INTERFACE:
196 /* translators: %s: PHP interface name */
197 $message = __( 'Cannot redeclare interface %s.', 'code-snippets' );
198 break;
199 default:
200 /* translators: %s: PHP identifier name*/
201 $message = __( 'Cannot redeclare %s.', 'code-snippets' );
202 }
203
204 return array(
205 'message' => sprintf( $message, $token[1] ),
206 'line' => $token[2],
207 );
208 }
209 }
210
211 // if we have entered into a class, eat tokens until we find the closing brace
212 if ( T_CLASS !== $structure_type ) {
213 continue;
214 }
215
216 // find the opening brace for the class
217 while ( ! $this->end() && '{' !== $token ) {
218 $token = $this->peek();
219 $this->next();
220 }
221
222 // continue traversing the class tokens until we have found the class closing brace
223 $depth = 1;
224 while ( ! $this->end() && $depth > 0 ) {
225 $token = $this->peek();
226
227 if ( '{' === $token ) {
228 $depth++;
229 } elseif ( '}' === $token ) {
230 $depth--;
231 }
232
233 $this->next();
234 }
235
236 // if we did not make it out of the class, then there's a problem
237 if ( $depth > 0 ) {
238 return array(
239 'message' => __( 'Parse error: syntax error, unexpected end of snippet', 'code-snippets' ),
240 'line' => $token[2],
241 );
242 }
243 }
244
245 return false;
246 }
247 }
248