Skip to content

Commit 2cef62b

Browse files
committed
build: drop php 7.1 and 7.2 support add php 8 support
1 parent 6ef3d4b commit 2cef62b

9 files changed

Lines changed: 66 additions & 39 deletions

File tree

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.php]
13+
indent_size = 4
14+
indent_style = space
15+
16+
[*.md]
17+
trim_trailing_whitespace = false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
composer.phar
33
composer.lock
44
vendor/
5+
56
phpunit.xml
7+
.phpunit.result.cache
68

79
.php_cs
810
.php_cs.cache

.php_cs.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $finder = PhpCsFixer\Finder::create()
77
return PhpCsFixer\Config::create()
88
->setRules([
99
'@PSR2' => true,
10+
'@PhpCsFixer' => true,
11+
'@PHP73Migration' => true,
1012
'array_syntax' => ['syntax' => 'short'],
1113
'combine_consecutive_issets' => true,
1214
'combine_consecutive_unsets' => true,
@@ -15,12 +17,14 @@ return PhpCsFixer\Config::create()
1517
'list_syntax' => ['syntax' => 'short'],
1618
// 'mb_str_functions' => true,
1719
'native_function_invocation' => true,
20+
'native_constant_invocation' => true,
1821
'no_null_property_initialization' => true,
1922
'no_useless_else' => true,
2023
'no_useless_return' => true,
2124
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
2225
'phpdoc_order' => true,
2326
'strict_comparison' => true,
27+
'combine_nested_dirname' => true,
2428
])
2529
->setRiskyAllowed(true)
2630
->setFinder($finder)

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
language: php
22
php:
3-
- 7.1
4-
- 7.2
53
- 7.3
64
- 7.4
5+
- 8.0
76
- nightly
87

98
matrix:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ print_r($resultCheckFile);
1919

2020
### Requirements:
2121

22-
- PHP >= 7.1.3
22+
- PHP >= 7.3
2323

2424
### Installation:
2525
```bash

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"description": "PHP code syntax checker.",
55
"license": "LGPL-3.0",
66
"require": {
7-
"php": "^7.1.3",
8-
"symfony/process": "^4.2|5.0"
7+
"php": ">=7.3",
8+
"symfony/process": "^4.4|^5.0"
99
},
1010
"require-dev": {
11-
"phpunit/phpunit": "^7.5",
12-
"friendsofphp/php-cs-fixer": "^2.16"
11+
"phpunit/phpunit": "^9.5",
12+
"friendsofphp/php-cs-fixer": "^2.17"
1313
},
1414
"authors": [
1515
{

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.5/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.5/phpunit.xsd"
55
colors="true"
66
bootstrap="vendor/autoload.php"
77
>

src/Php.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Syntax;
34

45
use Symfony\Component\Process\Process;
@@ -13,7 +14,6 @@ class Php
1314
private $sourceCharset;
1415
private $resultCharset = 'UTF-8';
1516

16-
1717
public function __construct()
1818
{
1919
$this->tempDirectory = \sys_get_temp_dir();
@@ -22,6 +22,7 @@ public function __construct()
2222
public function setResultCharset(string $resultCharset): self
2323
{
2424
$this->resultCharset = $resultCharset;
25+
2526
return $this;
2627
}
2728

@@ -33,6 +34,7 @@ public function getResultCharset(): string
3334
public function setSourceCharset(?string $sourceCharset): self
3435
{
3536
$this->sourceCharset = $sourceCharset;
37+
3638
return $this;
3739
}
3840

@@ -44,6 +46,7 @@ public function getSourceCharset(): ?string
4446
public function setCli(string $path): self
4547
{
4648
$this->cli = $path;
49+
4750
return $this;
4851
}
4952

@@ -55,6 +58,7 @@ public function getCli(): string
5558
public function setTempDirectory(string $path): self
5659
{
5760
$this->tempDirectory = $path;
61+
5862
return $this;
5963
}
6064

@@ -82,11 +86,11 @@ public function check(string $source): array
8286
throw new \Exception('Could not close temp file');
8387
}
8488

85-
8689
try {
8790
$result = $this->checkFile($file);
8891
} catch (\Throwable $e) {
8992
\unlink($file);
93+
9094
throw $e;
9195
}
9296

@@ -95,31 +99,20 @@ public function check(string $source): array
9599
return $this->formatCheckOutput($result);
96100
}
97101

98-
protected function formatCheckOutput(array $result): array
99-
{
100-
if (isset($result['errors'])) {
101-
\array_walk($result['errors'], static function (&$item) {
102-
$item['file'] = null;
103-
});
104-
}
105-
106-
return $result;
107-
}
108-
109102
public function checkFile(string $file): array
110103
{
111104
$result = $this->execute($file);
112105

113106
if (0 === $result['code']) {
114107
return [
115108
'validity' => true,
116-
'errors' => null
109+
'errors' => null,
117110
];
118111
}
119112

120113
$fullMessage = \preg_replace('/ in (?:.+) on line (?:[0-9]+)$/', '', $result['output']);
121114
\preg_match('/ on line ([0-9]+)$/', $result['output'], $matchLine);
122-
$line = isset($matchLine[1]) ? (int)($matchLine[1]) : null;
115+
$line = isset($matchLine[1]) ? (int) ($matchLine[1]) : null;
123116

124117
[$type, $message] = \explode(': ', $fullMessage);
125118

@@ -131,12 +124,28 @@ public function checkFile(string $file): array
131124
'code' => $result['code'],
132125
'line' => $line,
133126
'type' => $type,
134-
'message' => $this->convertMessage($message)
127+
'message' => $this->convertMessage($message),
135128
],
136129
],
137130
];
138131
}
139132

133+
public static function formatOutputHelper(string $source, int $line, string $cssCodeClass = 'syntax-code', string $cssCodeCorrectLineClass = 'syntax-correct-line', string $cssCodeIncorrectLineClass = 'syntax-incorrect-line'): string
134+
{
135+
return '<div class="'.\htmlspecialchars($cssCodeClass).'"><pre><code>'.self::formatCode($source, $line, $cssCodeCorrectLineClass, $cssCodeIncorrectLineClass).'</code></pre></div>';
136+
}
137+
138+
protected function formatCheckOutput(array $result): array
139+
{
140+
if (isset($result['errors'])) {
141+
\array_walk($result['errors'], static function (&$item) {
142+
$item['file'] = null;
143+
});
144+
}
145+
146+
return $result;
147+
}
148+
140149
protected function convertMessage(string $message): string
141150
{
142151
if (null !== $this->getSourceCharset()) {
@@ -169,11 +178,6 @@ protected function execute(string $file): array
169178
];
170179
}
171180

172-
public static function formatOutputHelper(string $source, int $line, string $cssCodeClass = 'syntax-code', string $cssCodeCorrectLineClass = 'syntax-correct-line', string $cssCodeIncorrectLineClass = 'syntax-incorrect-line'): string
173-
{
174-
return '<div class="' . \htmlspecialchars($cssCodeClass) . '"><pre><code>' . self::formatCode($source, $line, $cssCodeCorrectLineClass, $cssCodeIncorrectLineClass) . '</code></pre></div>';
175-
}
176-
177181
protected static function formatCode(string $source, int $line, string $cssCodeCorrectLineClass = 'syntax-correct-line', string $cssCodeIncorrectLineClass = 'syntax-incorrect-line'): string
178182
{
179183
$array = self::formatXhtmlHighlight($source);
@@ -183,7 +187,7 @@ protected static function formatCode(string $source, int $line, string $cssCodeC
183187
for ($i = 0; $i < $all; ++$i) {
184188
$next = $i + 1;
185189
$l = \strlen($next);
186-
$page .= '<span class="' . \htmlspecialchars($line === $next ? $cssCodeIncorrectLineClass : $cssCodeCorrectLineClass) . '">' . ($l < $len ? \str_repeat('&#160;', $len - $l) : '') . $next . '</span> ' . $array[$i] . "\n";
190+
$page .= '<span class="'.\htmlspecialchars($line === $next ? $cssCodeIncorrectLineClass : $cssCodeCorrectLineClass).'">'.($l < $len ? \str_repeat('&#160;', $len - $l) : '').$next.'</span> '.$array[$i]."\n";
187191
}
188192

189193
return $page;

tests/PhpTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
<?php
2+
23
namespace Syntax\Tests;
34

45
use PHPUnit\Framework\TestCase;
56
use Syntax\Php;
67

8+
/**
9+
* @internal
10+
* @coversNothing
11+
*/
712
class PhpTest extends TestCase
813
{
914
/**
1015
* @var Php
1116
*/
1217
private $syntax;
1318

14-
public function setUp()
19+
protected function setUp(): void
1520
{
1621
$this->syntax = new Php();
1722
}
@@ -23,15 +28,13 @@ public function testCheck()
2328
self::assertEquals(['validity' => true, 'errors' => null], $result);
2429
}
2530

26-
2731
public function testCheckFile()
2832
{
29-
$result = $this->syntax->checkFile(__DIR__ . '/fixtures/correct.php');
33+
$result = $this->syntax->checkFile(__DIR__.'/fixtures/correct.php');
3034

3135
self::assertEquals(['validity' => true, 'errors' => null], $result);
3236
}
3337

34-
3538
public function testCheckFail()
3639
{
3740
$result = $this->syntax->check('<?php echo "; ?>');
@@ -49,10 +52,9 @@ public function testCheckFail()
4952
self::assertTrue(\is_string($result['errors'][0]['message']));
5053
}
5154

52-
5355
public function testCheckFileFail()
5456
{
55-
$result = $this->syntax->checkFile(__DIR__ . '/fixtures/fail.php');
57+
$result = $this->syntax->checkFile(__DIR__.'/fixtures/fail.php');
5658

5759
self::assertTrue(\is_array($result));
5860

@@ -66,14 +68,13 @@ public function testCheckFileFail()
6668
self::assertTrue(\is_string($result['errors'][0]['type']));
6769
self::assertTrue(\is_string($result['errors'][0]['message']));
6870

69-
self::assertEquals(__DIR__ . '/fixtures/fail.php', $result['errors'][0]['file']);
71+
self::assertEquals(__DIR__.'/fixtures/fail.php', $result['errors'][0]['file']);
7072
}
7173

72-
7374
public function testFormatOutputHelper()
7475
{
7576
$result = Php::formatOutputHelper(
76-
'<?php echo ";' . "\n" . 'echo 1; ?>',
77+
'<?php echo ";'."\n".'echo 1; ?>',
7778
1
7879
);
7980

0 commit comments

Comments
 (0)