1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Roave\BackwardCompatibility; |
6
|
|
|
|
7
|
|
|
use Assert\Assert; |
8
|
|
|
use function sprintf; |
9
|
|
|
use function strtoupper; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @todo this class probably needs subclassing or being turned into an interface |
14
|
|
|
*/ |
15
|
|
|
final class Change |
16
|
|
|
{ |
17
|
|
|
private const ADDED = 'added'; |
18
|
|
|
private const CHANGED = 'changed'; |
19
|
|
|
private const REMOVED = 'removed'; |
20
|
|
|
private const SKIPPED = 'skipped'; |
21
|
|
|
|
22
|
|
|
/** @var string[] */ |
23
|
|
|
private static $validModificationTypes = [self::ADDED, self::CHANGED, self::REMOVED, self::SKIPPED]; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $modificationType; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $description; |
30
|
|
|
|
31
|
|
|
/** @var bool */ |
32
|
|
|
private $isBcBreak; |
33
|
|
|
|
34
|
|
|
private function __construct(string $modificationType, string $description, bool $isBcBreak) |
35
|
|
|
{ |
36
|
|
|
Assert::that($modificationType)->inArray(self::$validModificationTypes); |
37
|
|
|
$this->modificationType = $modificationType; |
38
|
|
|
$this->description = $description; |
39
|
|
|
$this->isBcBreak = $isBcBreak; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function added(string $description, bool $isBcBreak) : self |
43
|
|
|
{ |
44
|
|
|
return new self(self::ADDED, $description, $isBcBreak); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function changed(string $description, bool $isBcBreak) : self |
48
|
|
|
{ |
49
|
|
|
return new self(self::CHANGED, $description, $isBcBreak); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function removed(string $description, bool $isBcBreak) : self |
53
|
|
|
{ |
54
|
|
|
return new self(self::REMOVED, $description, $isBcBreak); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function skippedDueToFailure(Throwable $failure) : self |
58
|
|
|
{ |
59
|
|
|
// @TODO Note: we may consider importing the full exception for better printing later on |
60
|
|
|
return new self(self::SKIPPED, $failure->getMessage(), true); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isAdded() : bool |
64
|
|
|
{ |
65
|
|
|
return $this->modificationType === self::ADDED; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isRemoved() : bool |
69
|
|
|
{ |
70
|
|
|
return $this->modificationType === self::REMOVED; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function isChanged() : bool |
74
|
|
|
{ |
75
|
|
|
return $this->modificationType === self::CHANGED; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function isSkipped() : bool |
79
|
|
|
{ |
80
|
|
|
return $this->modificationType === self::SKIPPED; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function __toString() : string |
84
|
|
|
{ |
85
|
|
|
return sprintf( |
86
|
|
|
'%s%s: %s', |
87
|
|
|
$this->isBcBreak ? '[BC] ' : ' ', |
88
|
|
|
strtoupper($this->modificationType), |
89
|
|
|
$this->description |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|