Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PHP-Code-Style 2.0 #29

Merged
merged 4 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
3 changes: 2 additions & 1 deletion examples/snippet.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

Expand All @@ -7,4 +8,4 @@
$highlighter = new Highlighter(new ConsoleColor());

$fileContent = file_get_contents(__FILE__);
echo $highlighter->getCodeSnippet($fileContent, 3);
echo $highlighter->getCodeSnippet($fileContent, 3);
3 changes: 2 additions & 1 deletion examples/whole_file.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

Expand All @@ -7,4 +8,4 @@
$highlighter = new Highlighter(new ConsoleColor());

$fileContent = file_get_contents(__FILE__);
echo $highlighter->getWholeFile($fileContent);
echo $highlighter->getWholeFile($fileContent);
3 changes: 2 additions & 1 deletion examples/whole_file_line_numbers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

Expand All @@ -7,4 +8,4 @@
$highlighter = new Highlighter(new ConsoleColor());

$fileContent = file_get_contents(__FILE__);
echo $highlighter->getWholeFileWithLineNumbers($fileContent);
echo $highlighter->getWholeFileWithLineNumbers($fileContent);
38 changes: 32 additions & 6 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Jakub Onderka Coding Standard">
<ruleset name="PHP-Console-Highlighter">
<description>A coding standard for Jakub Onderka's projects.</description>

<file>./src/</file>
<!--
#############################################################################
COMMAND LINE ARGUMENTS
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
#############################################################################
-->

<!-- Scan all files. -->
<file>.</file>

<!-- Exclude dependencies and auto-generated files. -->
<exclude-pattern>*/build/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- Only check PHP files. -->
<arg name="extensions" value="php"/>
Expand All @@ -13,9 +25,23 @@
<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<rule ref="./vendor/php-parallel-lint/php-code-style/ruleset.xml"/>
<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>


<!--
#############################################################################
USE THE PHPParallelLint RULESET
#############################################################################
-->

<!-- Set the supported PHP versions for PHPCompatibility (included in PHPParallelLint). -->
<config name="testVersion" value="5.4-"/>

<rule ref="PHPParallelLint"/>

<rule ref="Generic.Files.LineLength">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
<type>warning</type>
</rule>
</ruleset>
91 changes: 61 additions & 30 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PHP_Parallel_Lint\PhpConsoleHighlighter;

use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
Expand Down Expand Up @@ -29,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
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions tests/HighlighterTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace PHP_Parallel_Lint\PhpConsoleHighlighter\Test;

use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

use PHPUnit\Framework\TestCase;

class HighlighterTest extends TestCase
Expand Down Expand Up @@ -267,17 +267,15 @@ public function testHashComment()
public function testEmpty()
{
$this->compare(
''
,
'',
''
);
}

public function testWhitespace()
{
$this->compare(
' '
,
' ',
'<token_html> </token_html>'
);
}
Expand Down