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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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_#'
Expand Down
22 changes: 13 additions & 9 deletions src/Utils/PatchParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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
}
Expand All @@ -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])) {
Expand Down
Loading