Skip to content

Commit 1f95ffd

Browse files
Remove deadcode after the bump to PHP >= 8.4
1 parent e0ea8eb commit 1f95ffd

File tree

6 files changed

+17
-128
lines changed

6 files changed

+17
-128
lines changed

DependencyInjection/StreamablePass.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,5 @@ public function process(ContainerBuilder $container): void
4747

4848
$container->getDefinition('.json_streamer.cache_warmer.streamer')
4949
->replaceArgument(0, $streamable);
50-
51-
if ($container->hasDefinition('.json_streamer.cache_warmer.lazy_ghost')) {
52-
$container->getDefinition('.json_streamer.cache_warmer.lazy_ghost')
53-
->replaceArgument(0, array_keys($streamable));
54-
}
5550
}
5651
}

JsonStreamReader.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ public function __construct(
4545
private ContainerInterface $valueTransformers,
4646
PropertyMetadataLoaderInterface $propertyMetadataLoader,
4747
string $streamReadersDir,
48-
?string $lazyGhostsDir = null,
4948
) {
5049
$this->streamReaderGenerator = new StreamReaderGenerator($propertyMetadataLoader, $streamReadersDir);
5150
$this->instantiator = new Instantiator();
52-
$this->lazyInstantiator = new LazyInstantiator($lazyGhostsDir);
51+
$this->lazyInstantiator = new LazyInstantiator();
5352
}
5453

5554
public function read($input, Type $type, array $options = []): mixed
@@ -63,10 +62,9 @@ public function read($input, Type $type, array $options = []): mixed
6362
/**
6463
* @param array<string, ValueTransformerInterface> $valueTransformers
6564
*/
66-
public static function create(array $valueTransformers = [], ?string $streamReadersDir = null, ?string $lazyGhostsDir = null): self
65+
public static function create(array $valueTransformers = [], ?string $streamReadersDir = null): self
6766
{
6867
$streamReadersDir ??= sys_get_temp_dir().'/json_streamer/read';
69-
$lazyGhostsDir ??= sys_get_temp_dir().'/json_streamer/lazy_ghost';
7068
$valueTransformers += [
7169
'json_streamer.value_transformer.string_to_date_time' => new StringToDateTimeValueTransformer(),
7270
];
@@ -101,6 +99,6 @@ public function get(string $id): ValueTransformerInterface
10199
$typeContextFactory,
102100
);
103101

104-
return new self($valueTransformersContainer, $propertyMetadataLoader, $streamReadersDir, $lazyGhostsDir);
102+
return new self($valueTransformersContainer, $propertyMetadataLoader, $streamReadersDir);
105103
}
106104
}

Read/LazyInstantiator.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111

1212
namespace Symfony\Component\JsonStreamer\Read;
1313

14-
use Symfony\Component\Filesystem\Filesystem;
15-
use Symfony\Component\JsonStreamer\Exception\InvalidArgumentException;
1614
use Symfony\Component\JsonStreamer\Exception\RuntimeException;
17-
use Symfony\Component\VarExporter\ProxyHelper;
1815

1916
/**
20-
* Instantiates a new $className lazy ghost {@see \Symfony\Component\VarExporter\LazyGhostTrait}.
17+
* Instantiates a new $className lazy ghost.
2118
*
22-
* Prior to PHP 8.4, the "$className" argument class must not be final.
2319
* The $initializer must be a callable that sets the actual object values when being called.
2420
*
2521
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
@@ -28,8 +24,6 @@
2824
*/
2925
final class LazyInstantiator
3026
{
31-
private ?Filesystem $fs = null;
32-
3327
/**
3428
* @var array{reflection: array<class-string, \ReflectionClass<object>>, lazy_class_name: array<class-string, class-string>}
3529
*/
@@ -38,19 +32,6 @@ final class LazyInstantiator
3832
'lazy_class_name' => [],
3933
];
4034

41-
/**
42-
* @var array<class-string, true>
43-
*/
44-
private static array $lazyClassesLoaded = [];
45-
46-
public function __construct(
47-
private ?string $lazyGhostsDir = null,
48-
) {
49-
if (null === $this->lazyGhostsDir && \PHP_VERSION_ID < 80400) {
50-
throw new InvalidArgumentException('The "$lazyGhostsDir" argument cannot be null when using PHP < 8.4.');
51-
}
52-
}
53-
5435
/**
5536
* @template T of object
5637
*
@@ -68,30 +49,6 @@ public function instantiate(string $className, callable $initializer): object
6849
}
6950

7051
// use native lazy ghosts if available
71-
if (\PHP_VERSION_ID >= 80400) {
72-
return $classReflection->newLazyGhost($initializer);
73-
}
74-
75-
$this->fs ??= new Filesystem();
76-
77-
$lazyClassName = self::$cache['lazy_class_name'][$className] ??= \sprintf('%sGhost', preg_replace('/\\\\/', '', $className));
78-
79-
if (isset(self::$lazyClassesLoaded[$className]) && class_exists($lazyClassName)) {
80-
return $lazyClassName::createLazyGhost($initializer);
81-
}
82-
83-
if (!is_file($path = \sprintf('%s%s%s.php', $this->lazyGhostsDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className)))) {
84-
if (!$this->fs->exists($this->lazyGhostsDir)) {
85-
$this->fs->mkdir($this->lazyGhostsDir);
86-
}
87-
88-
$this->fs->dumpFile($path, \sprintf('<?php class %s%s', $lazyClassName, ProxyHelper::generateLazyGhost($classReflection)));
89-
}
90-
91-
require_once $path;
92-
93-
self::$lazyClassesLoaded[$className] = true;
94-
95-
return $lazyClassName::createLazyGhost($initializer);
52+
return $classReflection->newLazyGhost($initializer);
9653
}
9754
}

Tests/DependencyInjection/StreamablePassTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function testSetStreamable()
2323

2424
$container->register('json_streamer.stream_writer');
2525
$container->register('.json_streamer.cache_warmer.streamer')->setArguments([null]);
26-
$container->register('.json_streamer.cache_warmer.lazy_ghost')->setArguments([null]);
2726

2827
$container->register('streamable')->setClass('Foo')->addTag('json_streamer.streamable', ['object' => true, 'list' => true]);
2928
$container->register('abstractStreamable')->setClass('Bar')->addTag('json_streamer.streamable', ['object' => true, 'list' => true])->setAbstract(true);
@@ -33,9 +32,7 @@ public function testSetStreamable()
3332
$pass->process($container);
3433

3534
$streamerCacheWarmer = $container->getDefinition('.json_streamer.cache_warmer.streamer');
36-
$lazyGhostCacheWarmer = $container->getDefinition('.json_streamer.cache_warmer.lazy_ghost');
3735

3836
$this->assertSame(['Foo' => ['object' => true, 'list' => true]], $streamerCacheWarmer->getArgument(0));
39-
$this->assertSame(['Foo'], $lazyGhostCacheWarmer->getArgument(0));
4037
}
4138
}

Tests/JsonStreamReaderTest.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,22 @@
2929
class JsonStreamReaderTest extends TestCase
3030
{
3131
private string $streamReadersDir;
32-
private string $lazyGhostsDir;
3332

3433
protected function setUp(): void
3534
{
3635
parent::setUp();
3736

3837
$this->streamReadersDir = \sprintf('%s/symfony_json_streamer_test/stream_reader', sys_get_temp_dir());
39-
$this->lazyGhostsDir = \sprintf('%s/symfony_json_streamer_test/lazy_ghost', sys_get_temp_dir());
4038

4139
if (is_dir($this->streamReadersDir)) {
4240
array_map('unlink', glob($this->streamReadersDir.'/*'));
4341
rmdir($this->streamReadersDir);
4442
}
45-
46-
if (is_dir($this->lazyGhostsDir)) {
47-
array_map('unlink', glob($this->lazyGhostsDir.'/*'));
48-
rmdir($this->lazyGhostsDir);
49-
}
5043
}
5144

5245
public function testReadScalar()
5346
{
54-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
47+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
5548

5649
$this->assertRead($reader, null, 'null', Type::nullable(Type::int()));
5750
$this->assertRead($reader, true, 'true', Type::bool());
@@ -63,7 +56,7 @@ public function testReadScalar()
6356

6457
public function testReadCollection()
6558
{
66-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
59+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
6760

6861
$this->assertRead(
6962
$reader,
@@ -92,7 +85,7 @@ public function testReadCollection()
9285

9386
public function testReadObject()
9487
{
95-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
88+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
9689

9790
$this->assertRead($reader, function (mixed $read) {
9891
$this->assertInstanceOf(ClassicDummy::class, $read);
@@ -103,7 +96,7 @@ public function testReadObject()
10396

10497
public function testReadObjectWithGenerics()
10598
{
106-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
99+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
107100

108101
$this->assertRead($reader, function (mixed $read) {
109102
$this->assertInstanceOf(DummyWithGenerics::class, $read);
@@ -114,7 +107,7 @@ public function testReadObjectWithGenerics()
114107

115108
public function testReadObjectWithStreamedName()
116109
{
117-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
110+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
118111

119112
$this->assertRead($reader, function (mixed $read) {
120113
$this->assertInstanceOf(DummyWithNameAttributes::class, $read);
@@ -125,12 +118,11 @@ public function testReadObjectWithStreamedName()
125118
public function testReadObjectWithValueTransformer()
126119
{
127120
$reader = JsonStreamReader::create(
128-
valueTransformers: [
121+
[
129122
StringToBooleanValueTransformer::class => new StringToBooleanValueTransformer(),
130123
DivideStringAndCastToIntValueTransformer::class => new DivideStringAndCastToIntValueTransformer(),
131124
],
132-
streamReadersDir: $this->streamReadersDir,
133-
lazyGhostsDir: $this->lazyGhostsDir,
125+
$this->streamReadersDir,
134126
);
135127

136128
$this->assertRead($reader, function (mixed $read) {
@@ -144,7 +136,7 @@ public function testReadObjectWithValueTransformer()
144136

145137
public function testReadObjectWithPhpDoc()
146138
{
147-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
139+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
148140

149141
$this->assertRead($reader, function (mixed $read) {
150142
$this->assertInstanceOf(DummyWithPhpDoc::class, $read);
@@ -156,7 +148,7 @@ public function testReadObjectWithPhpDoc()
156148

157149
public function testReadObjectWithNullableProperties()
158150
{
159-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
151+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
160152

161153
$this->assertRead($reader, function (mixed $read) {
162154
$this->assertInstanceOf(DummyWithNullableProperties::class, $read);
@@ -167,7 +159,7 @@ public function testReadObjectWithNullableProperties()
167159

168160
public function testReadObjectWithDateTimes()
169161
{
170-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
162+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
171163

172164
$this->assertRead($reader, function (mixed $read) {
173165
$this->assertInstanceOf(DummyWithDateTimes::class, $read);
@@ -178,7 +170,7 @@ public function testReadObjectWithDateTimes()
178170

179171
public function testCreateStreamReaderFile()
180172
{
181-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
173+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
182174

183175
$reader->read('true', Type::bool());
184176

@@ -188,7 +180,7 @@ public function testCreateStreamReaderFile()
188180

189181
public function testCreateStreamReaderFileOnlyIfNotExists()
190182
{
191-
$reader = JsonStreamReader::create(streamReadersDir: $this->streamReadersDir, lazyGhostsDir: $this->lazyGhostsDir);
183+
$reader = JsonStreamReader::create([], $this->streamReadersDir);
192184

193185
if (!file_exists($this->streamReadersDir)) {
194186
mkdir($this->streamReadersDir, recursive: true);

Tests/Read/LazyInstantiatorTest.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,11 @@
1212
namespace Symfony\Component\JsonStreamer\Tests\Read;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\JsonStreamer\Exception\InvalidArgumentException;
1615
use Symfony\Component\JsonStreamer\Read\LazyInstantiator;
1716
use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy;
1817

1918
class LazyInstantiatorTest extends TestCase
2019
{
21-
private string $lazyGhostsDir;
22-
23-
protected function setUp(): void
24-
{
25-
parent::setUp();
26-
27-
$this->lazyGhostsDir = \sprintf('%s/symfony_json_streamer_test/lazy_ghost', sys_get_temp_dir());
28-
29-
if (is_dir($this->lazyGhostsDir)) {
30-
array_map('unlink', glob($this->lazyGhostsDir.'/*'));
31-
rmdir($this->lazyGhostsDir);
32-
}
33-
}
34-
35-
/**
36-
* @requires PHP < 8.4
37-
*/
38-
public function testCreateLazyGhostUsingVarExporter()
39-
{
40-
$ghost = (new LazyInstantiator($this->lazyGhostsDir))->instantiate(ClassicDummy::class, function (ClassicDummy $object): void {
41-
$object->id = 123;
42-
});
43-
44-
$this->assertSame(123, $ghost->id);
45-
}
46-
47-
/**
48-
* @requires PHP < 8.4
49-
*/
50-
public function testCreateCacheFile()
51-
{
52-
// use DummyForLazyInstantiation class to be sure that the instantiated object is not already in cache.
53-
(new LazyInstantiator($this->lazyGhostsDir))->instantiate(DummyForLazyInstantiation::class, function (DummyForLazyInstantiation $object): void {});
54-
55-
$this->assertCount(1, glob($this->lazyGhostsDir.'/*'));
56-
}
57-
58-
/**
59-
* @requires PHP < 8.4
60-
*/
61-
public function testThrowIfLazyGhostDirNotDefined()
62-
{
63-
$this->expectException(InvalidArgumentException::class);
64-
new LazyInstantiator();
65-
}
66-
67-
/**
68-
* @requires PHP 8.4
69-
*/
7020
public function testCreateLazyGhostUsingPhp()
7121
{
7222
$ghost = (new LazyInstantiator())->instantiate(ClassicDummy::class, function (ClassicDummy $object): void {

0 commit comments

Comments
 (0)