diff --git a/.github/workflows/cs.yml b/.github/workflows/cs.yml index 3367e74..2bee7ce 100644 --- a/.github/workflows/cs.yml +++ b/.github/workflows/cs.yml @@ -38,7 +38,7 @@ jobs: run: vendor/bin/phpcs --report-full --report-checkstyle=./checkstyle.xml - name: Show PHPCS results in PR - run: cs2pr ./checkstyle.xml --graceful-warnings + run: cs2pr ./checkstyle.xml - name: Make sure no vardumps remain run: composer vardumpcheck diff --git a/composer.json b/composer.json index e9759e9..7f42803 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0", "php-parallel-lint/php-parallel-lint": "^1.0", "php-parallel-lint/php-var-dump-check": "0.*", - "squizlabs/php_codesniffer": "^3.5", - "php-parallel-lint/php-code-style": "^1.0" + "php-parallel-lint/php-code-style": "^2.0" }, "replace": { "jakub-onderka/php-console-highlighter": "*" diff --git a/examples/snippet.php b/examples/snippet.php index f7f69f3..b27b832 100644 --- a/examples/snippet.php +++ b/examples/snippet.php @@ -1,4 +1,5 @@ getCodeSnippet($fileContent, 3); \ No newline at end of file +echo $highlighter->getCodeSnippet($fileContent, 3); diff --git a/examples/whole_file.php b/examples/whole_file.php index 8cd3023..614b164 100644 --- a/examples/whole_file.php +++ b/examples/whole_file.php @@ -1,4 +1,5 @@ getWholeFile($fileContent); \ No newline at end of file +echo $highlighter->getWholeFile($fileContent); diff --git a/examples/whole_file_line_numbers.php b/examples/whole_file_line_numbers.php index 1416425..b5781ba 100644 --- a/examples/whole_file_line_numbers.php +++ b/examples/whole_file_line_numbers.php @@ -1,4 +1,5 @@ getWholeFileWithLineNumbers($fileContent); \ No newline at end of file +echo $highlighter->getWholeFileWithLineNumbers($fileContent); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index d07e229..4decb09 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,8 +1,20 @@ - + A coding standard for Jakub Onderka's projects. - ./src/ + + + + . + + + */build/* + */vendor/* @@ -13,9 +25,23 @@ - + + + + + + + + + + + + + */tests/* + - - warning - diff --git a/src/Highlighter.php b/src/Highlighter.php index 13cb251..4ca7750 100644 --- a/src/Highlighter.php +++ b/src/Highlighter.php @@ -1,4 +1,5 @@ 'dark_gray', ); + /** @var array */ + private $phpTagTokens = array( + T_OPEN_TAG => T_OPEN_TAG, + T_OPEN_TAG_WITH_ECHO => T_OPEN_TAG_WITH_ECHO, + T_CLOSE_TAG => T_CLOSE_TAG, + ); + + /** @var array */ + private $magicConstantTokens = array( + T_DIR => T_DIR, + T_FILE => T_FILE, + T_LINE => T_LINE, + T_CLASS_C => T_CLASS_C, + T_FUNC_C => T_FUNC_C, + T_METHOD_C => T_METHOD_C, + T_NS_C => T_NS_C, + T_TRAIT_C => T_TRAIT_C, + ); + + /** @var array */ + private $miscTokens = array( + T_STRING => T_STRING, // Labels. + T_VARIABLE => T_VARIABLE, + T_DNUMBER => T_DNUMBER, // Floats. + T_LNUMBER => T_LNUMBER, // Integers. + ); + + /** @var array */ + private $commentTokens = array( + T_COMMENT => T_COMMENT, + T_DOC_COMMENT => T_DOC_COMMENT, + ); + + /** @var array */ + private $textStringTokens = array( + T_ENCAPSED_AND_WHITESPACE => T_ENCAPSED_AND_WHITESPACE, + T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING, + ); + + /** @var array */ + private $htmlTokens = array( + T_INLINE_HTML => T_INLINE_HTML, + ); + /** * @param ConsoleColor $color * @throws \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException @@ -151,50 +196,36 @@ private function tokenize($source) */ private function getTokenType($arrayToken) { - switch ($arrayToken[0]) { - case T_OPEN_TAG: - case T_OPEN_TAG_WITH_ECHO: - case T_CLOSE_TAG: - case T_STRING: - case T_VARIABLE: - - // Constants - case T_DIR: - case T_FILE: - case T_METHOD_C: - case T_DNUMBER: - case T_LNUMBER: - case T_NS_C: - case T_LINE: - case T_CLASS_C: - case T_FUNC_C: - case T_TRAIT_C: + switch (true) { + case isset($this->phpTagTokens[$arrayToken[0]]): + case isset($this->magicConstantTokens[$arrayToken[0]]): + case isset($this->miscTokens[$arrayToken[0]]): return self::TOKEN_DEFAULT; - case T_COMMENT: - case T_DOC_COMMENT: + case isset($this->commentTokens[$arrayToken[0]]): return self::TOKEN_COMMENT; - case T_ENCAPSED_AND_WHITESPACE: - case T_CONSTANT_ENCAPSED_STRING: + case isset($this->textStringTokens[$arrayToken[0]]): return self::TOKEN_STRING; - case T_INLINE_HTML: + case isset($this->htmlTokens[$arrayToken[0]]): return self::TOKEN_HTML; } + // phpcs:disable PHPCompatibility.Constants.NewConstants -- The new token constants are only used when defined. + // Handle PHP >= 8.0 namespaced name tokens. // https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.tokenizer - if (defined('T_NAME_QUALIFIED') && $arrayToken[0] === T_NAME_QUALIFIED) { - return self::TOKEN_DEFAULT; - } - if (defined('T_NAME_FULLY_QUALIFIED') && $arrayToken[0] === T_NAME_FULLY_QUALIFIED) { - return self::TOKEN_DEFAULT; - } - if (defined('T_NAME_RELATIVE') && $arrayToken[0] === T_NAME_RELATIVE) { + if ( + (defined('T_NAME_QUALIFIED') && $arrayToken[0] === T_NAME_QUALIFIED) + || (defined('T_NAME_FULLY_QUALIFIED') && $arrayToken[0] === T_NAME_FULLY_QUALIFIED) + || (defined('T_NAME_RELATIVE') && $arrayToken[0] === T_NAME_RELATIVE) + ) { return self::TOKEN_DEFAULT; } + // phpcs:enable + return self::TOKEN_KEYWORD; } diff --git a/tests/HighlighterTest.php b/tests/HighlighterTest.php index 29a8ebb..4c1fb36 100644 --- a/tests/HighlighterTest.php +++ b/tests/HighlighterTest.php @@ -1,8 +1,8 @@ compare( - '' - , + '', '' ); } @@ -276,8 +275,7 @@ public function testEmpty() public function testWhitespace() { $this->compare( - ' ' - , + ' ', ' ' ); }