Skip to content

Commit ff1c8c5

Browse files
authored
bug #1525 [make:serializer:encoder] fix interface signature mismatch in template (#1525)
1 parent ed3465b commit ff1c8c5

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/Maker/MakeSerializerEncoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Command\Command;
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\HttpKernel\Kernel;
2223
use Symfony\Component\Serializer\Encoder\DecoderInterface;
2324
use Symfony\Component\Serializer\Encoder\EncoderInterface;
2425
use Symfony\Component\Serializer\Serializer;
@@ -61,12 +62,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6162
EncoderInterface::class,
6263
]);
6364

65+
/* @legacy - Remove "decoder_return_type" when Symfony 6.4 is no longer supported */
6466
$generator->generateClass(
6567
$encoderClassNameDetails->getFullName(),
6668
'serializer/Encoder.tpl.php',
6769
[
6870
'use_statements' => $useStatements,
6971
'format' => $format,
72+
'use_decoder_return_type' => Kernel::VERSION_ID >= 70000,
7073
]
7174
);
7275

src/Resources/skeleton/serializer/Encoder.tpl.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ class <?= $class_name ?> implements EncoderInterface, DecoderInterface
88
{
99
public const FORMAT = '<?= $format ?>';
1010

11-
public function encode($data, string $format, array $context = []): string
11+
public function encode(mixed $data, string $format, array $context = []): string
1212
{
1313
// TODO: return your encoded data
1414
return '';
1515
}
1616

17-
public function supportsEncoding(string $format, array $context = []): bool
17+
public function supportsEncoding(string $format): bool
1818
{
1919
return self::FORMAT === $format;
2020
}
2121

22-
public function decode(string $data, string $format, array $context = [])
22+
public function decode(string $data, string $format, array $context = [])<?php if ($use_decoder_return_type): ?>: mixed<?php endif; ?>
2323
{
2424
// TODO: return your decoded data
2525
return '';
2626
}
2727

28-
public function supportsDecoding(string $format, array $context = []): bool
28+
public function supportsDecoding(string $format): bool
2929
{
3030
return self::FORMAT === $format;
3131
}

tests/Maker/MakeSerializerEncoderTest.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ protected function getMakerClass(): string
2525
public function getTestDetails(): \Generator
2626
{
2727
yield 'it_makes_serializer_encoder' => [$this->createMakerTest()
28-
// serializer-pack 1.1 requires symfony/property-info >= 5.4
29-
// adding symfony/serializer-pack:* as an extra depends allows
30-
// us to use serializer-pack < 1.1 which does not conflict with
31-
// property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063
32-
->addExtraDependencies('symfony/serializer-pack:*')
3328
->run(function (MakerTestRunner $runner) {
29+
if (70000 >= $runner->getSymfonyVersion()) {
30+
$this->markTestSkipped('Legacy Symfony 6.4 Test');
31+
}
3432
$runner->runMaker(
3533
[
3634
// encoder class name
@@ -39,6 +37,33 @@ public function getTestDetails(): \Generator
3937
'foobar',
4038
]
4139
);
40+
41+
self::assertStringContainsString(
42+
needle: 'public function decode(string $data, string $format, array $context = []): mixed',
43+
haystack: file_get_contents($runner->getPath('src/Serializer/FooBarEncoder.php'))
44+
);
45+
}),
46+
];
47+
48+
/* @legacy - Remove when MakerBundle no longer supports Symfony 6.4 */
49+
yield 'it_makes_serializer_encoder_legacy' => [$this->createMakerTest()
50+
->run(function (MakerTestRunner $runner) {
51+
if (70000 < $runner->getSymfonyVersion()) {
52+
$this->markTestSkipped('Legacy Symfony 6.4 Test');
53+
}
54+
$runner->runMaker(
55+
[
56+
// encoder class name
57+
'FooBarEncoder',
58+
// encoder format
59+
'foobar',
60+
]
61+
);
62+
63+
self::assertStringNotContainsString(
64+
needle: 'public function decode(string $data, string $format, array $context = []): mixed',
65+
haystack: file_get_contents($runner->getPath('src/Serializer/FooBarEncoder.php'))
66+
);
4267
}),
4368
];
4469
}

0 commit comments

Comments
 (0)