@@ -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 ),
0 commit comments