Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 6eca23f

Browse files
committed
Improve reqs comparator
- now is known detailed difference between required and installed packages
1 parent b7e85cc commit 6eca23f

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/Checker.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace JakubBoucek\ComposerConsistency;
@@ -77,11 +78,11 @@ public function isReqsValid(): bool
7778
$definitions = $this->loadReqs();
7879
$instalations = $this->loadInstalled();
7980

80-
$diff = array_diff_assoc($definitions, $instalations);
81+
$diff = $this->compareReqs($definitions, $instalations);
8182

8283
return count($diff) === 0;
8384
} catch (FileReadException $e) {
84-
if($this->strictReqs !== true && $e->getRequiredfile() === self::FILE_REQS) {
85+
if ($this->strictReqs !== true && $e->getRequiredfile() === self::FILE_REQS) {
8586
// Ignore missing `composer.lock` file - not deployed to production?
8687
return true;
8788
}
@@ -210,4 +211,34 @@ protected function parseJsonArray(string $json): array
210211
}
211212
return $value;
212213
}
214+
215+
/**
216+
* @param array $required
217+
* @param array $installed
218+
* @return array
219+
*/
220+
protected function compareReqs(array $required, array $installed): array
221+
{
222+
$diffs = array_map(
223+
static function ($version) {
224+
return ['required' => $version, 'installed' => null];
225+
},
226+
$required
227+
);
228+
229+
foreach ($installed as $name => $version) {
230+
if (($diffs[$name]['required'] ?? null) === $version) {
231+
// Version matches, remove diff
232+
unset($diffs[$name]);
233+
234+
}else {
235+
$diffs[$name] = [
236+
'required' => $diffs[$name]['required'] ?? null,
237+
'installed' => $version
238+
];
239+
}
240+
}
241+
242+
return $diffs;
243+
}
213244
}

0 commit comments

Comments
 (0)