Skip to content

Commit af84bd6

Browse files
authored
Merge pull request #14 from PHPCSStandards/feature/tokenlist-more-defensive-coding
Debug/TokenList: add more defensive coding
2 parents 43bc636 + 9336096 commit af84bd6

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

Debug/Sniffs/Debug/TokenListSniff.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ class TokenListSniff implements Sniff
3535
'CSS',
3636
];
3737

38+
/**
39+
* Default values for the token indexes accessed.
40+
*
41+
* This prevents issues with "undefined index" notices in case of rare tokenizer issues.
42+
*
43+
* @var array
44+
*/
45+
private $tokenDefaults = [
46+
'type' => '?',
47+
'code' => '?',
48+
'content' => '',
49+
'line' => '?',
50+
'column' => '?',
51+
'level' => 0,
52+
'conditions' => [],
53+
];
54+
3855
/**
3956
* Returns an array of tokens this test wants to listen for.
4057
*
@@ -76,27 +93,29 @@ public function process(File $phpcsFile, $stackPtr)
7693
echo \str_repeat('-', ($ptrPadding + $linePadding + 35 + 16 + 18)), \PHP_EOL;
7794

7895
foreach ($tokens as $ptr => $token) {
96+
$token += $this->tokenDefaults;
97+
$content = $token['content'];
98+
7999
if (isset($token['length']) === false) {
80-
$token['length'] = strlen($token['content']);
100+
$token['length'] = 0;
101+
if (isset($token['content'])) {
102+
$token['length'] = strlen($content);
103+
}
81104
}
82105

83-
$content = $token['content'];
84106
if ($token['code'] === \T_WHITESPACE
85107
|| (defined('T_DOC_COMMENT_WHITESPACE')
86108
&& $token['code'] === \T_DOC_COMMENT_WHITESPACE)
87109
) {
88-
if (strpos($token['content'], "\t") !== false) {
89-
$content = str_replace("\t", '\t', $token['content']);
110+
if (strpos($content, "\t") !== false) {
111+
$content = str_replace("\t", '\t', $content);
90112
}
91113
if (isset($token['orig_content'])) {
92114
$content .= ' :: Orig: ' . str_replace("\t", '\t', $token['orig_content']);
93115
}
94116
}
95117

96-
$conditionCount = 'F'; // False.
97-
if (isset($token['conditions'])) {
98-
$conditionCount = count($token['conditions']);
99-
}
118+
$conditionCount = count($token['conditions']);
100119

101120
echo \str_pad($ptr, $ptrPadding, ' ', \STR_PAD_LEFT),
102121
' :: L', \str_pad($token['line'], $linePadding, '0', \STR_PAD_LEFT),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<?php
2+
3+
function

Debug/Tests/Debug/TokenListZUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function testOutput()
3737
$expected .= 'Ptr :: Ln :: Col :: Cond :: Token Type :: [len]: Content' . "\n";
3838
$expected .= '-------------------------------------------------------------------------' . "\n";
3939
$expected .= ' 0 :: L1 :: C 1 :: CC 0 :: T_OPEN_TAG :: [5]: <?php' . "\n\n";
40+
$expected .= ' 1 :: L2 :: C 1 :: CC 0 :: T_WHITESPACE :: [0]: ' . "\n\n";
41+
$expected .= ' 2 :: L3 :: C 1 :: CC 0 :: T_FUNCTION :: [8]: function' . "\n";
42+
$expected .= ' 3 :: L3 :: C 9 :: CC 0 :: T_WHITESPACE :: [0]: ' . "\n\n";
4043

4144
$this->assertSame($expected, $output);
4245
}

0 commit comments

Comments
 (0)