Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 490a110

Browse files
committed
Fix TraceableSerializer when collected caller from array map
If the TraceableSerializer runs from a callable in an array_map, then the caller looses the file and the line because it was invoked. This causes a hard fail since the required items are not defined. The error is the following: TraceableSerializer.php line 174: Warning: Undefined array key "file". The fix is to get the file and the line from the following array item which always is the caller class.
1 parent f8cca42 commit 490a110

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Symfony/Component/Serializer/Debug/TraceableSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ private function getCaller(string $method, string $interface): array
179179
&& $method === $trace[$i]['function']
180180
&& is_a($trace[$i]['class'], $interface, true)
181181
) {
182-
$file = $trace[$i]['file'];
183-
$line = $trace[$i]['line'];
182+
$file = $trace[$i]['file'] ?? $trace[$i + 1]['file'];
183+
$line = $trace[$i]['line'] ?? $trace[$i + 1]['line'];
184184

185185
break;
186186
}

src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ public function testAddDebugTraceIdInContext()
126126
$traceableSerializer->encode('data', 'format');
127127
$traceableSerializer->decode('data', 'format');
128128
}
129+
130+
public function testCollectedCaller()
131+
{
132+
$serializer = new \Symfony\Component\Serializer\Serializer();
133+
134+
$collector = new SerializerDataCollector();
135+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
136+
137+
$traceableSerializer->normalize('data');
138+
$collector->lateCollect();
139+
140+
$this->assertSame([
141+
'name' => 'TraceableSerializerTest.php',
142+
'file' => __FILE__,
143+
'line' => __LINE__ - 6,
144+
], $collector->getData()['normalize'][0]['caller']);
145+
}
146+
147+
public function testCollectedCallerFromArrayMap()
148+
{
149+
$serializer = new \Symfony\Component\Serializer\Serializer();
150+
151+
$collector = new SerializerDataCollector();
152+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
153+
154+
array_map([$traceableSerializer, 'normalize'], ['data']);
155+
$collector->lateCollect();
156+
157+
$this->assertSame([
158+
'name' => 'TraceableSerializerTest.php',
159+
'file' => __FILE__,
160+
'line' => __LINE__ - 6,
161+
], $collector->getData()['normalize'][0]['caller']);
162+
}
129163
}
130164

131165
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface

0 commit comments

Comments
 (0)