Skip to content

Commit f917041

Browse files
committed
refactor work with reflection api
1 parent 56fed96 commit f917041

File tree

6 files changed

+112
-42
lines changed

6 files changed

+112
-42
lines changed

composer.lock

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

src/JsonSerializer.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use ServiceBus\MessageSerializer\Exceptions\SerializationFailed;
1616
use ServiceBus\MessageSerializer\Exceptions\UnserializeFailed;
17+
use function ServiceBus\Common\jsonDecode;
18+
use function ServiceBus\Common\jsonEncode;
1719

1820
/**
1921
*
@@ -27,10 +29,7 @@ public function serialize(array $payload): string
2729
{
2830
try
2931
{
30-
/** @var string $encoded */
31-
$encoded = \json_encode($payload, \JSON_UNESCAPED_UNICODE | \JSON_THROW_ON_ERROR);
32-
33-
return $encoded;
32+
return jsonEncode($payload);
3433
}
3534
catch (\Throwable $throwable)
3635
{
@@ -49,12 +48,7 @@ public function unserialize(string $content): array
4948
{
5049
try
5150
{
52-
/** @noinspection PhpUnnecessaryLocalVariableInspection */
53-
/** @noinspection OneTimeUseVariablesInspection */
54-
/** @psalm-var array<string, string|int|float|null> $decoded */
55-
$decoded = \json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
56-
57-
return $decoded;
51+
return jsonDecode($content);
5852
}
5953
catch (\Throwable $throwable)
6054
{

src/Symfony/Extensions/PropertyNormalizerWrapper.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,16 @@ protected function extractAttributes(object $object, string $format = null, arra
5151

5252
if (false === isset($this->localStorage[$class]))
5353
{
54-
$this->localStorage[$class] = [];
55-
56-
/**
57-
* @var string $key
58-
* @var mixed $value
59-
*/
60-
foreach (\get_object_vars($object) as $key => $value)
61-
{
62-
$this->localStorage[$class][] = $key;
63-
}
54+
$this->localStorage[$class] = parent::extractAttributes($object, $format, $context);
6455
}
6556

6657
return $this->localStorage[$class];
6758
}
6859

6960
/**
7061
* {@inheritdoc}
62+
*
63+
* @throws \Throwable
7164
*/
7265
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
7366
{
@@ -76,7 +69,19 @@ protected function getAttributeValue(object $object, string $attribute, string $
7669
return $object->{$attribute};
7770
}
7871

79-
return parent::getAttributeValue($object, $attribute, $format, $context);
72+
try
73+
{
74+
return parent::getAttributeValue($object, $attribute, $format, $context);
75+
}
76+
catch (\Throwable $throwable)
77+
{
78+
if (\strpos($throwable->getMessage(), 'before initialization') !== false)
79+
{
80+
return null;
81+
}
82+
83+
throw $throwable;
84+
}
8085
}
8186

8287
/**

src/Symfony/Extractor/FailOverExtractor.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,45 @@
2020
*/
2121
final class FailOverExtractor implements PropertyTypeExtractorInterface
2222
{
23+
/**
24+
* @psalm-var array<string, \Symfony\Component\PropertyInfo\Type[]|null>
25+
*/
26+
private array $localStorage;
27+
2328
/**
2429
* @var PropertyTypeExtractorInterface[]
2530
*/
2631
private array $extractors;
2732

2833
public function __construct()
2934
{
30-
$this->extractors = [
31-
new ReflectionExtractor(),
32-
new PhpDocExtractor(),
33-
];
35+
$this->extractors = [new PhpDocExtractor(), new ReflectionExtractor()];
3436
}
3537

3638
/**
3739
* {@inheritdoc}
3840
*/
3941
public function getTypes(string $class, string $property, array $context = []): ?array
4042
{
41-
foreach ($this->extractors as $extractor)
43+
$cacheKey = \sha1($class . $property);
44+
45+
if (isset($this->localStorage[$cacheKey]) === false)
4246
{
43-
$types = $extractor->getTypes($class, $property, $context);
47+
$this->localStorage[$cacheKey] = null;
4448

45-
if (null !== $types)
49+
foreach ($this->extractors as $extractor)
4650
{
47-
return $types;
51+
$types = $extractor->getTypes($class, $property, $context);
52+
53+
if (null !== $types)
54+
{
55+
$this->localStorage[$cacheKey] = $types;
56+
57+
break;
58+
}
4859
}
4960
}
5061

51-
return null;
62+
return $this->localStorage[$cacheKey];
5263
}
5364
}

tests/Stubs/WithPrivateProperties.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Messages serializer implementation.
5+
*
6+
* @author Maksim Masiukevich <[email protected]>
7+
* @license MIT
8+
* @license https://opensource.org/licenses/MIT
9+
*/
10+
11+
declare(strict_types = 1);
12+
13+
namespace ServiceBus\MessageSerializer\Tests\Stubs;
14+
15+
/**
16+
*/
17+
final class WithPrivateProperties
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $qwerty;
23+
24+
private int $root;
25+
private ?\DateTimeImmutable $dateTime;
26+
27+
/**
28+
* @var \DateTimeImmutable
29+
*/
30+
private $createdAt;
31+
32+
public function __construct(string $qwerty, int $root, \DateTimeImmutable $createdAt)
33+
{
34+
$this->qwerty = $qwerty;
35+
$this->root = $root;
36+
$this->createdAt = $createdAt;
37+
}
38+
}

tests/Symfony/SymfonyMessageSerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use ServiceBus\MessageSerializer\Tests\Stubs\TestMessage;
2626
use ServiceBus\MessageSerializer\Tests\Stubs\WithDateTimeField;
2727
use ServiceBus\MessageSerializer\Tests\Stubs\WithNullableObjectArgument;
28+
use ServiceBus\MessageSerializer\Tests\Stubs\WithPrivateProperties;
29+
use function ServiceBus\Common\now;
2830

2931
/**
3032
*
@@ -249,4 +251,23 @@ public function legacyPropertiesSupport(): void
249251
static::assertSame($object->dateTime->getTimestamp(), $unserialized->dateTime->getTimestamp());
250252
static::assertSame($object->long, $unserialized->long);
251253
}
254+
255+
/**
256+
* @test
257+
*
258+
* @throws \Throwable
259+
*/
260+
public function privateMixedPropertiesSupport(): void
261+
{
262+
$serializer = new SymfonyMessageSerializer();
263+
264+
$object = new WithPrivateProperties(
265+
'Some string',
266+
100500,
267+
now()
268+
);
269+
270+
/** @var WithPrivateProperties $unserialized */
271+
$unserialized = $serializer->decode($serializer->encode($object));
272+
}
252273
}

0 commit comments

Comments
 (0)