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

JBZoo / Composer-Diff / 6669924894

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

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

79.25
/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

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

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

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

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

44
        return $this;
108✔
45
    }
46

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

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

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

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

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

83
        if ($this->source === null) {
100✔
84
            return $this->setMode(self::MODE_NEW);
60✔
85
        }
86

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

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

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

102
        if (self::isHashVersion($sourceVersion) || self::isHashVersion($targetVersion)) {
76✔
103
            return $this->setMode(self::MODE_CHANGED);
52✔
104
        }
105

106
        if (Comparator::greaterThan($sourceVersion, $targetVersion)) {
68✔
107
            return $this->setMode(self::MODE_DOWNGRADED);
52✔
108
        }
109

110
        if (Comparator::lessThan($sourceVersion, $targetVersion)) {
60✔
111
            return $this->setMode(self::MODE_UPGRADED);
60✔
112
        }
113

114
        return $this->setMode(self::MODE_CHANGED);
×
115
    }
116

117
    public function getComparingUrl(?string $fromVersion, ?string $toVersion): ?string
118
    {
119
        if (\in_array($fromVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) {
92✔
120
            return '';
×
121
        }
122

123
        if (\in_array($toVersion, [self::MODE_REMOVED, self::MODE_NEW], true)) {
92✔
124
            return '';
×
125
        }
126

127
        if ($this->source !== null) {
92✔
128
            return Url::getCompareUrl($this->source->getSourceUrl(), $fromVersion, $toVersion);
92✔
129
        }
130

131
        if ($this->target !== null) {
×
132
            return Url::getCompareUrl($this->target->getSourceUrl(), $fromVersion, $toVersion);
×
133
        }
134

135
        throw new Exception('Source and target packages are not defined');
×
136
    }
137

138
    private static function isHashVersion(string $version): bool
139
    {
140
        return \strlen($version) === Package::HASH_LENGTH && !\str_contains($version, '.');
76✔
141
    }
142
}
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