Skip to content

Commit db03909

Browse files
authored
Support PHP 7.4+ (#7)
1 parent 9c7bf0e commit db03909

File tree

9 files changed

+41
-28
lines changed

9 files changed

+41
-28
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.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
-

bin/inline-phpstan-ignores

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ try {
2323
$io = new Io();
2424
$input = $io->readInput();
2525
$comment = $io->readCliComment($argv);
26-
$errorsData = json_decode($input, associative: true, flags: JSON_THROW_ON_ERROR);
27-
$errors = $errorsData['files'] ?? throw new FailureException('No \'files\' key found on input JSON.');
26+
$errorsData = json_decode($input, true, 512, JSON_THROW_ON_ERROR);
27+
$errors = $errorsData['files'] ?? null;
28+
29+
if ($errors === null) {
30+
throw new FailureException('No \'files\' key found on input JSON.');
31+
}
2832

2933
$inliner = new InlineIgnoreInliner($io);
3034
$inliner->inlineErrors($errors, $comment);

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"error identifier"
1414
],
1515
"require": {
16-
"php": "^8.1"
16+
"php": "^7.4 || ^8.0"
1717
},
1818
"require-dev": {
1919
"editorconfig-checker/editorconfig-checker": "^10.7.0",
@@ -22,7 +22,7 @@
2222
"phpstan/phpstan": "^2.1.17",
2323
"phpstan/phpstan-phpunit": "^2.0.6",
2424
"phpstan/phpstan-strict-rules": "^2.0.4",
25-
"phpunit/phpunit": "^10.5.46",
25+
"phpunit/phpunit": "^9.6.23",
2626
"shipmonk/coding-standard": "^0.1.3",
2727
"shipmonk/phpstan-rules": "^4.1.2",
2828
"slevomat/coding-standard": "^8.18.0"

phpcs.xml.dist

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
<exclude-pattern>tests/data/*</exclude-pattern>
1313

1414
<config name="installed_paths" value="vendor/slevomat/coding-standard,vendor/shipmonk/coding-standard"/>
15-
<config name="php_version" value="80100"/>
15+
<config name="php_version" value="70400"/>
1616

17-
<rule ref="ShipMonkCodingStandard"/>
17+
<rule ref="ShipMonkCodingStandard">
18+
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden"/><!-- It removes @dataProvider, but PHPUnit 9 does not yet have #[DataProvider] -->
19+
</rule>
1820
</ruleset>

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
bootstrap="vendor/autoload.php"
66
beStrictAboutOutputDuringTests="true"
77
beStrictAboutChangesToGlobalState="true"
8-
beStrictAboutCoverageMetadata="true"
8+
beStrictAboutCoversAnnotation="true"
9+
beStrictAboutTodoAnnotatedTests="true"
910
failOnRisky="true"
1011
failOnWarning="true"
1112
cacheResultFile="cache/phpunit.result.cache"

src/InlineIgnoreInliner.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
use function explode;
66
use function implode;
77
use function rtrim;
8-
use function str_contains;
98
use function strlen;
9+
use function strpos;
1010
use function substr;
1111

1212
final class InlineIgnoreInliner
1313
{
1414

15-
public function __construct(private Io $io)
15+
private Io $io;
16+
17+
public function __construct(Io $io)
1618
{
19+
$this->io = $io;
1720
}
1821

1922
/**
@@ -23,7 +26,7 @@ public function __construct(private Io $io)
2326
*/
2427
public function inlineErrors(
2528
array $errors,
26-
?string $comment,
29+
?string $comment
2730
): void
2831
{
2932
foreach ($errors as $filePath => $fileErrors) {
@@ -46,7 +49,7 @@ public function inlineErrors(
4649
? ''
4750
: " ($comment)";
4851

49-
$append = str_contains($lineContent, '// @phpstan-ignore ')
52+
$append = strpos($lineContent, '// @phpstan-ignore ') !== false
5053
? ', ' . $identifier . $resolvedComment
5154
: ' // @phpstan-ignore ' . $identifier . $resolvedComment;
5255

src/Io.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use function in_array;
1010
use function is_array;
1111
use function is_string;
12-
use function str_starts_with;
1312
use function stream_get_contents;
13+
use function strpos;
1414
use const STDIN;
1515

1616
class Io
@@ -24,7 +24,7 @@ class Io
2424
public function readCliComment(array $argv): ?string
2525
{
2626
foreach (array_slice($argv, 1) as $arg) {
27-
if (str_starts_with($arg, '--') && !str_starts_with($arg, '--comment')) {
27+
if (strpos($arg, '--') === 0 && strpos($arg, '--comment') !== 0) {
2828
throw new FailureException('Unexpected option: ' . $arg);
2929
}
3030
}
@@ -86,7 +86,7 @@ public function readFile(string $filePath): array
8686
*/
8787
public function writeFile(
8888
string $filePath,
89-
string $contents,
89+
string $contents
9090
): void
9191
{
9292
if (file_put_contents($filePath, $contents) === false) {

tests/InlineIgnoreInlinerTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ShipMonk\PHPStan\Errors;
44

5-
use PHPUnit\Framework\Attributes\DataProvider;
65
use PHPUnit\Framework\TestCase;
76
use function file;
87
use function file_get_contents;
@@ -15,10 +14,12 @@
1514
class InlineIgnoreInlinerTest extends TestCase
1615
{
1716

18-
#[DataProvider('lineEndingProvider')]
17+
/**
18+
* @dataProvider lineEndingProvider
19+
*/
1920
public function testInlineErrors(
2021
string $lineEnding,
21-
?string $comment,
22+
?string $comment
2223
): void
2324
{
2425
$tmpFilePath = sys_get_temp_dir() . '/' . uniqid('ignore', true) . '.php';
@@ -42,12 +43,14 @@ public function testInlineErrors(
4243
});
4344
$ioMock->expects(self::exactly(2))
4445
->method('readFile')
45-
->willReturnCallback(static function (string $filePath) use ($tmpFilePath): array|false {
46-
return file($tmpFilePath);
46+
->willReturnCallback(static function (string $filePath) use ($tmpFilePath): array {
47+
$lines = file($tmpFilePath);
48+
self::assertIsArray($lines);
49+
return $lines;
4750
});
4851

4952
$testJson = file_get_contents(__DIR__ . '/data/errors.json');
50-
$testData = json_decode($testJson, associative: true)['files']; // @phpstan-ignore argument.type
53+
$testData = json_decode($testJson, true)['files']; // @phpstan-ignore argument.type
5154

5255
$inliner = new InlineIgnoreInliner($ioMock);
5356
$inliner->inlineErrors($testData, $comment);
@@ -57,7 +60,7 @@ public function testInlineErrors(
5760

5861
private function getTestFileContent(
5962
string $filename,
60-
string $lineEnding,
63+
string $lineEnding
6164
): string
6265
{
6366
$content = file_get_contents(__DIR__ . '/data/' . $filename);

tests/IoTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ShipMonk\PHPStan\Errors;
44

5-
use PHPUnit\Framework\Attributes\DataProvider;
65
use PHPUnit\Framework\TestCase;
76
use function fclose;
87
use function fwrite;
@@ -17,18 +16,19 @@ class IoTest extends TestCase
1716

1817
/**
1918
* @param list<string> $args
19+
*
20+
* @dataProvider optionsProvider
2021
*/
21-
#[DataProvider('optionsProvider')]
22-
public function testValidCliOptions(
22+
public function testCliOptions(
2323
int $exitCode,
2424
string $input,
2525
array $args,
26-
string $expectedOutput,
26+
string $expectedOutput
2727
): void
2828
{
2929
$result = $this->runCliCommand($args, $input);
30-
self::assertSame($exitCode, $result['exitCode']);
3130
self::assertStringContainsString($expectedOutput, $result['stdout']);
31+
self::assertSame($exitCode, $result['exitCode']);
3232
}
3333

3434
/**
@@ -58,7 +58,7 @@ public static function optionsProvider(): array
5858
*/
5959
private function runCliCommand(
6060
array $args,
61-
string $input,
61+
string $input
6262
): array
6363
{
6464
$binaryPath = __DIR__ . '/../bin/inline-phpstan-ignores';

0 commit comments

Comments
 (0)