Skip to content

Commit f37f783

Browse files
authored
Merge pull request #47 from netlogix/fix/expect-empty-arguments
2 parents 55cfee2 + 5a8dc89 commit f37f783

2 files changed

Lines changed: 127 additions & 5 deletions

File tree

Classes/Scope/Extra/VariablesFromStackProvider.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,39 @@ private function collectDataFromTrace(array $trace): Traversable
7777
if ($traceFunction !== $configFunction) {
7878
continue;
7979
}
80+
81+
yield from self::buildReadableRepresentationOfTrace($configFunction, $argumentPaths, $trace);
82+
}
83+
}
84+
85+
/**
86+
* @internal This method is only public because mocking \Throwable::getTrace() is not possible.
87+
*
88+
* @param string $callablePattern
89+
* @param array<string, string> $argumentPaths
90+
* @param array<mixed> $trace
91+
* @return Traversable<mixed>
92+
*/
93+
public static function buildReadableRepresentationOfTrace(string $callablePattern, array $argumentPaths, array $trace): Traversable
94+
{
95+
if (!isset($trace['args'])) {
96+
yield [$callablePattern => ['💥' => 'Argument tracing is disabled. To enable, set `zend.exception_ignore_args=0` ⚙️']];
97+
} else {
8098
$values = [];
8199
foreach ($argumentPaths as $argumentPathName => $argumentPathLookup) {
82100
try {
83-
$values[$argumentPathName] = $this->representationSerialize(
101+
$values[$argumentPathName] = self::representationSerialize(
84102
ObjectAccess::getPropertyPath($trace['args'], $argumentPathLookup)
85103
);
86104
} catch (Throwable $t) {
87105
$values[$argumentPathName] = '👻';
88106
}
89107
}
90-
yield [$configFunction => $values];
108+
yield [$callablePattern => $values];
91109
}
92110
}
93111

94-
private function representationSerialize($value)
112+
private static function representationSerialize($value)
95113
{
96114
static $representationSerialize;
97115

@@ -136,8 +154,14 @@ private function getSettings(): Traversable
136154

137155
$reflection = new MethodReflection($className, $methodName);
138156
foreach ($reflection->getParameters() as $parameter) {
139-
$search = sprintf('/^%s./', $parameter->getName());
140-
$replace = sprintf('%d.', $parameter->getPosition());
157+
$search = [
158+
sprintf('/^%s\\./', $parameter->getName()),
159+
sprintf('/^%s$/', $parameter->getName()),
160+
];
161+
$replace = [
162+
sprintf('%d.', $parameter->getPosition()),
163+
$parameter->getPosition(),
164+
];
141165
$argumentPaths = preg_replace($search, $replace, $argumentPaths);
142166
}
143167

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netlogix\Sentry\Tests\Unit\Scope\Extra;
6+
7+
use Neos\Flow\Tests\UnitTestCase;
8+
use Netlogix\Sentry\Scope\Extra\VariablesFromStackProvider;
9+
10+
use function iterator_to_array;
11+
12+
class VariablesFromStackProviderTest extends UnitTestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function Render_simple_values_as_single_line_information(): void
18+
{
19+
$result = iterator_to_array(
20+
VariablesFromStackProvider::buildReadableRepresentationOfTrace(
21+
'foo::bar()',
22+
[
23+
'baz' => '0'
24+
],
25+
[
26+
'args' => [
27+
'some value'
28+
]
29+
]
30+
)
31+
);
32+
33+
self::assertEquals([
34+
[
35+
'foo::bar()' => [
36+
'baz' => 'some value'
37+
]
38+
]
39+
], $result);
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function Render_complex_values_as_multi_line_information(): void
46+
{
47+
$result = iterator_to_array(
48+
VariablesFromStackProvider::buildReadableRepresentationOfTrace(
49+
'foo::bar()',
50+
[
51+
'baz.name' => '0.name',
52+
'baz.gender' => '0.gender',
53+
],
54+
[
55+
'args' => [
56+
[
57+
'name' => 'Stephan',
58+
'gender' => 'male'
59+
]
60+
]
61+
]
62+
)
63+
);
64+
65+
self::assertEquals([
66+
[
67+
'foo::bar()' => [
68+
'baz.name' => 'Stephan',
69+
'baz.gender' => 'male',
70+
]
71+
]
72+
], $result);
73+
}
74+
75+
/**
76+
* @test
77+
*/
78+
public function Render_special_message_when_frames_dont_contain_method_arguments(): void
79+
{
80+
$result = iterator_to_array(
81+
VariablesFromStackProvider::buildReadableRepresentationOfTrace(
82+
'foo::bar()',
83+
[
84+
'baz' => '0'
85+
],
86+
[]
87+
)
88+
);
89+
90+
self::assertEquals([
91+
[
92+
'foo::bar()' => [
93+
'💥' => 'Argument tracing is disabled. To enable, set `zend.exception_ignore_args=0` ⚙️'
94+
]
95+
]
96+
], $result);
97+
}
98+
}

0 commit comments

Comments
 (0)