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

JBZoo / Composer-Diff / 9339764623

08 Feb 2024 09:20PM UTC coverage: 94.345%. Remained the same
9339764623

push

github

web-flow
Bump composer/composer from 2.6.6 to 2.7.0 (#34)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

317 of 336 relevant lines covered (94.35%)

74.11 hits per line

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

80.36
/src/Diff.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 Composer\Semver\Comparator;
20
use Composer\Semver\VersionParser;
21

22
final class Diff
23
{
24
    public const MODE_NEW        = 'New';
25
    public const MODE_REMOVED    = 'Removed';
26
    public const MODE_CHANGED    = 'Changed';
27
    public const MODE_UPGRADED   = 'Upgraded';
28
    public const MODE_DOWNGRADED = 'Downgraded';
29
    public const MODE_SAME       = 'Same';
30

31
    private string $mode          = self::MODE_SAME;
32
    private ?string $comparingUrl = null;
33
    private ?Package $target      = null;
34
    private ?Package $source;
35

36
    public function __construct(?Package $sourcePackage = null)
37
    {
38
        $this->source = $sourcePackage;
162✔
39
    }
40

41
    public function setMode(string $newMode): self
42
    {
43
        $this->mode = $newMode;
162✔
44

45
        return $this;
162✔
46
    }
47

48
    public function getMode(): string
49
    {
50
        return $this->mode;
162✔
51
    }
52

53
    public function toArray(): array
54
    {
55
        if ($this->source !== null) {
138✔
56
            return [
126✔
57
                'name'         => $this->source->getName(),
126✔
58
                'url'          => $this->source->getPackageUrl(),
126✔
59
                'version_from' => $this->source->getVersion(true),
126✔
60
                'version_to'   => $this->target?->getVersion(true),
126✔
61
                'mode'         => $this->mode,
126✔
62
                'compare'      => $this->comparingUrl,
126✔
63
            ];
126✔
64
        }
65

66
        if ($this->target !== null) {
90✔
67
            return [
90✔
68
                'name'         => $this->target->getName(),
90✔
69
                'url'          => $this->target->getPackageUrl(),
90✔
70
                'version_from' => null,
90✔
71
                'version_to'   => $this->target->getVersion(true),
90✔
72
                'mode'         => $this->mode,
90✔
73
                'compare'      => $this->comparingUrl,
90✔
74
            ];
90✔
75
        }
76

77
        throw new Exception('Source and target packages are not defined');
×
78
    }
79

80
    public function compareWithPackage(Package $targetPackage): self
81
    {
82
        $this->target = $targetPackage;
150✔
83

84
        if ($this->source === null) {
150✔
85
            return $this->setMode(self::MODE_NEW);
90✔
86
        }
87

88
        if ($this->source->getName() !== $this->target->getName()) {
138✔
89
            throw new Exception(
×
90
                "Can't compare versions of different packages. " .
×
91
                "Source:{$this->source->getName()}; Target:{$this->target->getName()};",
×
92
            );
×
93
        }
94

95
        $sourceVersion      = $this->source->getVersion();
138✔
96
        $targetVersion      = $this->target->getVersion();
138✔
97
        $this->comparingUrl = $this->getComparingUrl($sourceVersion, $targetVersion);
138✔
98

99
        if ($sourceVersion === $targetVersion) {
138✔
100
            return $this->setMode(self::MODE_SAME);
102✔
101
        }
102

103
        if (self::isHashVersion($sourceVersion) || self::isHashVersion($targetVersion)) {
114✔
104
            return $this->setMode(self::MODE_CHANGED);
78✔
105
        }
106

107
        $parser = new VersionParser();
102✔
108

109
        $normalizedSource = $parser->normalize($sourceVersion);
102✔
110
        $normalizedTarget = $parser->normalize($targetVersion);
102✔
111

112
        if (Comparator::greaterThan($normalizedSource, $normalizedTarget)) {
102✔
113
            return $this->setMode(self::MODE_DOWNGRADED);
78✔
114
        }
115

116
        if (Comparator::lessThan($normalizedSource, $normalizedTarget)) {
90✔
117
            return $this->setMode(self::MODE_UPGRADED);
90✔
118
        }
119

120
        return $this->setMode(self::MODE_CHANGED);
×
121
    }
122

123
    public function getComparingUrl(?string $fromVersion, ?string $toVersion): ?string
124
    {
125
        if (\in_array($fromVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) {
138✔
126
            return '';
×
127
        }
128

129
        if (\in_array($toVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) {
138✔
130
            return '';
×
131
        }
132

133
        if ($this->source !== null) {
138✔
134
            return Url::getCompareUrl($this->source->getSourceUrl(), $fromVersion, $toVersion);
138✔
135
        }
136

137
        if ($this->target !== null) {
×
138
            return Url::getCompareUrl($this->target->getSourceUrl(), $fromVersion, $toVersion);
×
139
        }
140

141
        throw new Exception('Source and target packages are not defined');
×
142
    }
143

144
    private static function isHashVersion(string $version): bool
145
    {
146
        return \strlen($version) === Package::HASH_LENGTH && !\str_contains($version, '.');
114✔
147
    }
148
}
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