Skip to content

[ErrorHandler] Serialize FlattenException #38800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/Symfony/Component/ErrorHandler/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,22 @@ public function setTraceFromThrowable(\Throwable $throwable): self
/**
* @return $this
*/
public function setTrace($trace, $file, $line): self
public function setTrace($trace, $file = null, $line = null): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a BC break for inheriting classes? Any other idea?

{
$this->trace = [];
$this->trace[] = [
'namespace' => '',
'short_class' => '',
'class' => '',
'type' => '',
'function' => '',
'file' => $file,
'line' => $line,
'args' => [],
];
if (null !== $file) {
$this->trace[] = [
'namespace' => '',
'short_class' => '',
'class' => '',
'type' => '',
'function' => '',
'file' => $file,
'line' => $line,
'args' => [],
];
}

foreach ($trace as $entry) {
$class = '';
$namespace = '';
Expand Down Expand Up @@ -357,6 +360,18 @@ private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): str

public function getTraceAsString(): string
{
if (null === $this->traceAsString) {
$this->traceAsString = '';
if (null !== $this->trace) {
foreach ($this->trace as $i => $trace) {
if (0 === $i) {
continue;
}
$this->traceAsString .= sprintf('#%d %s(%s): %s%s%s()', $i - 1, $trace['file'], $trace['line'], $trace['class'], $trace['type'], $trace['function']).\PHP_EOL;
}
}
}

return $this->traceAsString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class FlattenExceptionTest extends TestCase
{
Expand Down Expand Up @@ -405,6 +409,22 @@ public function testToStringParent()
$this->assertSame($exception->__toString(), $flattened->getAsString());
}

public function testSerialize()
{
$serializer = new Serializer([new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]);

$flattened = FlattenException::createFromThrowable(new \LogicException('Bad things happened'));
$trace = $flattened->getTraceAsString();
$data = $serializer->serialize($flattened, 'json');

$restored = $serializer->deserialize($data, FlattenException::class, 'json');
$restoredTrace = $restored->getTraceAsString();

// Verify that they look kind of similar.
$this->assertEquals(substr($trace, 0, 100), substr($restoredTrace, 0, 100));
$this->assertEquals(\count(explode(\PHP_EOL, $trace)), \count(explode(\PHP_EOL, $restoredTrace)));
}

private function createException($foo): \Exception
{
return new \Exception();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ private function getWriteInfo(string $class, string $property, $value): Property
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
'enable_adder_remover_extraction' => $useAdderAndRemover,
'value' => $value,
]);

if (isset($item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,12 @@ public function getWriteInfo(string $class, string $property, array $context = [
}

$method = $reflClass->getMethod($methodName);
/** @var \ReflectionParameter $parameter */
$parameter = $method->getParameters()[0];
if (\array_key_exists('value', $context) && null === $context['value'] && !$parameter->allowsNull()) {
$errors[] = sprintf('The method "%s" in class "%s" was found but does not allow null.', $methodName, $class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks PropertyAccess test suite (see Travis's failure)

continue;
}

if (!\in_array($mutatorPrefix, $this->arrayMutatorPrefixes, true)) {
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD, $methodName, $this->getWriteVisiblityForMethod($method), $method->isStatic());
Expand Down