|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; |
|
6
|
|
|
|
|
7
|
|
|
use Roave\BackwardCompatibility\Change; |
|
8
|
|
|
use Roave\BackwardCompatibility\Changes; |
|
9
|
|
|
use Roave\BackwardCompatibility\Formatter\ReflectionPropertyName; |
|
10
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
|
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionProperty; |
|
12
|
|
|
use function array_diff; |
|
13
|
|
|
use function array_filter; |
|
14
|
|
|
use function array_keys; |
|
15
|
|
|
use function array_map; |
|
16
|
|
|
use function Safe\preg_match; |
|
17
|
|
|
use function Safe\sprintf; |
|
18
|
|
|
|
|
19
|
|
|
final class PropertyRemoved implements ClassBased |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ReflectionPropertyName */ |
|
22
|
|
|
private $formatProperty; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->formatProperty = new ReflectionPropertyName(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes |
|
30
|
|
|
{ |
|
31
|
|
|
$fromProperties = $this->accessibleProperties($fromClass); |
|
32
|
|
|
$removedProperties = array_diff( |
|
33
|
|
|
array_keys($fromProperties), |
|
34
|
|
|
array_keys($this->accessibleProperties($toClass)) |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
return Changes::fromList(...array_map(function (string $property) use ($fromProperties) : Change { |
|
38
|
|
|
return Change::removed( |
|
39
|
|
|
sprintf('Property %s was removed', $this->formatProperty->__invoke($fromProperties[$property])), |
|
40
|
|
|
true |
|
41
|
|
|
); |
|
42
|
|
|
}, $removedProperties)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @return ReflectionProperty[] */ |
|
46
|
|
|
private function accessibleProperties(ReflectionClass $class) : array |
|
47
|
|
|
{ |
|
48
|
|
|
return array_filter($class->getProperties(), function (ReflectionProperty $property) : bool { |
|
49
|
|
|
return ($property->isPublic() |
|
50
|
|
|
|| $property->isProtected()) |
|
51
|
|
|
&& ! $this->isInternalDocComment($property->getDocComment()); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function isInternalDocComment(string $comment) : bool |
|
56
|
|
|
{ |
|
57
|
|
|
return preg_match('/\s+@internal\s+/', $comment) === 1; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|