Skip to content

Commit 4a60b86

Browse files
committed
4.0 | Wiki/Advanced Usage: update for change in PHP open tag tokenization
Notes: 1. I've toned down the indentation a little to reduce the need for vertical scrolling. This is in line with how these output samples were previously presented in the wiki. 2. And while I _am_ adding a `annotated-ruleset-debug-output.php` file which can be used to generate these output samples to the `build/wiki-code-samples` directory, at this time, we cannot yet selectively only retrieve the desired output for this documentation, so we cannot automate updating these output blocks. This will hopefully change in the foreseeable future once PHPCSStandards/PHP_CodeSniffer 1030 has been implemented, though as there is also a textual part which refers to line numbers in the output blocks, manually updating these output blocks may still be the more stable way. For the "Verbose Token Processing Output" I've added a comment to document how to retrieve the desired output to update this output block. I've used the `PEAR` standard, not the `PSR12` standard to keep the output within reasonable bounds.
1 parent f182701 commit 4a60b86

File tree

2 files changed

+91
-81
lines changed

2 files changed

+91
-81
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
if ($condition) {
3+
echo 'Condition was true';
4+
}
5+
?>

wiki/Advanced-Usage.md

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -357,32 +357,34 @@ if ($condition) {
357357
The scope map output is:
358358
```text
359359
*** START SCOPE MAP ***
360-
Start scope map at 1: T_IF => if
361-
Process token 2 []: T_WHITESPACE =>
362-
Process token 3 []: T_OPEN_PARENTHESIS => (
363-
* skipping parenthesis *
364-
Process token 6 []: T_WHITESPACE =>
365-
Process token 7 []: T_OPEN_CURLY_BRACKET => {
366-
=> Found scope opener for 1 (T_IF)
367-
Process token 8 [opener:7;]: T_WHITESPACE => \n
368-
Process token 9 [opener:7;]: T_WHITESPACE =>
369-
Process token 10 [opener:7;]: T_ECHO => echo
370-
Process token 11 [opener:7;]: T_WHITESPACE =>
371-
Process token 12 [opener:7;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
372-
Process token 13 [opener:7;]: T_SEMICOLON => ;
373-
Process token 14 [opener:7;]: T_WHITESPACE => \n
374-
Process token 15 [opener:7;]: T_CLOSE_CURLY_BRACKET => }
375-
=> Found scope closer for 1 (T_IF)
360+
Start scope map at 2:T_IF => if
361+
=> Begin scope map recursion at token 2 with depth 1
362+
Process token 3 on line 2 []: T_WHITESPACE =>
363+
Process token 4 on line 2 []: T_OPEN_PARENTHESIS => (
364+
Process token 5 on line 2 []: T_VARIABLE => $condition
365+
Process token 6 on line 2 []: T_CLOSE_PARENTHESIS => )
366+
Process token 7 on line 2 []: T_WHITESPACE =>
367+
Process token 8 on line 2 []: T_OPEN_CURLY_BRACKET => {
368+
=> Found scope opener for 2:T_IF
369+
Process token 9 on line 2 [opener:8;]: T_WHITESPACE => \n
370+
Process token 10 on line 3 [opener:8;]: T_WHITESPACE =>
371+
Process token 11 on line 3 [opener:8;]: T_ECHO => echo
372+
Process token 12 on line 3 [opener:8;]: T_WHITESPACE =>
373+
Process token 13 on line 3 [opener:8;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
374+
Process token 14 on line 3 [opener:8;]: T_SEMICOLON => ;
375+
Process token 15 on line 3 [opener:8;]: T_WHITESPACE => \n
376+
Process token 16 on line 4 [opener:8;]: T_CLOSE_CURLY_BRACKET => }
377+
=> Found scope closer (16:T_CLOSE_CURLY_BRACKET) for 2:T_IF
376378
*** END SCOPE MAP ***
377379
```
378380

379381
The scope map output above shows the following pieces of information about the file:
380-
* A scope token `if` was found at token 1 (note that token 0 is the open PHP tag).
381-
* The opener for the if statement, the open curly brace, was found at token 7.
382-
* The closer for the if statement, the close curly brace, was found at token 15.
383-
* Tokens 8 - 15 are all included in the scope set by the scope opener at token 7, the open curly brace. This indicates that these tokens are all within the if statement.
382+
* A scope token `if` was found at token 2 (note that token 0 is the open PHP tag).
383+
* The opener for the if statement, the open curly brace, was found at token 8.
384+
* The closer for the if statement, the close curly brace, was found at token 16.
385+
* Tokens 9 - 15 are all included in the scope set by the scope opener at token 8, the open curly brace. This indicates that these tokens are all within the if statement.
384386

385-
The scope map output is most useful when debugging PHP_CodeSniffer's scope map, which is critically important to the successful checking of a file, but is also useful for checking the type of a particular token. For example, if you are unsure of the token type for an opening curly brace, the scope map output shows you that the type is T_OPEN_CURLY_BRACKET and not, for example, T_OPEN_CURLY_BRACE.
387+
The scope map output is most useful when debugging PHP_CodeSniffer's scope map, which is critically important to the successful checking of a file, but is also useful for checking the type of a particular token. For example, if you are unsure of the token type for an opening curly brace, the scope map output shows you that the type is `T_OPEN_CURLY_BRACKET` and not, for example, `T_OPEN_CURLY_BRACE`.
386388

387389
### The Level Map
388390

@@ -399,36 +401,37 @@ if ($condition) {
399401
The level map output is:
400402
```text
401403
*** START LEVEL MAP ***
402-
Process token 0 on line 1 [lvl:0;]: T_OPEN_TAG => <?php\n
403-
Process token 1 on line 2 [lvl:0;]: T_IF => if
404-
Process token 2 on line 2 [lvl:0;]: T_WHITESPACE =>
405-
Process token 3 on line 2 [lvl:0;]: T_OPEN_PARENTHESIS => (
406-
Process token 4 on line 2 [lvl:0;]: T_VARIABLE => $condition
407-
Process token 5 on line 2 [lvl:0;]: T_CLOSE_PARENTHESIS => )
408-
Process token 6 on line 2 [lvl:0;]: T_WHITESPACE =>
409-
Process token 7 on line 2 [lvl:0;]: T_OPEN_CURLY_BRACKET => {
410-
=> Found scope opener for 1 (T_IF)
404+
Process token 0 on line 1 [col:1;len:5;lvl:0;]: T_OPEN_TAG => <?php
405+
Process token 1 on line 1 [col:6;len:0;lvl:0;]: T_WHITESPACE => \n
406+
Process token 2 on line 2 [col:1;len:2;lvl:0;]: T_IF => if
407+
Process token 3 on line 2 [col:3;len:1;lvl:0;]: T_WHITESPACE =>
408+
Process token 4 on line 2 [col:4;len:1;lvl:0;]: T_OPEN_PARENTHESIS => (
409+
Process token 5 on line 2 [col:5;len:10;lvl:0;]: T_VARIABLE => $condition
410+
Process token 6 on line 2 [col:15;len:1;lvl:0;]: T_CLOSE_PARENTHESIS => )
411+
Process token 7 on line 2 [col:16;len:1;lvl:0;]: T_WHITESPACE =>
412+
Process token 8 on line 2 [col:17;len:1;lvl:0;]: T_OPEN_CURLY_BRACKET => {
413+
=> Found scope opener for 2:T_IF
411414
* level increased *
412-
* token 1 (T_IF) added to conditions array *
413-
Process token 8 on line 2 [lvl:1;conds;T_IF;]: T_WHITESPACE => \n
414-
Process token 9 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE =>
415-
Process token 10 on line 3 [lvl:1;conds;T_IF;]: T_ECHO => echo
416-
Process token 11 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE =>
417-
Process token 12 on line 3 [lvl:1;conds;T_IF;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
418-
Process token 13 on line 3 [lvl:1;conds;T_IF;]: T_SEMICOLON => ;
419-
Process token 14 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE => \n
420-
Process token 15 on line 4 [lvl:1;conds;T_IF;]: T_CLOSE_CURLY_BRACKET => }
421-
=> Found scope closer for 7 (T_OPEN_CURLY_BRACKET)
415+
* token 2:T_IF added to conditions array *
416+
Process token 9 on line 2 [col:18;len:0;lvl:1;conds;T_IF;]: T_WHITESPACE => \n
417+
Process token 10 on line 3 [col:1;len:4;lvl:1;conds;T_IF;]: T_WHITESPACE =>
418+
Process token 11 on line 3 [col:5;len:4;lvl:1;conds;T_IF;]: T_ECHO => echo
419+
Process token 12 on line 3 [col:9;len:1;lvl:1;conds;T_IF;]: T_WHITESPACE =>
420+
Process token 13 on line 3 [col:10;len:20;lvl:1;conds;T_IF;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
421+
Process token 14 on line 3 [col:30;len:1;lvl:1;conds;T_IF;]: T_SEMICOLON => ;
422+
Process token 15 on line 3 [col:31;len:0;lvl:1;conds;T_IF;]: T_WHITESPACE => \n
423+
Process token 16 on line 4 [col:1;len:1;lvl:1;conds;T_IF;]: T_CLOSE_CURLY_BRACKET => }
424+
=> Found scope closer for 8:T_OPEN_CURLY_BRACKET
422425
* token T_IF removed from conditions array *
423426
* level decreased *
424-
Process token 16 on line 4 [lvl:0;]: T_WHITESPACE => \n
425-
Process token 17 on line 5 [lvl:0;]: T_CLOSE_TAG => ?>\n
427+
Process token 17 on line 4 [col:2;len:0;lvl:0;]: T_WHITESPACE => \n
428+
Process token 18 on line 5 [col:1;len:2;lvl:0;]: T_CLOSE_TAG => ?>\n
426429
*** END LEVEL MAP ***
427430
```
428431

429432
The level map output above shows the following pieces of information about the file:
430-
* A scope opener, an open curly brace, was found at token 7 and opened the scope for an if statement, defined at token 1.
431-
* Tokens 8 - 15 are all included in the scope set by the scope opener at token 7, the open curly brace. All these tokens are at level 1, indicating that they are enclosed in 1 scope condition, and all these tokens are enclosed in a single condition; an if statement.
433+
* A scope opener, an open curly brace, was found at token 8 and opened the scope for an if statement, defined at token 2.
434+
* Tokens 9 - 16 are all included in the scope set by the scope opener at token 8, the open curly brace. All these tokens are at level 1, indicating that they are enclosed in 1 scope condition, and all these tokens are enclosed in a single condition; an if statement.
432435

433436
The level map is most commonly used to determine indentation rules (e.g., a token 4 levels deep requires 16 spaces of indentation) or to determine if a particular token is within a particular scope (e.g., a function keyword is within a class scope, making it a method).
434437

@@ -451,54 +454,56 @@ if ($condition) {
451454
?>
452455
```
453456

457+
<!-- Regenerate using "phpcs ./build/wiki-code-samples/annotated-ruleset-debug-output.php -vvv --standard=PEAR" -->
454458
The token processing output is:
455459
```text
456460
*** START TOKEN PROCESSING ***
457-
Process token 0: T_OPEN_TAG => <?php\n
458-
Processing PEAR_Sniffs_Commenting_FileCommentSniff... DONE in 0 seconds
459-
Processing Generic_Sniffs_PHP_DisallowShortOpenTagSniff... DONE in 0 seconds
460-
Processing Generic_Sniffs_Files_LineLengthSniff... DONE in 0.0001 seconds
461-
Processing Generic_Sniffs_Files_LineEndingsSniff... DONE in 0 seconds
462-
Process token 1: T_IF => if
463-
Processing PEAR_Sniffs_ControlStructures_ControlSignatureSniff... DONE in 0.0001 seconds
464-
Processing PEAR_Sniffs_ControlStructures_MultiLineConditionSniff... DONE in 0 seconds
465-
Processing PEAR_Sniffs_WhiteSpace_ScopeClosingBraceSniff... DONE in 0 seconds
466-
Processing PEAR_Sniffs_WhiteSpace_ScopeIndentSniff... DONE in 0 seconds
467-
Processing Generic_Sniffs_ControlStructures_InlineControlStructureSniff... DONE in 0 seconds
468-
Process token 2: T_WHITESPACE =>
469-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
470-
Process token 3: T_OPEN_PARENTHESIS => (
471-
Process token 4: T_VARIABLE => $condition
472-
Processing PEAR_Sniffs_NamingConventions_ValidVariableNameSniff... DONE in 0 seconds
473-
Process token 5: T_CLOSE_PARENTHESIS => )
474-
Process token 6: T_WHITESPACE =>
475-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
476-
Process token 7: T_OPEN_CURLY_BRACKET => {
477-
Process token 8: T_WHITESPACE => \n
478-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
479-
Process token 9: T_WHITESPACE =>
480-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
481-
Process token 10: T_ECHO => echo
482-
Process token 11: T_WHITESPACE =>
483-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
484-
Process token 12: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
485-
Process token 13: T_SEMICOLON => ;
486-
Process token 14: T_WHITESPACE => \n
487-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
488-
Process token 15: T_CLOSE_CURLY_BRACKET => }
489-
Process token 16: T_WHITESPACE => \n
490-
Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds
491-
Process token 17: T_CLOSE_TAG => ?>\n
461+
Process token 0: T_OPEN_TAG => <?php
462+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff... DONE in 0.0002 seconds
463+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\WhiteSpace\ScopeIndentSniff... DONE in 0.0003 seconds
464+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowShortOpenTagSniff... DONE in 0.0002 seconds
465+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\DisallowTabIndentSniff... DONE in 0.0004 seconds
466+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff... DONE in 0.0002 seconds
467+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineEndingsSniff... DONE in 0.0002 seconds
468+
Process token 1: T_WHITESPACE => \n
469+
Process token 2: T_IF => if
470+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\ControlStructures\ControlSignatureSniff... DONE in 0.0002 seconds
471+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\ControlStructures\MultiLineConditionSniff... DONE in 0.0001 seconds
472+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\WhiteSpace\ScopeClosingBraceSniff... DONE in 0.0001 seconds
473+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures\InlineControlStructureSniff... DONE in 0.0001 seconds
474+
Process token 3: T_WHITESPACE =>
475+
Process token 4: T_OPEN_PARENTHESIS => (
476+
Process token 5: T_VARIABLE => $condition
477+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds
478+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidVariableNameSniff... DONE in 0.001 seconds
479+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0001 seconds
480+
Process token 6: T_CLOSE_PARENTHESIS => )
481+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds
482+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0001 seconds
483+
Process token 7: T_WHITESPACE =>
484+
Process token 8: T_OPEN_CURLY_BRACKET => {
485+
Process token 9: T_WHITESPACE => \n
486+
Process token 10: T_WHITESPACE =>
487+
Process token 11: T_ECHO => echo
488+
Process token 12: T_WHITESPACE =>
489+
Process token 13: T_CONSTANT_ENCAPSED_STRING => 'Condition was true'
490+
Process token 14: T_SEMICOLON => ;
491+
Process token 15: T_WHITESPACE => \n
492+
Process token 16: T_CLOSE_CURLY_BRACKET => }
493+
Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds
494+
Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0002 seconds
495+
Process token 17: T_WHITESPACE => \n
496+
Process token 18: T_CLOSE_TAG => ?>\n
492497
*** END TOKEN PROCESSING ***
493498
```
494499

495500
Every token processed is shown, along with its ID, type and contents. For each token, all sniffs that were executed on the token are displayed, along with the running time.
496501

497-
For example, the output above shows us that token 1, an if keyword, had 5 sniffs executed on it; the ControlSignature sniff, the MultiLineCondition sniff, the ScopeClosingBrace sniff, the ScopeIndent sniff and the InlineControlStructure sniff. Each was executed fairly quickly, but the slowest was the ControlSignature sniff, taking 0.0001 seconds to process that token.
502+
For example, the output above shows us that token 2, an if keyword, had 4 sniffs executed on it; the ControlSignature sniff, the MultiLineCondition sniff, the ScopeClosingBrace sniff, and the InlineControlStructure sniff. Each was executed fairly quickly, but the slowest was the `ControlSignature` sniff, taking 0.0002 seconds to process that token.
498503

499504
The other interesting piece of information we get from the output above is that some tokens didn't have any sniffs executed on them. This is normal behaviour for PHP_CodeSniffer as most sniffs listen for specific or rarely used tokens and then execute on it and a number of tokens following it.
500505

501-
For example, the ScopeIndentSniff executes on the if statement's token only, but actually checks the indentation of every line within the if statement. The sniff uses the scope map to find all tokens within the if statement.
506+
For example, the `MultiLineConditionSniff` executes on the if statement's token only, but actually checks the format and indentation of every line within the if statement condition. The sniff uses the token map to find all tokens within the if condition.
502507

503508
<p align="right"><a href="#table-of-contents">back to top</a></p>
504509

0 commit comments

Comments
 (0)