Skip to content

Commit 0740abf

Browse files
committed
fix compatibility error on php 7.4
1 parent fa3a30a commit 0740abf

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
4343
\Toolkit\Cli\Cli::write($rendered);
4444
```
4545

46-
![colors](./example/cli-php-file-highlight.jpg)
46+
![colors](./example/cli-php-file-highlight.png)
4747

4848
## Console color
4949

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"require": {
2020
"php": ">7.1.0"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^7.5"
24+
},
2225
"autoload": {
2326
"psr-4": {
2427
"Toolkit\\Cli\\": "src/"
@@ -29,6 +32,9 @@
2932
"Toolkit\\CliTest\\": "src/"
3033
}
3134
},
35+
"scripts": {
36+
"test": "./vendor/bin/phpunit"
37+
},
3238
"suggest": {
3339
"inhere/php-validate": "Very lightweight data validate tool",
3440
"inhere/console": "a lightweight php console application library."

example/cli-php-file-highlight.png

73.2 KB
Loading

src/Color.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class Color
164164

165165
// option
166166
'bold' => '1',
167+
'italic' => '3',
167168
'underscore' => '4',
168169
'reverse' => '7',
169170
];

src/Flags.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static function parseArgv(array $params, array $config = []): array
132132
next($params);
133133

134134
// is options
135-
if ($p{0} === '-') {
135+
if ($p[0] === '-') {
136136
$value = true;
137137
$option = substr($p, 1);
138138
$isLong = false;
@@ -148,7 +148,7 @@ public static function parseArgv(array $params, array $config = []): array
148148
}
149149

150150
// short-opt: value specified inline (-<opt>=<value>)
151-
} elseif (isset($option{1}) && $option{1} === '=') {
151+
} elseif (isset($option[1]) && $option[1] === '=') {
152152
[$option, $value] = explode('=', $option, 2);
153153
}
154154

@@ -310,7 +310,7 @@ public static function nextIsValue($val): bool
310310
}
311311

312312
// it isn't option or named argument
313-
return $val{0} !== '-' && false === strpos($val, '=');
313+
return $val[0] !== '-' && false === strpos($val, '=');
314314
}
315315

316316
/**

src/Highlighter.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@
5757
*/
5858
class Highlighter
5959
{
60-
public const TOKEN_DEFAULT = 'token_default';
61-
public const TOKEN_COMMENT = 'token_comment';
62-
public const TOKEN_STRING = 'token_string';
63-
public const TOKEN_HTML = 'token_html';
64-
public const TOKEN_KEYWORD = 'token_keyword';
60+
public const TOKEN_DEFAULT = 'token_default';
61+
public const TOKEN_COMMENT = 'token_comment';
62+
public const TOKEN_STRING = 'token_string';
63+
public const TOKEN_HTML = 'token_html';
64+
65+
public const TOKEN_KEYWORD = 'token_keyword';
66+
public const TOKEN_CONSTANT = 'token_constant';
67+
public const TOKEN_VARIABLE = 'token_variable';
6568

6669
public const ACTUAL_LINE_MARK = 'actual_line_mark';
6770
public const LINE_NUMBER = 'line_number';
@@ -74,11 +77,13 @@ class Highlighter
7477

7578
/** @var array */
7679
private $defaultTheme = [
77-
self::TOKEN_STRING => 'red',
78-
self::TOKEN_COMMENT => 'yellow',
79-
self::TOKEN_KEYWORD => 'info',
80+
self::TOKEN_STRING => 'green',
81+
self::TOKEN_COMMENT => 'italic',
82+
self::TOKEN_KEYWORD => 'yellow',
8083
self::TOKEN_DEFAULT => 'normal',
84+
self::TOKEN_CONSTANT => 'red',
8185
self::TOKEN_HTML => 'cyan',
86+
self::TOKEN_VARIABLE => 'cyan',
8287
self::ACTUAL_LINE_MARK => 'red',
8388
self::LINE_NUMBER => 'darkGray',
8489
];
@@ -227,10 +232,9 @@ private function tokenize(string $source): array
227232
case T_OPEN_TAG_WITH_ECHO:
228233
case T_CLOSE_TAG:
229234
case T_STRING:
230-
case T_VARIABLE:
231235
// Constants
232-
case T_DIR:
233-
case T_FILE:
236+
// case T_DIR:
237+
// case T_FILE:
234238
case T_METHOD_C:
235239
case T_DNUMBER:
236240
case T_LNUMBER:
@@ -241,6 +245,14 @@ private function tokenize(string $source): array
241245
//case T_TRAIT_C:
242246
$newType = self::TOKEN_DEFAULT;
243247
break;
248+
// Constants
249+
case T_DIR:
250+
case T_FILE:
251+
$newType = self::TOKEN_CONSTANT;
252+
break;
253+
case T_VARIABLE:
254+
$newType = self::TOKEN_VARIABLE;
255+
break;
244256
default:
245257
// Compatibility with PHP 5.3
246258
if (defined('T_TRAIT_C') && $token[0] === T_TRAIT_C) {

0 commit comments

Comments
 (0)