Skip to content

Commit 648d9df

Browse files
authored
Merge pull request #8 from PHPCSStandards/feature/tokenlist-enhance-output
Debug\TokenList: enhance the output
2 parents 06720bc + 147af1b commit 648d9df

4 files changed

Lines changed: 61 additions & 32 deletions

File tree

Debug/Sniffs/Debug/TokenListSniff.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,23 @@ public function register()
6060
public function process(File $phpcsFile, $stackPtr)
6161
{
6262
$tokens = $phpcsFile->getTokens();
63+
$last = ($phpcsFile->numTokens - 1);
64+
65+
$ptrPadding = max(3, strlen($last));
66+
$linePadding = strlen($tokens[$last]['line']);
6367

6468
$oldIniValue = \ini_set('xdebug.overload_var_dump', 1);
6569

6670
echo \PHP_EOL;
71+
echo \str_pad('Ptr', $ptrPadding, ' ', \STR_PAD_BOTH),
72+
' :: ', \str_pad('Ln', ($linePadding + 1), ' ', \STR_PAD_BOTH),
73+
' :: ', \str_pad('Col', 4, ' ', \STR_PAD_BOTH),
74+
' :: ', 'Cond',
75+
' :: ', \str_pad('Token Type', 26), // Longest token type name is 26 chars.
76+
' :: [len]: Content', \PHP_EOL;
77+
78+
echo \str_repeat('-', ($ptrPadding + $linePadding + 35 + 16 + 18)), \PHP_EOL;
79+
6780
foreach ($tokens as $ptr => $token) {
6881
if (isset($token['length']) === false) {
6982
$token['length'] = strlen($token['content']);
@@ -82,8 +95,17 @@ public function process(File $phpcsFile, $stackPtr)
8295
}
8396
}
8497

85-
echo $ptr, ' :: L', \str_pad($token['line'], 3, '0', \STR_PAD_LEFT), ' :: C', $token['column'],
86-
' :: ', $token['type'], ' :: (', $token['length'], ') :: ', $content, \PHP_EOL;
98+
$conditionCount = 'F'; // False.
99+
if (isset($token['conditions'])) {
100+
$conditionCount = count($token['conditions']);
101+
}
102+
103+
echo \str_pad($ptr, $ptrPadding, ' ', \STR_PAD_LEFT),
104+
' :: L', \str_pad($token['line'], $linePadding, '0', \STR_PAD_LEFT),
105+
' :: C', \str_pad($token['column'], 3, ' ', \STR_PAD_LEFT),
106+
' :: CC', \str_pad($conditionCount, 2, ' ', \STR_PAD_LEFT),
107+
' :: ', \str_pad($token['type'], 26), // Longest token type name is 26 chars.
108+
' :: [', $token['length'], ']: ', $content, \PHP_EOL;
87109
}
88110

89111
// If necessary, reset the ini setting.

Debug/Tests/Debug/TokenListUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Unit test class for the TokenList sniff.
1717
*
18-
* @covers PHPCSStandards\Debug\Sniffs\Debug::TokenListSniff
18+
* @covers PHPCSStandards\Debug\Sniffs\Debug\TokenListSniff
1919
*
2020
* @since 1.0.0
2121
*/

Debug/Tests/Debug/TokenListZUnitTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Unit test class for the TokenList sniff.
1717
*
18-
* @covers PHPCSStandards\Debug\Sniffs\Debug::TokenListSniff
18+
* @covers PHPCSStandards\Debug\Sniffs\Debug\TokenListSniff
1919
*
2020
* @since 1.0.0
2121
*/
@@ -33,7 +33,12 @@ public function testOutput()
3333

3434
$this->assertNotEmpty($output);
3535

36-
$expected = '0 :: L001 :: C1 :: T_OPEN_TAG :: (5) :: <?php';
36+
$expected = 'Ptr :: Ln :: Col :: Cond :: Token Type :: [len]: Content';
37+
$expected .= PHP_EOL;
38+
$expected .= '-------------------------------------------------------------------------';
39+
$expected .= PHP_EOL;
40+
$expected .= ' 0 :: L1 :: C 1 :: CC 0 :: T_OPEN_TAG :: [5]: <?php';
41+
3742
$this->assertSame($expected, $output);
3843
}
3944
}

README.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,37 @@ phpcs ./SniffNameUnitTest.inc --standard=Debug
132132
```bash
133133
phpcs ./SniffNameUnitTest.inc --standard=YourStandard,Debug --sniffs=YourStandard.Category.NewSniffName,Debug.Debug.TokenList
134134
```
135-
135+
136136
The output will look something along the lines of:
137137
```
138-
0 :: L001 :: C1 :: T_OPEN_TAG :: (5) :: <?php
139-
140-
1 :: L002 :: C1 :: T_WHITESPACE :: (0) ::
141-
142-
2 :: L003 :: C1 :: T_COMMENT :: (32) :: // Boolean not operator: All OK.
143-
144-
3 :: L004 :: C1 :: T_IF :: (2) :: if
145-
4 :: L004 :: C3 :: T_WHITESPACE :: (1) ::
146-
5 :: L004 :: C4 :: T_OPEN_PARENTHESIS :: (1) :: (
147-
6 :: L004 :: C5 :: T_WHITESPACE :: (1) ::
148-
7 :: L004 :: C6 :: T_CONSTANT_ENCAPSED_STRING :: (4) :: 'bb'
149-
8 :: L004 :: C10 :: T_WHITESPACE :: (1) ::
150-
9 :: L004 :: C11 :: T_IS_NOT_IDENTICAL :: (3) :: !==
151-
10 :: L004 :: C14 :: T_WHITESPACE :: (1) ::
152-
11 :: L004 :: C15 :: T_CONSTANT_ENCAPSED_STRING :: (4) :: 'bb'
153-
12 :: L004 :: C19 :: T_WHITESPACE :: (1) ::
154-
13 :: L004 :: C20 :: T_CLOSE_PARENTHESIS :: (1) :: )
155-
14 :: L004 :: C21 :: T_WHITESPACE :: (1) ::
156-
15 :: L004 :: C22 :: T_OPEN_CURLY_BRACKET :: (1) :: {
157-
16 :: L004 :: C23 :: T_WHITESPACE :: (0) ::
158-
159-
17 :: L005 :: C1 :: T_WHITESPACE :: (1) :: \t
160-
18 :: L005 :: C2 :: T_IF :: (2) :: if
161-
19 :: L005 :: C4 :: T_WHITESPACE :: (1) ::
162-
20 :: L005 :: C5 :: T_OPEN_PARENTHESIS :: (1) :: (
163-
21 :: L005 :: C6 :: T_WHITESPACE :: (0) ::
138+
Ptr :: Ln :: Col :: Cond :: Token Type :: [len]: Content
139+
-------------------------------------------------------------------------
140+
0 :: L1 :: C 1 :: CC 0 :: T_OPEN_TAG :: [5]: <?php
141+
142+
1 :: L2 :: C 1 :: CC 0 :: T_WHITESPACE :: [0]:
143+
144+
2 :: L3 :: C 1 :: CC 0 :: T_COMMENT :: [32]: // Boolean not operator: All OK.
145+
146+
3 :: L4 :: C 1 :: CC 0 :: T_IF :: [2]: if
147+
4 :: L4 :: C 3 :: CC 0 :: T_WHITESPACE :: [1]:
148+
5 :: L4 :: C 4 :: CC 0 :: T_OPEN_PARENTHESIS :: [1]: (
149+
6 :: L4 :: C 5 :: CC 0 :: T_WHITESPACE :: [1]:
150+
7 :: L4 :: C 6 :: CC 0 :: T_CONSTANT_ENCAPSED_STRING :: [4]: 'bb'
151+
8 :: L4 :: C 10 :: CC 0 :: T_WHITESPACE :: [1]:
152+
9 :: L4 :: C 11 :: CC 0 :: T_IS_NOT_IDENTICAL :: [3]: !==
153+
10 :: L4 :: C 14 :: CC 0 :: T_WHITESPACE :: [1]:
154+
11 :: L4 :: C 15 :: CC 0 :: T_CONSTANT_ENCAPSED_STRING :: [4]: 'bb'
155+
12 :: L4 :: C 19 :: CC 0 :: T_WHITESPACE :: [1]:
156+
13 :: L4 :: C 20 :: CC 0 :: T_CLOSE_PARENTHESIS :: [1]: )
157+
14 :: L4 :: C 21 :: CC 0 :: T_WHITESPACE :: [1]:
158+
15 :: L4 :: C 22 :: CC 0 :: T_OPEN_CURLY_BRACKET :: [1]: {
159+
16 :: L4 :: C 23 :: CC 0 :: T_WHITESPACE :: [0]:
160+
161+
17 :: L5 :: C 1 :: CC 0 :: T_WHITESPACE :: [1]: \t
162+
18 :: L5 :: C 2 :: CC 0 :: T_IF :: [2]: if
163+
19 :: L5 :: C 4 :: CC 0 :: T_WHITESPACE :: [1]:
164+
20 :: L5 :: C 5 :: CC 0 :: T_OPEN_PARENTHESIS :: [1]: (
165+
21 :: L5 :: C 6 :: CC 0 :: T_WHITESPACE :: [0]:
164166
```
165167

166168
PHPCS itself can also display similar information using the `-vv` or `-vvv` verbosity flags, however, when using those, you will receive a *lot* more information than just the token list and, while useful for debugging PHPCS itself, the additional information is mostly just noise when developing a sniff.

0 commit comments

Comments
 (0)