Skip to content

Commit f3c3428

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 93f71d1 commit f3c3428

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

380382
The scope map output above shows the following pieces of information about the file:
381-
* A scope token `if` was found at token 1 (note that token 0 is the open PHP tag).
382-
* The opener for the if statement, the open curly brace, was found at token 7.
383-
* The closer for the if statement, the close curly brace, was found at token 15.
384-
* 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.
383+
* A scope token `if` was found at token 2 (note that token 0 is the open PHP tag).
384+
* The opener for the if statement, the open curly brace, was found at token 8.
385+
* The closer for the if statement, the close curly brace, was found at token 16.
386+
* 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.
385387

386-
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.
388+
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`.
387389

388390
### The Level Map
389391

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

430433
The level map output above shows the following pieces of information about the file:
431-
* A scope opener, an open curly brace, was found at token 7 and opened the scope for an if statement, defined at token 1.
432-
* 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.
434+
* A scope opener, an open curly brace, was found at token 8 and opened the scope for an if statement, defined at token 2.
435+
* 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.
433436

434437
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).
435438

@@ -452,54 +455,56 @@ if ($condition) {
452455
?>
453456
```
454457

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

496501
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.
497502

498-
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.
503+
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.
499504

500505
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.
501506

502-
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.
507+
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.
503508

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

0 commit comments

Comments
 (0)