diff --git a/README.md b/README.md index f352a03..c610f4a 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ vendor/bin/coverage-guard init ## Optional dependencies - Libraries: - `phpunit/php-code-coverage` for loading coverage cov files - - `sebastian/diff` for processing diff/patch files + - `sebastian/diff` (5.1+) for processing diff/patch files - PHP extensions: - `ext-libxml` and `ext-simplexml` for loading coverage XML files - `ext-dom` for `check` and `merge` commands diff --git a/composer.json b/composer.json index 1cb0edc..23bdb5a 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "phpstan/phpstan-strict-rules": "2.0.11", "phpunit/php-code-coverage": "^10.1", "phpunit/phpunit": "~10.5.58", - "sebastian/diff": "^4.0 || ^5.0 || ^6.0 || ^7.0", + "sebastian/diff": "^5.1.1 || ^6.0 || ^7.0", "shipmonk/coding-standard": "^0.2.4", "shipmonk/composer-dependency-analyser": "1.8.4", "shipmonk/dead-code-detector": "^1.0", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d26b1ea..0f94974 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -72,12 +72,6 @@ parameters: - src/Extractor/PhpUnitCoverageExtractor.php - tests/Extractor/PhpUnitCoverageExtractorTest.php - # support even old sebastian/diff - - - message: "#Call to function method_exists\\(\\) with SebastianBergmann\\\\Diff.*#" - paths: - - src/Utils/PatchParser.php - # support even old nikic/php-parser - message: '#PhpParser\\Node\\Stmt\\Throw_#' diff --git a/src/Utils/PatchParser.php b/src/Utils/PatchParser.php index 0d6bae0..e54a1c3 100644 --- a/src/Utils/PatchParser.php +++ b/src/Utils/PatchParser.php @@ -12,10 +12,10 @@ use function file_exists; use function file_get_contents; use function is_file; -use function method_exists; use function str_ends_with; use function str_starts_with; use function substr; +use function version_compare; use const DIRECTORY_SEPARATOR; final class PatchParser @@ -50,6 +50,12 @@ public function getPatchChangedLines( throw new ErrorException('In order to use --patch mode, you need to install sebastian/diff'); } + $diffVersion = InstalledVersions::getPrettyVersion('sebastian/diff'); + + if ($diffVersion !== null && version_compare($diffVersion, '5.1', '<')) { + throw new ErrorException("In order to use --patch mode, you need sebastian/diff 5.1 or newer, {$diffVersion} is installed"); + } + $gitRoot = $this->resolveGitRoot($config); $patchContent = file_get_contents($patchFile); @@ -61,7 +67,7 @@ public function getPatchChangedLines( $changes = []; foreach ($diffs as $diff) { - $diffTo = method_exists($diff, 'to') ? $diff->to() : $diff->getTo(); + $diffTo = $diff->to(); if ($diffTo === '/dev/null') { continue; // deleted file } @@ -79,14 +85,12 @@ public function getPatchChangedLines( $changes[$realPath] = []; - $diffChunks = method_exists($diff, 'chunks') ? $diff->chunks() : $diff->getChunks(); - foreach ($diffChunks as $chunk) { - $lineNumber = method_exists($chunk, 'end') ? $chunk->end() : $chunk->getEnd(); - $chunkLines = method_exists($chunk, 'lines') ? $chunk->lines() : $chunk->getLines(); + foreach ($diff->chunks() as $chunk) { + $lineNumber = $chunk->end(); - foreach ($chunkLines as $line) { - $lineType = method_exists($line, 'type') ? $line->type() : $line->getType(); - $lineContent = method_exists($line, 'content') ? $line->content() : $line->getContent(); + foreach ($chunk->lines() as $line) { + $lineType = $line->type(); + $lineContent = $line->content(); if ($lineType === Line::ADDED) { if (!isset($actualFileLines[$lineNumber - 1])) {