|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased; |
|
6
|
|
|
|
|
7
|
|
|
use Roave\ApiCompare\Change; |
|
8
|
|
|
use Roave\ApiCompare\Changes; |
|
9
|
|
|
use Roave\ApiCompare\Formatter\ReflectionPropertyName; |
|
10
|
|
|
use Roave\BetterReflection\Reflection\ReflectionProperty; |
|
11
|
|
|
use function sprintf; |
|
12
|
|
|
|
|
13
|
|
|
final class PropertyVisibilityReduced implements PropertyBased |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ReflectionPropertyName */ |
|
16
|
|
|
private $formatProperty; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->formatProperty = new ReflectionPropertyName(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private const VISIBILITY_PRIVATE = 'private'; |
|
24
|
|
|
|
|
25
|
|
|
private const VISIBILITY_PROTECTED = 'protected'; |
|
26
|
|
|
|
|
27
|
|
|
private const VISIBILITY_PUBLIC = 'public'; |
|
28
|
|
|
|
|
29
|
|
|
public function compare(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes |
|
30
|
|
|
{ |
|
31
|
|
|
$visibilityFrom = $this->propertyVisibility($fromProperty); |
|
32
|
|
|
$visibilityTo = $this->propertyVisibility($toProperty); |
|
33
|
|
|
|
|
34
|
|
|
// Works because private, protected and public are sortable: |
|
35
|
|
|
if ($visibilityFrom <= $visibilityTo) { |
|
36
|
|
|
return Changes::new(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return Changes::fromArray([Change::changed( |
|
40
|
|
|
sprintf( |
|
41
|
|
|
'Property %s visibility reduced from %s to %s', |
|
42
|
|
|
$this->formatProperty->__invoke($fromProperty), |
|
43
|
|
|
$visibilityFrom, |
|
44
|
|
|
$visibilityTo |
|
45
|
|
|
), |
|
46
|
|
|
true |
|
47
|
|
|
), |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function propertyVisibility(ReflectionProperty $property) : string |
|
52
|
|
|
{ |
|
53
|
|
|
if ($property->isPublic()) { |
|
54
|
|
|
return self::VISIBILITY_PUBLIC; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($property->isProtected()) { |
|
58
|
|
|
return self::VISIBILITY_PROTECTED; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return self::VISIBILITY_PRIVATE; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|