Skip to content
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

Remove PHP/Symfony versions matrix hacks #62

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ class Member
}
```

> **note** both PHP Attributes & Annotations are supported :
> ```php
> /**
> * @Enum(StatusEnum::class)
> */
> public ?string $status = null;
> ```

### Setting up the form

Now that validation is configured, the only thing we have to do is to add a field on our form :
Expand Down
15 changes: 7 additions & 8 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
*/
class Enum implements EnumInterface
{
/**
* @var string
*/
private $name;
private string $name;

/**
* @var array<string, mixed>|null
*/
private $choices;
private array|null $choices;

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

public function getLabel($value): string
public function getLabel(mixed $value): string
{
$this->init();

Expand Down
4 changes: 1 addition & 3 deletions src/EnumInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public function getValues(): array;

/**
* Returns enum value label.
*
* @param mixed $value
*/
public function getLabel($value): string;
public function getLabel(mixed $value): string;

/**
* Returns enum identifier (must be unique across app).
Expand Down
2 changes: 1 addition & 1 deletion src/EnumRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class EnumRegistry
/**
* @var EnumInterface[]
*/
private $enums = [];
private array $enums = [];

/**
* @throws LogicException
Expand Down
5 changes: 1 addition & 4 deletions src/Form/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
final class EnumType extends AbstractType
{
/**
* @var EnumRegistry
*/
private $enumRegistry;
private EnumRegistry $enumRegistry;

public function __construct(EnumRegistry $enumRegistry)
{
Expand Down
19 changes: 5 additions & 14 deletions src/TranslatedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ class TranslatedEnum extends Enum
/**
* @var array<int|string, mixed>
*/
private $values;
private array $values;

/**
* @var TranslatorInterface
*/
private $translator;
private TranslatorInterface $translator;

/**
* @var string
*/
private $transPattern;
private string $transPattern;

/**
* @var string
*/
private $transDomain;
private string $transDomain;

/**
* @param array<int|string, mixed> $values
Expand All @@ -44,7 +35,7 @@ public function __construct(
string $transDomain = 'messages',
?string $name = null
) {
if (\strpos($transPattern, '%s') === false) {
if (!\str_contains($transPattern, '%s')) {
throw LogicException::placeholderRequired($transPattern);
}

Expand Down
5 changes: 1 addition & 4 deletions src/Twig/Extension/EnumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
*/
final class EnumExtension extends AbstractExtension
{
/**
* @var EnumRegistry
*/
private $registry;
private EnumRegistry $registry;

public function __construct(EnumRegistry $registry)
{
Expand Down
8 changes: 1 addition & 7 deletions src/Validator/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
use Symfony\Component\Validator\Constraints\Choice;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Yann Eugoné <[email protected]>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
final class Enum extends Choice
{
/**
* @var string
*/
public $enum;
public string $enum;

/**
* @param array<string, mixed> $options
Expand Down
7 changes: 2 additions & 5 deletions src/Validator/Constraints/EnumValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
final class EnumValidator extends ChoiceValidator
{
/**
* @var EnumRegistry
*/
private $enumRegistry;
private EnumRegistry $enumRegistry;

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

if (!$constraint->enum) {
if (!isset($constraint->enum)) {
throw new ConstraintDefinitionException('"enum" must be specified on constraint Enum');
}

Expand Down
6 changes: 0 additions & 6 deletions tests/Integration/config/packages/annotations.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions tests/Integration/config/services-8.1.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ services:

Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestLabelEnum: ~
Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestMyCLabsStatusEnum: ~
Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestNativeStatusEnum: ~
Yokai\EnumBundle\Tests\Integration\App\Form\PullRequestType: ~
15 changes: 2 additions & 13 deletions tests/Integration/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public function registerBundles(): iterable
{
yield new \Symfony\Bundle\FrameworkBundle\FrameworkBundle();
yield new \Yokai\EnumBundle\YokaiEnumBundle();
if (\PHP_VERSION_ID < 80000) {
yield new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle();
}
}

public function getProjectDir(): string
Expand All @@ -32,22 +29,14 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../config/packages/framework.yaml');
$loader->load(__DIR__ . '/../config/packages/translation.yaml');
if (\PHP_VERSION_ID < 80000) {
$loader->load(__DIR__ . '/../config/packages/annotations.yaml');
}

if (\PHP_VERSION_ID < 80100) {
$loader->load(__DIR__ . '/../config/services-8.0.yaml');
} else {
$loader->load(__DIR__ . '/../config/services-8.1.yaml');
}
$loader->load(__DIR__ . '/../config/services.yaml');
}

protected function build(ContainerBuilder $container): void
{
$container->addCompilerPass(
new class() implements CompilerPassInterface {
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$container->findDefinition('form.factory')->setPublic(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
/**
* @author Yann Eugoné <[email protected]>
*/
final class PullRequestPhp80
final class MyCLabsPullRequest
{
/**
* @var MyCLabsStatus
*/
#[Enum(enum: PullRequestMyCLabsStatusEnum::class)]
public $status;
public MyCLabsStatus $status;

/**
* @var string[]
*/
#[Enum(enum: PullRequestLabelEnum::class, multiple: true)]
public $labels;
public array $labels;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
/**
* @author Yann Eugoné <[email protected]>
*/
final class PullRequestPhp81
final class NativeEnumPullRequest
{
/**
* @var NativeStatus
*/
#[Enum(enum: PullRequestNativeStatusEnum::class)]
public $status;
public NativeStatus $status;

/**
* @var string[]
*/
#[Enum(enum: PullRequestLabelEnum::class, multiple: true)]
public $labels;
public array $labels;
}
29 changes: 0 additions & 29 deletions tests/Integration/src/Model/PullRequestPhp7.php

This file was deleted.

Loading
Loading