Skip to content

Commit 8ffd1b6

Browse files
committed
Check all properties for visibility change
1 parent d2aca03 commit 8ffd1b6

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Exercise/PhpPromotion.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,23 @@ public function check(Input $input): ResultInterface
9191
return Failure::fromNameAndReason($this->getName(), 'Property "config" should not be promoted');
9292
}
9393

94+
$expectedProperties = [
95+
'basePath' => 'protected',
96+
'key' => 'private',
97+
'visitor' => 'private',
98+
];
99+
94100
$properties = collect($reflectionClass->getProperties());
95-
$properties = $properties->flatMap(fn (\ReflectionProperty $prop) => [$prop->getName() => $prop]);
96-
$private = $properties->filter(fn (\ReflectionProperty $prop) => $prop->isPrivate());
101+
$properties = $properties->flatMap(fn (\ReflectionProperty $prop) => [
102+
$prop->getName() => $this->getPropertyVisibility($prop)
103+
])->getArrayCopy();
104+
ksort($properties);
97105

98-
if ($notPrivate = array_diff(['visitor', 'key'], $private->keys()->getArrayCopy())) {
106+
if ($changedVisibility = array_keys(array_diff_assoc($expectedProperties, $properties))) {
99107
return Failure::fromNameAndReason($this->getName(), pluralise(
100-
'Visibility changed for property %s',
101-
$notPrivate,
102-
implode('" & "', $notPrivate)
108+
'Visibility changed for property "%s"',
109+
$changedVisibility,
110+
implode('" & "', $changedVisibility)
103111
));
104112
}
105113

@@ -143,4 +151,13 @@ public function check(Input $input): ResultInterface
143151

144152
return new Success($this->getName());
145153
}
154+
155+
private function getPropertyVisibility(\ReflectionProperty $prop): string
156+
{
157+
return match (true) {
158+
$prop->isPrivate() => 'private',
159+
$prop->isProtected() => 'protected',
160+
default => 'public',
161+
};
162+
}
146163
}

test/Exercise/PhpPromotionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testFailureWhenPropertyVisibilityChanges()
7272

7373
$this->assertVerifyWasNotSuccessful();
7474

75-
$this->assertResultsHasFailure(Failure::class, 'Visibility changed for properties "key" & "basePath"');
75+
$this->assertResultsHasFailure(Failure::class, 'Visibility changed for properties "basePath" & "key"');
7676
}
7777

7878
public function testBadPropertyPromotion()

0 commit comments

Comments
 (0)