Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [ '8.1', '8.2', '8.3', '8.4' ]
php-version: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
-
Expand Down
8 changes: 6 additions & 2 deletions bin/inline-phpstan-ignores
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ try {
$io = new Io();
$input = $io->readInput();
$comment = $io->readCliComment($argv);
$errorsData = json_decode($input, associative: true, flags: JSON_THROW_ON_ERROR);
$errors = $errorsData['files'] ?? throw new FailureException('No \'files\' key found on input JSON.');
$errorsData = json_decode($input, true, 512, JSON_THROW_ON_ERROR);
$errors = $errorsData['files'] ?? null;

if ($errors === null) {
throw new FailureException('No \'files\' key found on input JSON.');
}

$inliner = new InlineIgnoreInliner($io);
$inliner->inlineErrors($errors, $comment);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"error identifier"
],
"require": {
"php": "^8.1"
"php": "^7.4 || ^8.0"
},
"require-dev": {
"editorconfig-checker/editorconfig-checker": "^10.7.0",
Expand All @@ -22,7 +22,7 @@
"phpstan/phpstan": "^2.1.17",
"phpstan/phpstan-phpunit": "^2.0.6",
"phpstan/phpstan-strict-rules": "^2.0.4",
"phpunit/phpunit": "^10.5.46",
"phpunit/phpunit": "^9.6.23",
"shipmonk/coding-standard": "^0.1.3",
"shipmonk/phpstan-rules": "^4.1.2",
"slevomat/coding-standard": "^8.18.0"
Expand Down
6 changes: 4 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<exclude-pattern>tests/data/*</exclude-pattern>

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

<rule ref="ShipMonkCodingStandard"/>
<rule ref="ShipMonkCodingStandard">
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenAnnotations.AnnotationForbidden"/><!-- It removes @dataProvider, but PHPUnit 9 does not yet have #[DataProvider] -->
</rule>
</ruleset>
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
cacheResultFile="cache/phpunit.result.cache"
Expand Down
11 changes: 7 additions & 4 deletions src/InlineIgnoreInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
use function explode;
use function implode;
use function rtrim;
use function str_contains;
use function strlen;
use function strpos;
use function substr;

final class InlineIgnoreInliner
{

public function __construct(private Io $io)
private Io $io;

public function __construct(Io $io)
{
$this->io = $io;
}

/**
Expand All @@ -23,7 +26,7 @@ public function __construct(private Io $io)
*/
public function inlineErrors(
array $errors,
?string $comment,
?string $comment
): void
{
foreach ($errors as $filePath => $fileErrors) {
Expand All @@ -46,7 +49,7 @@ public function inlineErrors(
? ''
: " ($comment)";

$append = str_contains($lineContent, '// @phpstan-ignore ')
$append = strpos($lineContent, '// @phpstan-ignore ') !== false
? ', ' . $identifier . $resolvedComment
: ' // @phpstan-ignore ' . $identifier . $resolvedComment;

Expand Down
6 changes: 3 additions & 3 deletions src/Io.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use function in_array;
use function is_array;
use function is_string;
use function str_starts_with;
use function stream_get_contents;
use function strpos;
use const STDIN;

class Io
Expand All @@ -24,7 +24,7 @@ class Io
public function readCliComment(array $argv): ?string
{
foreach (array_slice($argv, 1) as $arg) {
if (str_starts_with($arg, '--') && !str_starts_with($arg, '--comment')) {
if (strpos($arg, '--') === 0 && strpos($arg, '--comment') !== 0) {
throw new FailureException('Unexpected option: ' . $arg);
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public function readFile(string $filePath): array
*/
public function writeFile(
string $filePath,
string $contents,
string $contents
): void
{
if (file_put_contents($filePath, $contents) === false) {
Expand Down
17 changes: 10 additions & 7 deletions tests/InlineIgnoreInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ShipMonk\PHPStan\Errors;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function file;
use function file_get_contents;
Expand All @@ -15,10 +14,12 @@
class InlineIgnoreInlinerTest extends TestCase
{

#[DataProvider('lineEndingProvider')]
/**
* @dataProvider lineEndingProvider
*/
public function testInlineErrors(
string $lineEnding,
?string $comment,
?string $comment
): void
{
$tmpFilePath = sys_get_temp_dir() . '/' . uniqid('ignore', true) . '.php';
Expand All @@ -42,12 +43,14 @@ public function testInlineErrors(
});
$ioMock->expects(self::exactly(2))
->method('readFile')
->willReturnCallback(static function (string $filePath) use ($tmpFilePath): array|false {
return file($tmpFilePath);
->willReturnCallback(static function (string $filePath) use ($tmpFilePath): array {
$lines = file($tmpFilePath);
self::assertIsArray($lines);
return $lines;
});

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

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

private function getTestFileContent(
string $filename,
string $lineEnding,
string $lineEnding
): string
{
$content = file_get_contents(__DIR__ . '/data/' . $filename);
Expand Down
12 changes: 6 additions & 6 deletions tests/IoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ShipMonk\PHPStan\Errors;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function fclose;
use function fwrite;
Expand All @@ -17,18 +16,19 @@ class IoTest extends TestCase

/**
* @param list<string> $args
*
* @dataProvider optionsProvider
*/
#[DataProvider('optionsProvider')]
public function testValidCliOptions(
public function testCliOptions(
int $exitCode,
string $input,
array $args,
string $expectedOutput,
string $expectedOutput
): void
{
$result = $this->runCliCommand($args, $input);
self::assertSame($exitCode, $result['exitCode']);
self::assertStringContainsString($expectedOutput, $result['stdout']);
self::assertSame($exitCode, $result['exitCode']);
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ public static function optionsProvider(): array
*/
private function runCliCommand(
array $args,
string $input,
string $input
): array
{
$binaryPath = __DIR__ . '/../bin/inline-phpstan-ignores';
Expand Down