Skip to content

Commit

Permalink
Modernize codebase for PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed May 7, 2024
1 parent 1f85545 commit 061776b
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 311 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

name: "Coding Standards"

on:
pull_request:
branches:
- "*.x"
push:
branches:
- "*.x"

jobs:
coding-standards:
name: "Coding Standards"
uses: "doctrine/.github/.github/workflows/[email protected]"
129 changes: 7 additions & 122 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: "Continuous Integration"

on:
Expand All @@ -9,125 +8,11 @@ on:
branches:
- "*.x"

env:
fail-fast: true

jobs:

unit-tests:
name: "Unit tests"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"

steps:
- name: "Checkout code"
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"

- name: "Test fixes"
run: "vendor/bin/phpunit --coverage-text"

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"

coding-standards:
name: "Coding Standards"
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=checkstyle | cs2pr"
phpunit:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/[email protected]"
with:
php-versions: '["8.1", "8.2", "8.3"]'
secrets:
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"
15 changes: 15 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

name: "Static Analysis"

on:
pull_request:
branches:
- "*.x"
push:
branches:
- "*.x"

jobs:
static-analysis:
name: "Static Analysis"
uses: "doctrine/.github/.github/workflows/[email protected]"
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.phpunit.result.cache
/.phpunit.cache
/.phpcs-cache
vendor/
/vendor
/composer.lock
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"license": "MIT",
"type": "library",
"require": {
"php": "^7.2 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"doctrine/coding-standard": "^9.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^8.5 || ^9.6",
"vimeo/psalm": "^4.11"
"doctrine/coding-standard": "^12",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"vimeo/psalm": "^5.24"
},
"authors": [
{
Expand Down
8 changes: 4 additions & 4 deletions examples/examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
Usage:
<pre>
<?php highlight_string(
'<?php' . "\n" . '$formatted = (new SqlFormatter())->format($sql);' . "\n" . '?>'
'<?php' . "\n" . '$formatted = (new SqlFormatter())->format($sql);' . "\n" . '?>',
); ?>
</pre>
</div>
Expand Down Expand Up @@ -195,7 +195,7 @@
<pre><?= $sql; ?></pre>
</td>
<td><pre><?= htmlentities((new SqlFormatter(
new NullHighlighter()
new NullHighlighter(),
))->format($sql)); ?></pre></td>
</tr>
<?php endforeach ?>
Expand All @@ -208,7 +208,7 @@
Usage:
<pre>
<?php highlight_string(
'<?php' . "\n" . '$highlighted = (new SqlFormatter())->highlight($sql);' . "\n" . '?>'
'<?php' . "\n" . '$highlighted = (new SqlFormatter())->highlight($sql);' . "\n" . '?>',
); ?>
</pre>
</div>
Expand All @@ -234,7 +234,7 @@
Usage:
<pre>
<?php highlight_string(
'<?php' . "\n" . '$compressed = (new SqlFormatter())->compress($sql);' . "\n" . '?>'
'<?php' . "\n" . '$compressed = (new SqlFormatter())->compress($sql);' . "\n" . '?>',
); ?>
</pre>
</div>
Expand Down
18 changes: 9 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<source>
<include>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<psalm
errorLevel="5"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
10 changes: 4 additions & 6 deletions src/CliHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ final class CliHighlighter implements Highlighter
public const HIGHLIGHT_FUNCTIONS = 'functions';

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

/**
* @param array<string, string> $escapeSequences
*/
/** @param array<string, string> $escapeSequences */
public function __construct(array $escapeSequences = [])
{
$this->escapeSequences = $escapeSequences + [
Expand Down Expand Up @@ -48,7 +46,7 @@ public function highlightToken(int $type, string $value): string
return $prefix . $value . "\x1b[0m";
}

private function prefix(int $type): ?string
private function prefix(int $type): string|null

Check warning on line 49 in src/CliHighlighter.php

View check run for this annotation

Codecov / codecov/patch

src/CliHighlighter.php#L49

Added line #L49 was not covered by tests
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -64,7 +62,7 @@ public function highlightError(string $value): string
PHP_EOL,
$this->escapeSequences[self::HIGHLIGHT_ERROR],
$value,
"\x1b[0m"
"\x1b[0m",

Check warning on line 65 in src/CliHighlighter.php

View check run for this annotation

Codecov / codecov/patch

src/CliHighlighter.php#L65

Added line #L65 was not covered by tests
);
}

Expand Down
20 changes: 7 additions & 13 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@

final class Cursor
{
/** @var int */
private $position = -1;
private int $position = -1;

/** @var Token[] */
private $tokens;

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

Check warning on line 12 in src/Cursor.php

View check run for this annotation

Codecov / codecov/patch

src/Cursor.php#L12

Added line #L12 was not covered by tests
private readonly array $tokens,
) {
}

public function next(?int $exceptTokenType = null): ?Token
public function next(int|null $exceptTokenType = null): Token|null

Check warning on line 17 in src/Cursor.php

View check run for this annotation

Codecov / codecov/patch

src/Cursor.php#L17

Added line #L17 was not covered by tests
{
while ($token = $this->tokens[++$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand All @@ -33,7 +27,7 @@ public function next(?int $exceptTokenType = null): ?Token
return null;
}

public function previous(?int $exceptTokenType = null): ?Token
public function previous(int|null $exceptTokenType = null): Token|null

Check warning on line 30 in src/Cursor.php

View check run for this annotation

Codecov / codecov/patch

src/Cursor.php#L30

Added line #L30 was not covered by tests
{
while ($token = $this->tokens[--$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand Down
21 changes: 8 additions & 13 deletions src/HtmlHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ final class HtmlHighlighter implements Highlighter
{
public const HIGHLIGHT_PRE = 'pre';

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

/** @var array<string, string> */
private $htmlAttributes;
private readonly 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 = [], bool $usePre = true)
{
public function __construct(

Check warning on line 26 in src/HtmlHighlighter.php

View check run for this annotation

Codecov / codecov/patch

src/HtmlHighlighter.php#L26

Added line #L26 was not covered by tests
array $htmlAttributes = [],
private readonly bool $usePre = true,
) {
$this->htmlAttributes = $htmlAttributes + [
self::HIGHLIGHT_QUOTE => 'style="color: blue;"',
self::HIGHLIGHT_BACKTICK_QUOTE => 'style="color: purple;"',
Expand All @@ -43,7 +39,6 @@ public function __construct(array $htmlAttributes = [], bool $usePre = true)
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 @@ -62,7 +57,7 @@ public function highlightToken(int $type, string $value): string
return '<span ' . $attributes . '>' . $value . '</span>';
}

public function attributes(int $type): ?string
public function attributes(int $type): string|null

Check warning on line 60 in src/HtmlHighlighter.php

View check run for this annotation

Codecov / codecov/patch

src/HtmlHighlighter.php#L60

Added line #L60 was not covered by tests
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -77,7 +72,7 @@ public function highlightError(string $value): string
'%s<span %s>%s</span>',
PHP_EOL,
$this->htmlAttributes[self::HIGHLIGHT_ERROR],
$value
$value,

Check warning on line 75 in src/HtmlHighlighter.php

View check run for this annotation

Codecov / codecov/patch

src/HtmlHighlighter.php#L75

Added line #L75 was not covered by tests
);
}

Expand Down
Loading

0 comments on commit 061776b

Please sign in to comment.