Skip to content

Commit 0528648

Browse files
authored
Remove PHP/Symfony versions matrix hacks (#62)
1 parent cafca27 commit 0528648

25 files changed

+172
-358
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ class Member
8989
}
9090
```
9191

92-
> **note** both PHP Attributes & Annotations are supported :
93-
> ```php
94-
> /**
95-
> * @Enum(StatusEnum::class)
96-
> */
97-
> public ?string $status = null;
98-
> ```
99-
10092
### Setting up the form
10193

10294
Now that validation is configured, the only thing we have to do is to add a field on our form :

src/Enum.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
*/
1313
class Enum implements EnumInterface
1414
{
15-
/**
16-
* @var string
17-
*/
18-
private $name;
15+
private string $name;
1916

2017
/**
2118
* @var array<string, mixed>|null
2219
*/
23-
private $choices;
20+
private array|null $choices;
2421

2522
/**
2623
* @param array<string, mixed>|null $choices Allowed to be null if you are extending this class
@@ -41,8 +38,10 @@ public function __construct(?array $choices, ?string $name = null)
4138
if ($name === null) {
4239
$name = static::class;
4340
if (
44-
\strpos($name, 'Yokai\\EnumBundle\\') === 0 // using FQCN as name is only allowed for other namespaces
45-
&& \strpos($name, 'Yokai\\EnumBundle\\Tests\\') !== 0 // except for our tests
41+
// using FQCN as name is only allowed for other namespaces
42+
\str_starts_with($name, 'Yokai\\EnumBundle\\')
43+
// except for our tests
44+
&& !\str_starts_with($name, 'Yokai\\EnumBundle\\Tests\\')
4645
) {
4746
throw new LogicException(
4847
'When using ' . static::class . ', $name argument in ' . __METHOD__ . ' method cannot be null'
@@ -67,7 +66,7 @@ public function getValues(): array
6766
return \array_values($this->choices);
6867
}
6968

70-
public function getLabel($value): string
69+
public function getLabel(mixed $value): string
7170
{
7271
$this->init();
7372

src/EnumInterface.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public function getValues(): array;
2525

2626
/**
2727
* Returns enum value label.
28-
*
29-
* @param mixed $value
3028
*/
31-
public function getLabel($value): string;
29+
public function getLabel(mixed $value): string;
3230

3331
/**
3432
* Returns enum identifier (must be unique across app).

src/EnumRegistry.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class EnumRegistry
1515
/**
1616
* @var EnumInterface[]
1717
*/
18-
private $enums = [];
18+
private array $enums = [];
1919

2020
/**
2121
* @throws LogicException

src/Form/Type/EnumType.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
*/
1717
final class EnumType extends AbstractType
1818
{
19-
/**
20-
* @var EnumRegistry
21-
*/
22-
private $enumRegistry;
19+
private EnumRegistry $enumRegistry;
2320

2421
public function __construct(EnumRegistry $enumRegistry)
2522
{

src/TranslatedEnum.php

+5-14
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,13 @@ class TranslatedEnum extends Enum
1515
/**
1616
* @var array<int|string, mixed>
1717
*/
18-
private $values;
18+
private array $values;
1919

20-
/**
21-
* @var TranslatorInterface
22-
*/
23-
private $translator;
20+
private TranslatorInterface $translator;
2421

25-
/**
26-
* @var string
27-
*/
28-
private $transPattern;
22+
private string $transPattern;
2923

30-
/**
31-
* @var string
32-
*/
33-
private $transDomain;
24+
private string $transDomain;
3425

3526
/**
3627
* @param array<int|string, mixed> $values
@@ -44,7 +35,7 @@ public function __construct(
4435
string $transDomain = 'messages',
4536
?string $name = null
4637
) {
47-
if (\strpos($transPattern, '%s') === false) {
38+
if (!\str_contains($transPattern, '%s')) {
4839
throw LogicException::placeholderRequired($transPattern);
4940
}
5041

src/Twig/Extension/EnumExtension.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
*/
1515
final class EnumExtension extends AbstractExtension
1616
{
17-
/**
18-
* @var EnumRegistry
19-
*/
20-
private $registry;
17+
private EnumRegistry $registry;
2118

2219
public function __construct(EnumRegistry $registry)
2320
{

src/Validator/Constraints/Enum.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77
use Symfony\Component\Validator\Constraints\Choice;
88

99
/**
10-
* @Annotation
11-
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
12-
*
1310
* @author Yann Eugoné <[email protected]>
1411
*/
1512
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
1613
final class Enum extends Choice
1714
{
18-
/**
19-
* @var string
20-
*/
21-
public $enum;
15+
public string $enum;
2216

2317
/**
2418
* @param array<string, mixed> $options

src/Validator/Constraints/EnumValidator.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
final class EnumValidator extends ChoiceValidator
1717
{
18-
/**
19-
* @var EnumRegistry
20-
*/
21-
private $enumRegistry;
18+
private EnumRegistry $enumRegistry;
2219

2320
public function __construct(EnumRegistry $enumRegistry)
2421
{
@@ -34,7 +31,7 @@ public function validate(mixed $value, Constraint $constraint): void
3431
$constraint->choices = null;
3532
$constraint->callback = null;
3633

37-
if (!$constraint->enum) {
34+
if (!isset($constraint->enum)) {
3835
throw new ConstraintDefinitionException('"enum" must be specified on constraint Enum');
3936
}
4037

tests/Integration/config/packages/annotations.yaml

-6
This file was deleted.

tests/Integration/config/services-8.1.yaml

-8
This file was deleted.

tests/Integration/config/services-8.0.yaml tests/Integration/config/services.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ services:
55

66
Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestLabelEnum: ~
77
Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestMyCLabsStatusEnum: ~
8+
Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestNativeStatusEnum: ~
89
Yokai\EnumBundle\Tests\Integration\App\Form\PullRequestType: ~

tests/Integration/src/Kernel.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ public function registerBundles(): iterable
1818
{
1919
yield new \Symfony\Bundle\FrameworkBundle\FrameworkBundle();
2020
yield new \Yokai\EnumBundle\YokaiEnumBundle();
21-
if (\PHP_VERSION_ID < 80000) {
22-
yield new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle();
23-
}
2421
}
2522

2623
public function getProjectDir(): string
@@ -32,22 +29,14 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
3229
{
3330
$loader->load(__DIR__ . '/../config/packages/framework.yaml');
3431
$loader->load(__DIR__ . '/../config/packages/translation.yaml');
35-
if (\PHP_VERSION_ID < 80000) {
36-
$loader->load(__DIR__ . '/../config/packages/annotations.yaml');
37-
}
38-
39-
if (\PHP_VERSION_ID < 80100) {
40-
$loader->load(__DIR__ . '/../config/services-8.0.yaml');
41-
} else {
42-
$loader->load(__DIR__ . '/../config/services-8.1.yaml');
43-
}
32+
$loader->load(__DIR__ . '/../config/services.yaml');
4433
}
4534

4635
protected function build(ContainerBuilder $container): void
4736
{
4837
$container->addCompilerPass(
4938
new class() implements CompilerPassInterface {
50-
public function process(ContainerBuilder $container)
39+
public function process(ContainerBuilder $container): void
5140
{
5241
$container->findDefinition('form.factory')->setPublic(true);
5342
}

tests/Integration/src/Model/PullRequestPhp80.php tests/Integration/src/Model/MyCLabsPullRequest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
/**
1212
* @author Yann Eugoné <[email protected]>
1313
*/
14-
final class PullRequestPhp80
14+
final class MyCLabsPullRequest
1515
{
16-
/**
17-
* @var MyCLabsStatus
18-
*/
1916
#[Enum(enum: PullRequestMyCLabsStatusEnum::class)]
20-
public $status;
17+
public MyCLabsStatus $status;
2118

2219
/**
2320
* @var string[]
2421
*/
2522
#[Enum(enum: PullRequestLabelEnum::class, multiple: true)]
26-
public $labels;
23+
public array $labels;
2724
}

tests/Integration/src/Model/PullRequestPhp81.php tests/Integration/src/Model/NativeEnumPullRequest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
/**
1212
* @author Yann Eugoné <[email protected]>
1313
*/
14-
final class PullRequestPhp81
14+
final class NativeEnumPullRequest
1515
{
16-
/**
17-
* @var NativeStatus
18-
*/
1916
#[Enum(enum: PullRequestNativeStatusEnum::class)]
20-
public $status;
17+
public NativeStatus $status;
2118

2219
/**
2320
* @var string[]
2421
*/
2522
#[Enum(enum: PullRequestLabelEnum::class, multiple: true)]
26-
public $labels;
23+
public array $labels;
2724
}

tests/Integration/src/Model/PullRequestPhp7.php

-29
This file was deleted.

0 commit comments

Comments
 (0)