• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Composer-Diff / 5920911726

16 Aug 2023 06:22PM UTC coverage: 94.294%. Remained the same
5920911726

push

github

web-flow
Upgrade `jbzoo/cli:^7.1.1` (#26)

1 of 1 new or added line in 1 file covered. (100.0%)

314 of 333 relevant lines covered (94.29%)

49.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.5
/src/Comparator.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Composer-Diff.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Composer-Diff
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\ComposerDiff;
18

19
use Symfony\Component\Process\Exception\ProcessFailedException;
20
use Symfony\Component\Process\Process;
21

22
use function JBZoo\Data\json;
23

24
final class Comparator
25
{
26
    public const ENV_BOTH = 'both';
27
    public const ENV_PROD = 'require';
28
    public const ENV_DEV  = 'require-dev';
29

30
    public static function compare(string $sourceFile, string $targetFile): array
31
    {
32
        $sourceData = self::load($sourceFile);
108✔
33
        $targetData = self::load($targetFile);
108✔
34

35
        return [
108✔
36
            self::ENV_PROD => self::diff(self::ENV_PROD, $sourceData, $targetData),
108✔
37
            self::ENV_DEV  => self::diff(self::ENV_DEV, $sourceData, $targetData),
108✔
38
        ];
108✔
39
    }
40

41
    /**
42
     * @return Diff[]
43
     */
44
    private static function diff(string $type, ComposerLock $sourceData, ComposerLock $targetData): array
45
    {
46
        if ($type === self::ENV_PROD) {
108✔
47
            $sourcePackages = $sourceData->getRequired();
108✔
48
            $targetPackages = $targetData->getRequired();
108✔
49
        } else {
50
            $sourcePackages = $sourceData->getRequiredDev();
108✔
51
            $targetPackages = $targetData->getRequiredDev();
108✔
52
        }
53

54
        /** @var Diff[] $resultDiff */
55
        $resultDiff = [];
108✔
56

57
        foreach ($sourcePackages as $package) {
108✔
58
            $resultDiff[$package->getName()] = (new Diff($package))->setMode(Diff::MODE_REMOVED);
100✔
59
        }
60

61
        foreach ($targetPackages as $targetName => $targetPackage) {
108✔
62
            if (!\array_key_exists($targetName, $resultDiff)) {
100✔
63
                $resultDiff[$targetName] = (new Diff())->setMode(Diff::MODE_NEW);
60✔
64
            }
65

66
            $resultDiff[$targetName]->compareWithPackage($targetPackage);
100✔
67
        }
68

69
        $resultDiff = \array_filter(
108✔
70
            $resultDiff,
108✔
71
            static fn (Diff $diff) => $diff->getMode() !== Diff::MODE_SAME,
108✔
72
            \ARRAY_FILTER_USE_BOTH,
108✔
73
        );
108✔
74

75
        \ksort($resultDiff, \SORT_NATURAL);
108✔
76

77
        return $resultDiff;
108✔
78
    }
79

80
    private static function load(string $composerFile): ComposerLock
81
    {
82
        if (
83
            Url::isUrl($composerFile)
108✔
84
            && !\in_array(\parse_url($composerFile, \PHP_URL_SCHEME), \stream_get_wrappers(), true)
108✔
85
        ) {
86
            throw new Exception("There is no stream wrapper to open \"{$composerFile}\"");
×
87
        }
88

89
        if (\file_exists($composerFile)) {
108✔
90
            $json = json(\file_get_contents($composerFile));
108✔
91

92
            return new ComposerLock($json->getArrayCopy());
108✔
93
        }
94

95
        if (\str_contains($composerFile, ':')) {
4✔
96
            $json = json(self::exec('git show ' . \escapeshellarg($composerFile)));
4✔
97

98
            return new ComposerLock($json->getArrayCopy());
4✔
99
        }
100

101
        throw new Exception("Composer lock file \"{$composerFile}\" not found");
×
102
    }
103

104
    private static function exec(string $command): string
105
    {
106
        $process = Process::fromShellCommandline($command);
4✔
107
        $process->run();
4✔
108

109
        if ($process->isSuccessful()) {
4✔
110
            return $process->getOutput();
4✔
111
        }
112

113
        throw new ProcessFailedException($process);
×
114
    }
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc