From 586859bc70bf004befd1f6361412f4ad063fc566 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 12 Dec 2021 03:37:07 +0100 Subject: [PATCH 1/4] PHPCS: use PHP-Code-Style 2.0 * Composer: require `php-parallel-lint/php-code-style` at 2.0 or higher. * Composer: remove the requirement for PHPCS. This will now be inherited from the code style repo. --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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": "*" From 24b6da466a3807b0ffd9538b70703ee187dc6212 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 12 Dec 2021 03:38:30 +0100 Subject: [PATCH 2/4] Ruleset: various tweaks for PHP-Code-Style 2.0 * Scan all PHP files, not just the file in `src` for improved consistency across the code base, but exclude the `vendor` and the `build` (code coverage) directories. * Enable parallel scanning for faster results. * Use the ruleset from PHP-Code-Style by name. * Set the `testVersion` for use with PHPCompatibility. * Exclude the `LineLength` sniff for the tests. * Include minimal documentation in the ruleset. --- phpcs.xml.dist | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index d07e229..32a48e5 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,27 @@ - + + + + + + + + + + + + + warning + + + + */tests/* + - - warning - From 0f73c93875c343b44bd2b0b95f973ee23380e3e6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 12 Dec 2021 03:40:17 +0100 Subject: [PATCH 3/4] CS: various minor fixes Fix up the issues which will be flagged by PHP-Code-Style 2.0 and explicitly ignore a couple of (non-)issues. Includes no longer allowing warnings in the GH Actions build. --- .github/workflows/cs.yml | 2 +- examples/snippet.php | 3 ++- examples/whole_file.php | 3 ++- examples/whole_file_line_numbers.php | 3 ++- src/Highlighter.php | 6 +++++- tests/HighlighterTest.php | 8 +++----- 6 files changed, 15 insertions(+), 10 deletions(-) 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/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/src/Highlighter.php b/src/Highlighter.php index 13cb251..5b0e100 100644 --- a/src/Highlighter.php +++ b/src/Highlighter.php @@ -1,4 +1,5 @@ = 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) { @@ -195,6 +197,8 @@ private function getTokenType($arrayToken) 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( - ' ' - , + ' ', ' ' ); } From 378d0b8595589e5f4326a5df135b05a306dde8c6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 19 Dec 2021 07:06:27 +0100 Subject: [PATCH 4/4] Highlighter::getTokenType(): refactor to lower complexity Rewrite the method to use predefined token lists. Includes removing the PHPCS ruleset tweak put in place in an earlier commit. Fixes 11 --- phpcs.xml.dist | 4 --- src/Highlighter.php | 85 +++++++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 32a48e5..4decb09 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -40,10 +40,6 @@ - - warning - - */tests/* diff --git a/src/Highlighter.php b/src/Highlighter.php index 5b0e100..4ca7750 100644 --- a/src/Highlighter.php +++ b/src/Highlighter.php @@ -30,6 +30,50 @@ class Highlighter self::LINE_NUMBER => '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 @@ -152,34 +196,19 @@ 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; } @@ -187,13 +216,11 @@ private function getTokenType($arrayToken) // 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; }