Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
Revert "Modernize codebase for PHP 8"
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 8, 2024
1 parent 69e4144 commit 71fe98d
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 84 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@ on:
branches:
- "*.x"
push:
branches:
- "*.x"

jobs:
coding-standards:
name: "Coding Standards"
uses: "doctrine/.github/.github/workflows/coding-standards.yml@5.0.1"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"

steps:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "cs2pr"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"

# Remove -q when updating to phpcs v4
- name: "Run squizlabs/php_codesniffer"
run: |
vendor/bin/phpcs -q --no-colors --report=diff || true
vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr
42 changes: 33 additions & 9 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,38 @@ on:
branches:
- "*.x"
push:
branches:
- "*.x"

env:
fail-fast: false

jobs:
phpunit:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/continuous-integration.yml@5.0.1"
with:
php-versions: '["8.1", "8.2", "8.3"]'
secrets:
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"
unit-tests:
name: "Unit tests"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"

steps:
- name: "Checkout code"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"

- name: "Test fixes"
run: "vendor/bin/phpunit --coverage-text"
64 changes: 59 additions & 5 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,64 @@ on:
branches:
- "*.x"
push:
branches:
- "*.x"

env:
fail-fast: false

jobs:
static-analysis:
name: "Static Analysis"
uses: "doctrine/.github/.github/workflows/static-analysis.yml@5.0.1"
static-analysis-phpstan:
name: "Static Analysis with PHPStan"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"

steps:
- name: "Checkout code"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
extensions: "mbstring"
tools: "cs2pr"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"


static-analysis-psalm:
name: "Static Analysis with Psalm"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"

steps:
- name: "Checkout code"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"

- name: "Run a static analysis with vimeo/psalm"
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=4"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.phpunit.cache
/.phpunit.result.cache
/.phpcs-cache
/vendor
/composer.lock
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"license": "MIT",
"type": "library",
"require": {
"php": "^8.1"
"php": "^7.4 || ^8.0"
},
"require-dev": {
"doctrine/coding-standard": "^12",
"doctrine/coding-standard": "^9.0",
"ergebnis/phpunit-slow-test-detector": "^2.14",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"vimeo/psalm": "^5.24"
"phpunit/phpunit": "^8.5 || ^9.6",
"vimeo/psalm": "^4.11"
},
"authors": [
{
Expand Down
7 changes: 2 additions & 5 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors" />

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<config name="php_version" value="80100"/>
<!-- Show progress of the run -->
<arg value="p"/>

<rule ref="Doctrine"/>

Expand Down
11 changes: 6 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
colors="true"
verbose="true">
cacheDirectory=".phpunit.cache"
>
<testsuites>
Expand All @@ -18,9 +19,9 @@
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>

<source>
<include>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</include>
</source>
</whitelist>
</filter>
</phpunit>
4 changes: 2 additions & 2 deletions src/CliHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function highlightToken(int $type, string $value): string
}

/** @param Token::TOKEN_TYPE_* $type */
private function prefix(int $type): string|null
private function prefix(int $type): ?string
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -61,7 +61,7 @@ public function highlightError(string $value): string
"\n",
$this->escapeSequences[self::HIGHLIGHT_ERROR],
$value,
"\x1b[0m",
"\x1b[0m"
);
}

Expand Down
17 changes: 11 additions & 6 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ final class Cursor
{
private int $position = -1;

/** @param list<Token> $tokens */
public function __construct(
private readonly array $tokens,
) {
/** @var Token[] */
private array $tokens;

/**
* @param Token[] $tokens
*/
public function __construct(array $tokens)
{
$this->tokens = $tokens;
}

/** @param Token::TOKEN_TYPE_* $exceptTokenType */
public function next(int|null $exceptTokenType = null): Token|null
public function next(?int $exceptTokenType = null): ?Token
{
while ($token = $this->tokens[++$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand All @@ -29,7 +34,7 @@ public function next(int|null $exceptTokenType = null): Token|null
}

/** @param Token::TOKEN_TYPE_* $exceptTokenType */
public function previous(int|null $exceptTokenType = null): Token|null
public function previous(?int $exceptTokenType = null): ?Token
{
while ($token = $this->tokens[--$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand Down
17 changes: 9 additions & 8 deletions src/HtmlHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ final class HtmlHighlighter implements Highlighter
{
public const HIGHLIGHT_PRE = 'pre';

/** This flag tells us if queries need to be enclosed in <pre> tags */
private bool $usePre;

/** @var array<string, string> */
private readonly array $htmlAttributes;
private array $htmlAttributes;

/**
* @param array<string, string> $htmlAttributes
* @param bool $usePre This flag tells us if queries need to be enclosed in <pre> tags
*/
public function __construct(
array $htmlAttributes = [],
private readonly bool $usePre = true,
) {
public function __construct(array $htmlAttributes = [], bool $usePre = true)
{
$this->htmlAttributes = $htmlAttributes + [
self::HIGHLIGHT_QUOTE => 'style="color: blue;"',
self::HIGHLIGHT_BACKTICK_QUOTE => 'style="color: purple;"',
Expand All @@ -38,6 +38,7 @@ public function __construct(
self::HIGHLIGHT_VARIABLE => 'style="color: orange;"',
self::HIGHLIGHT_PRE => 'style="color: black; background-color: white;"',
];
$this->usePre = $usePre;
}

public function highlightToken(int $type, string $value): string
Expand All @@ -57,7 +58,7 @@ public function highlightToken(int $type, string $value): string
}

/** @param Token::TOKEN_TYPE_* $type */
public function attributes(int $type): string|null
public function attributes(int $type): ?string
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -72,7 +73,7 @@ public function highlightError(string $value): string
'%s<span %s>%s</span>',
"\n",
$this->htmlAttributes[self::HIGHLIGHT_ERROR],
$value,
$value
);
}

Expand Down
18 changes: 9 additions & 9 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

final class SqlFormatter
{
private readonly Highlighter $highlighter;
private readonly Tokenizer $tokenizer;
private Highlighter $highlighter;
private Tokenizer $tokenizer;

private const INDENT_TYPE_BLOCK = 'block';
private const INDENT_TYPE_SPECIAL = 'special';

public function __construct(Highlighter|null $highlighter = null)
public function __construct(?Highlighter $highlighter = null)
{
$this->tokenizer = new Tokenizer();
$this->highlighter = $highlighter ?? (PHP_SAPI === 'cli' ? new CliHighlighter() : new HtmlHighlighter());
Expand Down Expand Up @@ -101,7 +101,7 @@ public function format(string $string, string $indentString = ' '): string
while ($token = $cursor->next(Token::TOKEN_TYPE_WHITESPACE)) {
$highlighted = $this->highlighter->highlightToken(
$token->type(),
$token->value(),
$token->value()
);

// If we are increasing the special indent level now
Expand Down Expand Up @@ -209,7 +209,7 @@ public function format(string $string, string $indentString = ' '): string
Token::TOKEN_TYPE_RESERVED_TOPLEVEL,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_COMMENT,
Token::TOKEN_TYPE_BLOCK_COMMENT,
Token::TOKEN_TYPE_BLOCK_COMMENT
)
) {
break;
Expand Down Expand Up @@ -383,7 +383,7 @@ public function format(string $string, string $indentString = ' '): string
Token::TOKEN_TYPE_QUOTE,
Token::TOKEN_TYPE_BACKTICK_QUOTE,
Token::TOKEN_TYPE_WORD,
Token::TOKEN_TYPE_NUMBER,
Token::TOKEN_TYPE_NUMBER
)
) {
continue;
Expand All @@ -396,7 +396,7 @@ public function format(string $string, string $indentString = ' '): string
if (array_search(self::INDENT_TYPE_BLOCK, $indentTypes) !== false) {
$return = rtrim($return, ' ');
$return .= $this->highlighter->highlightErrorMessage(
'WARNING: unclosed parentheses or section',
'WARNING: unclosed parentheses or section'
);
}

Expand All @@ -422,7 +422,7 @@ public function highlight(string $string): string
while ($token = $cursor->next()) {
$return .= $this->highlighter->highlightToken(
$token->type(),
$token->value(),
$token->value()
);
}

Expand Down Expand Up @@ -454,7 +454,7 @@ public function compress(string $string): string
$token->isOfType(
Token::TOKEN_TYPE_RESERVED,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_RESERVED_TOPLEVEL,
Token::TOKEN_TYPE_RESERVED_TOPLEVEL
)
) {
$newValue = preg_replace('/\s+/', ' ', $token->value());
Expand Down
Loading

0 comments on commit 71fe98d

Please sign in to comment.