Skip to content

Commit 15611fe

Browse files
authored
Merge pull request #253 from Roave/fix/#202-removed-protected-properties-of-final-classes-are-not-bc-breaks
Fix #202 - `PropertyRemoved` should only operate on `public` properties for `final` classes
2 parents 8a3d4a3 + de333c4 commit 15611fe

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/DetectChanges/BCBreak/ClassBased/PropertyRemoved.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass):
4545
/** @return ReflectionProperty[] */
4646
private function accessibleProperties(ReflectionClass $class): array
4747
{
48-
return array_filter($class->getProperties(), function (ReflectionProperty $property): bool {
48+
$classIsOpen = ! $class->isFinal();
49+
50+
return array_filter($class->getProperties(), function (ReflectionProperty $property) use ($classIsOpen): bool {
4951
return ($property->isPublic()
50-
|| $property->isProtected())
52+
|| ($classIsOpen && $property->isProtected()))
5153
&& ! $this->isInternalDocComment($property->getDocComment());
5254
});
5355
}

test/unit/DetectChanges/BCBreak/ClassBased/PropertyRemovedTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Roave\BetterReflection\Reflection\ReflectionClass;
1212
use Roave\BetterReflection\Reflector\ClassReflector;
1313
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
14+
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
1415

1516
use function array_map;
1617
use function iterator_to_array;
@@ -64,6 +65,41 @@ public function classesToBeTested(): array
6465
'[BC] REMOVED: Property RoaveTestAsset\ClassWithPropertiesBeingRemoved#$nameCaseChangeProtectedProperty was removed',
6566
],
6667
],
68+
'Decreased property visibility / removed properties in a final class - only `public` properties affect BC' => [
69+
(new ClassReflector(new StringSourceLocator(
70+
<<<'PHP'
71+
<?php
72+
73+
final class FinalClass
74+
{
75+
public $decreasedVisibilityPublicProperty;
76+
public $removedPublicProperty;
77+
protected $decreasedVisibilityProtectedProperty;
78+
protected $removedProtectedProperty;
79+
private $privateProperty;
80+
}
81+
PHP
82+
,
83+
$locator
84+
)))->reflect('FinalClass'),
85+
(new ClassReflector(new StringSourceLocator(
86+
<<<'PHP'
87+
<?php
88+
89+
final class FinalClass
90+
{
91+
protected $decreasedVisibilityPublicProperty;
92+
private $decreasedVisibilityProtectedProperty;
93+
}
94+
PHP
95+
,
96+
$locator
97+
)))->reflect('FinalClass'),
98+
[
99+
'[BC] REMOVED: Property FinalClass#$decreasedVisibilityPublicProperty was removed',
100+
'[BC] REMOVED: Property FinalClass#$removedPublicProperty was removed',
101+
],
102+
],
67103
];
68104
}
69105
}

0 commit comments

Comments
 (0)