Skip to content

Commit 800aa0e

Browse files
simbigSimon Bigelmayr
and
Simon Bigelmayr
authored
Add PHP 7.4 compatibility (#20)
* php 7.4 compatible code * more * github workflow * fix * revert require phpstan/phpstan 1.0. as identifiers in baseline is only available in 2.0: phpstan/phpstan-src@c8b7ea9 --------- Co-authored-by: Simon Bigelmayr <simon.bigelmayr@mll.com>
1 parent 2ac04e7 commit 800aa0e

10 files changed

+47
-26
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
php-version: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
34+
php-version: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
3535
dependency-version: [ prefer-lowest, prefer-stable ]
3636
steps:
3737
-

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"error identifier"
1414
],
1515
"require": {
16-
"php": "^8.0",
16+
"php": "^7.4 || ^8.0",
1717
"nette/neon": "^3.3.3 || ^4.0",
18-
"phpstan/phpstan": "^2.0.0"
18+
"phpstan/phpstan": "^2"
1919
},
2020
"require-dev": {
2121
"editorconfig-checker/editorconfig-checker": "10.6.0",

src/BaselinePerIdentifierFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private function getPathDifference(string $from, string $to): string
130130
$toParts = explode(DIRECTORY_SEPARATOR, $to);
131131

132132
// Find the common base path
133-
while (count($fromParts) > 0 && count($toParts) > 0 && ($fromParts[0] === $toParts[0])) {
133+
while ($fromParts !== [] && $toParts !== [] && ($fromParts[0] === $toParts[0])) {
134134
array_shift($fromParts);
135135
array_shift($toParts);
136136
}

src/BaselineSplitter.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
class BaselineSplitter
1919
{
2020

21-
public function __construct(
22-
private string $indent
23-
)
21+
private string $indent;
22+
23+
public function __construct(string $indent)
2424
{
25+
$this->indent = $indent;
2526
}
2627

2728
/**
@@ -97,9 +98,23 @@ private function groupErrorsByIdentifier(array $errors, string $folder): array
9798

9899
$identifier = $error['identifier'] ?? 'missing-identifier';
99100

100-
$message = $error['message'] ?? throw new ErrorException("Ignored error #$index is missing 'message'");
101-
$count = $error['count'] ?? throw new ErrorException("Ignored error #$index is missing 'count'");
102-
$path = $error['path'] ?? throw new ErrorException("Ignored error #$index is missing 'path'");
101+
if (!isset($error['message'])) {
102+
throw new ErrorException("Ignored error #$index is missing 'message'");
103+
}
104+
105+
$message = $error['message'];
106+
107+
if (!isset($error['count'])) {
108+
throw new ErrorException("Ignored error #$index is missing 'count'");
109+
}
110+
111+
$count = $error['count'];
112+
113+
if (!isset($error['path'])) {
114+
throw new ErrorException("Ignored error #$index is missing 'path'");
115+
}
116+
117+
$path = $error['path'];
103118

104119
assert(is_string($identifier));
105120
assert(is_string($message));

src/Handler/HandlerFactory.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ class HandlerFactory
1212
*/
1313
public static function create(string $extension): BaselineHandler
1414
{
15-
return match ($extension) {
16-
'neon' => new NeonBaselineHandler(),
17-
'php' => new PhpBaselineHandler(),
18-
default => throw new ErrorException("Invalid baseline file extension '$extension', expected neon or php file"),
19-
};
15+
switch ($extension) {
16+
case 'neon':
17+
return new NeonBaselineHandler();
18+
19+
case 'php':
20+
return new PhpBaselineHandler();
21+
22+
default:
23+
throw new ErrorException("Invalid baseline file extension '$extension', expected neon or php file");
24+
}
2025
}
2126

2227
}

src/Handler/NeonBaselineHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Nette\Neon\Neon;
77
use ShipMonk\PHPStan\Baseline\Exception\ErrorException;
88
use ShipMonk\PHPStan\Baseline\NeonHelper;
9-
use function get_debug_type;
9+
use function gettype;
1010
use function is_array;
1111

1212
class NeonBaselineHandler implements BaselineHandler
@@ -19,7 +19,7 @@ public function decodeBaseline(string $filepath): array
1919
$decoded = Neon::decodeFile($filepath);
2020

2121
if (!is_array($decoded)) {
22-
throw new ErrorException('Invalid neon file: root must be an array, ' . get_debug_type($decoded) . ' given');
22+
throw new ErrorException('Invalid neon file: root must be an array, ' . gettype($decoded) . ' given');
2323
}
2424

2525
return $decoded;

src/Handler/PhpBaselineHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use ShipMonk\PHPStan\Baseline\Exception\ErrorException;
66
use Throwable;
7-
use function get_debug_type;
7+
use function gettype;
88
use function is_array;
99
use function sprintf;
1010
use function var_export;
@@ -15,12 +15,10 @@ class PhpBaselineHandler implements BaselineHandler
1515
public function decodeBaseline(string $filepath): array
1616
{
1717
try {
18-
$decoded = (static function () use ($filepath): mixed {
19-
return require $filepath;
20-
})();
18+
$decoded = (static fn() => require $filepath)();
2119

2220
if (!is_array($decoded)) {
23-
throw new ErrorException("File '$filepath' must return array, " . get_debug_type($decoded) . ' given');
21+
throw new ErrorException("File '$filepath' must return array, " . gettype($decoded) . ' given');
2422
}
2523

2624
return $decoded;

src/NeonHelper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
class NeonHelper
1111
{
1212

13-
public static function encode(mixed $data, string $indent): string
13+
/**
14+
* @param mixed $data
15+
*/
16+
public static function encode($data, string $indent): string
1417
{
15-
return trim(Neon::encode($data, blockMode: true, indentation: $indent)) . "\n";
18+
return trim(Neon::encode($data, true, $indent)) . "\n";
1619
}
1720

1821
public static function escape(string $value): string

tests/Rule/BaselinePerIdentifierFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use function sys_get_temp_dir;
1111
use function uniqid;
1212

13-
class BaselinePerIdentifierFormatterTest extends PHPStanTestCase
13+
final class BaselinePerIdentifierFormatterTest extends PHPStanTestCase
1414
{
1515

1616
public function testFormat(): void

tests/SplitterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use function uniqid;
1515
use function var_export;
1616

17-
class SplitterTest extends TestCase
17+
final class SplitterTest extends TestCase
1818
{
1919

2020
public function testBinaryWithNeon(): void

0 commit comments

Comments
 (0)