Skip to content

Commit 0f153b1

Browse files
siganushkanicolas-grekas
authored andcommitted
fix: fixed State contamination in marking stores due to class-based getter cache
1 parent 07f3560 commit 0f153b1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

MarkingStore/MethodMarkingStore.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getMarking(object $subject): Marking
5151
{
5252
$marking = null;
5353
try {
54-
$marking = ($this->getters[$subject::class] ??= $this->getGetter($subject))();
54+
$marking = ($this->getters[$subject::class] ??= $this->getGetter($subject))($subject);
5555
} catch (\Error $e) {
5656
$unInitializedPropertyMessage = \sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
5757
if ($e->getMessage() !== $unInitializedPropertyMessage) {
@@ -93,8 +93,8 @@ private function getGetter(object $subject): callable
9393
$method = 'get'.ucfirst($property);
9494

9595
return match (self::getType($subject, $property, $method)) {
96-
MarkingStoreMethod::METHOD => $subject->{$method}(...),
97-
MarkingStoreMethod::PROPERTY => static fn () => $subject->{$property},
96+
MarkingStoreMethod::METHOD => static fn ($subject) => $subject->{$method}(),
97+
MarkingStoreMethod::PROPERTY => static fn ($subject) => $subject->{$property},
9898
};
9999
}
100100

Tests/MarkingStore/MethodMarkingStoreTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ public function testGetMarkingWithUninitializedProperty2()
125125
$markingStore->getMarking($subject);
126126
}
127127

128+
public function testGetMarkingWithSameSubjectMultipleTimes()
129+
{
130+
$subject1 = new Subject('first_place');
131+
$subject2 = new Subject('second_place');
132+
$subject3 = new Subject('third_place');
133+
134+
$markingStore = new MethodMarkingStore(true);
135+
136+
$marking1 = $markingStore->getMarking($subject1);
137+
$marking2 = $markingStore->getMarking($subject2);
138+
$marking3 = $markingStore->getMarking($subject3);
139+
140+
$this->assertSame(['first_place' => 1], $marking1->getPlaces());
141+
$this->assertSame(['second_place' => 1], $marking2->getPlaces());
142+
$this->assertSame(['third_place' => 1], $marking3->getPlaces());
143+
}
144+
128145
private function createValueObject(string $markingValue): object
129146
{
130147
return new class($markingValue) {

0 commit comments

Comments
 (0)