1313use function array_merge ;
1414use function array_slice ;
1515use function defined ;
16- use function end ;
1716use function explode ;
1817use function file_get_contents ;
1918use function function_exists ;
2019use function implode ;
2120use function is_array ;
22- use function key ;
2321use function max ;
2422use function str_pad ;
2523use function str_replace ;
26- use function strlen ;
2724use function token_get_all ;
2825use const PHP_EOL ;
2926use const STR_PAD_LEFT ;
@@ -82,8 +79,10 @@ class Highlighter
8279 /** @var self */
8380 private static $ instance ;
8481
85- /** @var array */
86- private $ defaultTheme = [
82+ /**
83+ * @var array
84+ */
85+ private $ codeTheme = [
8786 self ::TOKEN_STRING => 'green ' ,
8887 self ::TOKEN_COMMENT => 'italic ' ,
8988 self ::TOKEN_KEYWORD => 'yellow ' ,
@@ -336,7 +335,7 @@ private function colorLines(array $tokenLines): array
336335 foreach ($ tokenLines as $ lineCount => $ tokenLine ) {
337336 $ line = '' ;
338337 foreach ($ tokenLine as [$ tokenType , $ tokenValue ]) {
339- $ style = $ this ->defaultTheme [$ tokenType ];
338+ $ style = $ this ->codeTheme [$ tokenType ];
340339
341340 if (Color::hasStyle ($ style )) {
342341 $ line .= Color::apply ($ style , $ tokenValue );
@@ -359,22 +358,22 @@ private function colorLines(array $tokenLines): array
359358 */
360359 private function lineNumbers (array $ lines , $ markLine = null ): string
361360 {
362- end ($ lines );
363-
364361 $ snippet = '' ;
365- $ lineLen = strlen ( key ( $ lines ) + 1 ) ;
366- $ lmStyle = $ this ->defaultTheme [self ::ACTUAL_LINE_MARK ];
367- $ lnStyle = $ this ->defaultTheme [self ::LINE_NUMBER ];
362+ $ lineLen = count ( $ lines ) + 1 ;
363+ $ lmStyle = $ this ->codeTheme [self ::ACTUAL_LINE_MARK ];
364+ $ lnStyle = $ this ->codeTheme [self ::LINE_NUMBER ];
368365
369366 foreach ($ lines as $ i => $ line ) {
367+ $ lineNum = $ i + 1 ;
368+ $ lineStr = (string )($ lineNum );
370369 if ($ markLine !== null ) {
371- $ snippet .= ($ markLine === $ i + 1 ? Color::apply ($ lmStyle , ' > ' ) : ' ' );
370+ $ snippet .= ($ markLine === $ lineNum ? Color::apply ($ lmStyle , ' > ' ) : ' ' );
372371 $ snippet .= Color::apply (
373- $ markLine === $ i + 1 ? $ lmStyle : $ lnStyle ,
374- str_pad ($ i + 1 , $ lineLen , ' ' , STR_PAD_LEFT ) . '| '
372+ $ markLine === $ lineNum ? $ lmStyle : $ lnStyle ,
373+ str_pad ($ lineStr , $ lineLen , ' ' , STR_PAD_LEFT ) . '| '
375374 );
376375 } else {
377- $ snippet .= Color::apply ($ lnStyle , str_pad ($ i + 1 , $ lineLen , ' ' , STR_PAD_LEFT ) . '| ' );
376+ $ snippet .= Color::apply ($ lnStyle , str_pad ($ lineStr , $ lineLen , ' ' , STR_PAD_LEFT ) . '| ' );
378377 }
379378
380379 $ snippet .= $ line . PHP_EOL ;
@@ -386,16 +385,16 @@ private function lineNumbers(array $lines, $markLine = null): string
386385 /**
387386 * @return array
388387 */
389- public function getDefaultTheme (): array
388+ public function getCodeTheme (): array
390389 {
391- return $ this ->defaultTheme ;
390+ return $ this ->codeTheme ;
392391 }
393392
394393 /**
395- * @param array $defaultTheme
394+ * @param array $codeTheme
396395 */
397- public function setDefaultTheme (array $ defaultTheme ): void
396+ public function setCodeTheme (array $ codeTheme ): void
398397 {
399- $ this ->defaultTheme = array_merge ($ this ->defaultTheme , $ defaultTheme );
398+ $ this ->codeTheme = array_merge ($ this ->codeTheme , $ codeTheme );
400399 }
401400}
0 commit comments