@@ -68,10 +68,14 @@ abstract class AbstractArrayDeclarationSniff implements Sniff
6868 * A multi-dimentional array with information on each array item.
6969 *
7070 * The array index is 1-based and contains the following information on each array item:
71- * - 'start' : The stack pointer to the first token in the array item.
72- * - 'end' : The stack pointer to the first token in the array item.
73- * - 'raw' : A string with the contents of all tokens between `start` and `end`.
74- * - 'clean' : Same as `raw`, but all comment tokens have been stripped out.
71+ * ```php
72+ * 1 => array(
73+ * 'start' => int, // The stack pointer to the first token in the array item.
74+ * 'end' => int, // The stack pointer to the last token in the array item.
75+ * 'raw' => string, // A string with the contents of all tokens between `start` and `end`.
76+ * 'clean' => string, // Same as `raw`, but all comment tokens have been stripped out.
77+ * )
78+ * ```
7579 *
7680 * @since 1.0.0
7781 *
@@ -100,6 +104,8 @@ abstract class AbstractArrayDeclarationSniff implements Sniff
100104 /**
101105 * List of tokens which can safely be used with an eval() expression.
102106 *
107+ * This list gets enhanced with additional token groups in the constructor.
108+ *
103109 * @since 1.0.0
104110 *
105111 * @var array
@@ -161,7 +167,7 @@ public function register()
161167 * Processes this test when one of its tokens is encountered.
162168 *
163169 * This method fills the properties with relevant information for examining the array
164- * and then passes off to the ` processArray()` method.
170+ * and then passes off to the {@see AbstractArrayDeclarationSniff:: processArray()} method.
165171 *
166172 * @since 1.0.0
167173 *
@@ -203,7 +209,19 @@ final public function process(File $phpcsFile, $stackPtr)
203209 /**
204210 * Process every part of the array declaration.
205211 *
206- * This contains the default logic for the sniff, but can be overloaded in a concrete child class
212+ * Controller which calls the individual `process...()` methods for each part of the array.
213+ *
214+ * The method starts by calling the {@see AbstractArrayDeclarationSniff::processOpenClose()} method
215+ * and subsequently calls the following methods for each array item:
216+ *
217+ * Unkeyed arrays | Keyed arrays
218+ * -------------- | ------------
219+ * processNoKey() | processKey()
220+ * - | processArrow()
221+ * processValue() | processValue()
222+ * processComma() | processComma()
223+ *
224+ * This is the default logic for the sniff, but can be overloaded in a concrete child class
207225 * if needed.
208226 *
209227 * @since 1.0.0
@@ -224,7 +242,12 @@ public function processArray(File $phpcsFile)
224242 }
225243
226244 foreach ($ this ->arrayItems as $ itemNr => $ arrayItem ) {
227- $ arrowPtr = Arrays::getDoubleArrowPtr ($ phpcsFile , $ arrayItem ['start ' ], $ arrayItem ['end ' ]);
245+ try {
246+ $ arrowPtr = Arrays::getDoubleArrowPtr ($ phpcsFile , $ arrayItem ['start ' ], $ arrayItem ['end ' ]);
247+ } catch (RuntimeException $ e ) {
248+ // Parse error: empty array item. Ignore.
249+ continue ;
250+ }
228251
229252 if ($ arrowPtr !== false ) {
230253 if ($ this ->processKey ($ phpcsFile , $ arrayItem ['start ' ], ($ arrowPtr - 1 ), $ itemNr ) === true ) {
@@ -260,7 +283,7 @@ public function processArray(File $phpcsFile)
260283 /**
261284 * Process the array opener and closer.
262285 *
263- * Optional method to be implemented in concrete child classes.
286+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
264287 *
265288 * @since 1.0.0
266289 *
@@ -271,7 +294,9 @@ public function processArray(File $phpcsFile)
271294 * @param int $openPtr The position of the array opener token in the token stack.
272295 * @param int $closePtr The position of the array closer token in the token stack.
273296 *
274- * @return true|void Returning `true` will short-circuit the sniff and stop processing.
297+ * @return true|void Returning `TRUE` will short-circuit the sniff and stop processing.
298+ * In effect, this means that the sniff will not examine the individual
299+ * array items if `TRUE` is returned.
275300 */
276301 public function processOpenClose (File $ phpcsFile , $ openPtr , $ closePtr )
277302 {
@@ -280,10 +305,10 @@ public function processOpenClose(File $phpcsFile, $openPtr, $closePtr)
280305 /**
281306 * Process the tokens in an array key.
282307 *
283- * Optional method to be implemented in concrete child classes.
308+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
284309 *
285- * The $startPtr and $endPtr do not discount whitespace or comments, but are all inclusive to
286- * allow examining all tokens in an array key.
310+ * Note: The ` $startPtr` and ` $endPtr` do not discount whitespace or comments, but are all inclusive
311+ * to allow for examining all tokens in an array key.
287312 *
288313 * @since 1.0.0
289314 *
@@ -300,7 +325,9 @@ public function processOpenClose(File $phpcsFile, $openPtr, $closePtr)
300325 * @param int $itemNr Which item in the array is being handled.
301326 * 1-based, i.e. the first item is item 1, the second 2 etc.
302327 *
303- * @return true|void Returning `true` will short-circuit the array item loop and stop processing.
328+ * @return true|void Returning `TRUE` will short-circuit the array item loop and stop processing.
329+ * In effect, this means that the sniff will not examine the double arrow, the array
330+ * value or comma for this array item and will not process any array items after this one.
304331 */
305332 public function processKey (File $ phpcsFile , $ startPtr , $ endPtr , $ itemNr )
306333 {
@@ -309,12 +336,17 @@ public function processKey(File $phpcsFile, $startPtr, $endPtr, $itemNr)
309336 /**
310337 * Process an array item without an array key.
311338 *
312- * Optional method to be implemented in concrete child classes.
339+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
340+ *
341+ * Note: This method is _not_ intended for processing the array _value_. Use the
342+ * {@see AbstractArrayDeclarationSniff::processValue()} method to implement processing of the array value.
313343 *
314344 * @since 1.0.0
315345 *
316346 * @codeCoverageIgnore
317347 *
348+ * @see \PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff::processValue() Method to process the array value.
349+ *
318350 * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
319351 * token was found.
320352 * @param int $startPtr The stack pointer to the first token in the array item,
@@ -323,7 +355,9 @@ public function processKey(File $phpcsFile, $startPtr, $endPtr, $itemNr)
323355 * @param int $itemNr Which item in the array is being handled.
324356 * 1-based, i.e. the first item is item 1, the second 2 etc.
325357 *
326- * @return true|void Returning `true` will short-circuit the array item loop and stop processing.
358+ * @return true|void Returning `TRUE` will short-circuit the array item loop and stop processing.
359+ * In effect, this means that the sniff will not examine the array value or
360+ * comma for this array item and will not process any array items after this one.
327361 */
328362 public function processNoKey (File $ phpcsFile , $ startPtr , $ itemNr )
329363 {
@@ -332,7 +366,7 @@ public function processNoKey(File $phpcsFile, $startPtr, $itemNr)
332366 /**
333367 * Process the double arrow.
334368 *
335- * Optional method to be implemented in concrete child classes.
369+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
336370 *
337371 * @since 1.0.0
338372 *
@@ -344,7 +378,9 @@ public function processNoKey(File $phpcsFile, $startPtr, $itemNr)
344378 * @param int $itemNr Which item in the array is being handled.
345379 * 1-based, i.e. the first item is item 1, the second 2 etc.
346380 *
347- * @return true|void Returning `true` will short-circuit the array item loop and stop processing.
381+ * @return true|void Returning `TRUE` will short-circuit the array item loop and stop processing.
382+ * In effect, this means that the sniff will not examine the array value or
383+ * comma for this array item and will not process any array items after this one.
348384 */
349385 public function processArrow (File $ phpcsFile , $ arrowPtr , $ itemNr )
350386 {
@@ -353,10 +389,10 @@ public function processArrow(File $phpcsFile, $arrowPtr, $itemNr)
353389 /**
354390 * Process the tokens in an array value.
355391 *
356- * Optional method to be implemented in concrete child classes.
392+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
357393 *
358- * The $startPtr and $endPtr do not discount whitespace or comments, but are all inclusive to
359- * allow examining all tokens in an array value.
394+ * Note: The ` $startPtr` and ` $endPtr` do not discount whitespace or comments, but are all inclusive
395+ * to allow for examining all tokens in an array value.
360396 *
361397 * @since 1.0.0
362398 *
@@ -371,7 +407,9 @@ public function processArrow(File $phpcsFile, $arrowPtr, $itemNr)
371407 * @param int $itemNr Which item in the array is being handled.
372408 * 1-based, i.e. the first item is item 1, the second 2 etc.
373409 *
374- * @return true|void Returning `true` will short-circuit the array item loop and stop processing.
410+ * @return true|void Returning `TRUE` will short-circuit the array item loop and stop processing.
411+ * In effect, this means that the sniff will not examine the comma for this
412+ * array item and will not process any array items after this one.
375413 */
376414 public function processValue (File $ phpcsFile , $ startPtr , $ endPtr , $ itemNr )
377415 {
@@ -380,7 +418,7 @@ public function processValue(File $phpcsFile, $startPtr, $endPtr, $itemNr)
380418 /**
381419 * Process the comma after an array item.
382420 *
383- * Optional method to be implemented in concrete child classes.
421+ * Optional method to be implemented in concrete child classes. By default, this method does nothing.
384422 *
385423 * @since 1.0.0
386424 *
@@ -392,7 +430,9 @@ public function processValue(File $phpcsFile, $startPtr, $endPtr, $itemNr)
392430 * @param int $itemNr Which item in the array is being handled.
393431 * 1-based, i.e. the first item is item 1, the second 2 etc.
394432 *
395- * @return true|void Returning `true` will short-circuit the array item loop and stop processing.
433+ * @return true|void Returning `TRUE` will short-circuit the array item loop and stop processing.
434+ * In effect, this means that the sniff will not process any array items
435+ * after this one.
396436 */
397437 public function processComma (File $ phpcsFile , $ commaPtr , $ itemNr )
398438 {
@@ -401,7 +441,8 @@ public function processComma(File $phpcsFile, $commaPtr, $itemNr)
401441 /**
402442 * Determine what the actual array key would be.
403443 *
404- * Optional helper function for processsing array keys in the processKey() function.
444+ * Helper function for processsing array keys in the processKey() function.
445+ * Using this method is up to the sniff implementation in the child class.
405446 *
406447 * @since 1.0.0
407448 *
0 commit comments