diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index f68450a..918d16e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - php-versions: [ '7.4', '8.0', '8.1' ] + php-versions: [ '8.2', '8.3' ] steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 6c601b6..173d2bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /vendor/ /composer.phar /composer.lock -.phpunit.result.cache +/.phpunit.cache/ phpcs.cache diff --git a/Makefile b/Makefile deleted file mode 100644 index 0e8e4ab..0000000 --- a/Makefile +++ /dev/null @@ -1,45 +0,0 @@ -ARGS = $(filter-out $@,$(MAKECMDGOALS)) -MAKEFLAGS += --silent - -list: - sh -c "echo; $(MAKE) -p no_targets__ | awk -F':' '/^[a-zA-Z0-9][^\$$#\/\\t=]*:([^=]|$$)/ {split(\$$1,A,/ /);for(i in A)print A[i]}' | grep -v '__\$$' | grep -v 'Makefile'| sort" - - -############################# -# Docker machine states -############################# - -up: - docker-compose up -d - -start: - docker-compose start - -build: - docker-compose build - -stop: - docker-compose stop - -state: - docker-compose ps - -rebuild: - docker-compose stop - docker-compose pull - docker-compose rm --force app - docker-compose build --no-cache --pull - docker-compose up -d --force-recreate - - -bash: - docker-compose exec --user application app /bin/bash - -root: - docker-compose exec --user root app /bin/bash - -############################# -# Argument fix workaround -############################# -%: - @: diff --git a/composer.json b/composer.json index 9ef89df..7915fc1 100644 --- a/composer.json +++ b/composer.json @@ -30,35 +30,35 @@ } ], "require": { - "php": "^7.4|^8.0", + "php": "^8.2", - "symfony/framework-bundle": "^4.4|^5.4|^6.0", - "symfony/config": "^4.4|^5.4|^6.0", - "symfony/http-kernel": "^4.4|^5.4|^6.0", - "symfony/dependency-injection": "^4.4|^5.4|^6.0", - "symfony/form": "^4.4|^5.4|^6.0", - "symfony/yaml": "^4.4|^5.4|^6.0", - "symfony/options-resolver": "^4.4|^5.4|^6.0", - "symfony/property-access": "^4.4|^5.4|^6.0", - "symfony/property-info": "^4.4|^5.4|^6.0", + "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0", + "symfony/config": "^5.4 || ^6.4 || ^7.0", + "symfony/http-kernel": "^5.4 || ^6.4 || ^7.0", + "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0", + "symfony/form": "^5.4 || ^6.4 || ^7.0", + "symfony/yaml": "^5.4 || ^6.4 || ^7.0", + "symfony/options-resolver": "^5.4 || ^6.4 || ^7.0", + "symfony/property-access": "^5.4 || ^6.4 || ^7.0", + "symfony/property-info": "^5.4 || ^6.4 || ^7.0", - "doctrine/persistence": "^1.3|^2.0|^3.0", + "doctrine/persistence": "^3.0", "webmozart/assert": "^1.9" }, "require-dev": { - "icanhazstring/composer-unused": "~0.7", - "doctrine/coding-standard": "^9.0", + "icanhazstring/composer-unused": "~0.8", + "doctrine/coding-standard": "^12.0", "roave/security-advisories": "dev-master", - "squizlabs/php_codesniffer": "^3.6", - "phpstan/phpstan": "~1.2", - "phpstan/phpstan-deprecation-rules": "~1.0", - "phpstan/phpstan-phpunit": "~1.0", - "phpstan/phpstan-strict-rules": "~1.1", - "phpstan/phpstan-webmozart-assert": "~1.0", - "maglnet/composer-require-checker": "^4.0", - "phpunit/phpunit": "^9.5", - "symfony/var-dumper": "^6.0", - "phpstan/phpstan-symfony": "^1.0" + "squizlabs/php_codesniffer": "^3.7", + "phpstan/phpstan": "~1.10", + "phpstan/phpstan-deprecation-rules": "~1.1", + "phpstan/phpstan-phpunit": "~1.3", + "phpstan/phpstan-strict-rules": "~1.5", + "phpstan/phpstan-webmozart-assert": "~1.2", + "maglnet/composer-require-checker": "^4.7", + "phpstan/phpstan-symfony": "^1.3", + "phpunit/phpunit": "^10.5", + "symfony/var-dumper": "^7.0" }, "autoload": { "psr-4": { @@ -74,7 +74,6 @@ "check": [ "@crc", "@unused", - "@cs-fix", "@cs-check", "@phpstan", "@phpunit" diff --git a/docker-compose.yml b/docker-compose.yml index cb760f3..9fe60d4 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,9 @@ version: '3.6' services: app: - image: webdevops/php:7.4 + image: webdevops/php:8.3 volumes: - ./:/app/ - env_file: - - etc/environment.yml + environment: + PHP_MEMORY_LIMIT: 4G + PHP_DATE_TIMEZONE: Europe/Berlin diff --git a/etc/environment.yml b/etc/environment.yml deleted file mode 100644 index deb0e78..0000000 --- a/etc/environment.yml +++ /dev/null @@ -1,28 +0,0 @@ -####################################### -# PHP settings - -# PHP.ini - -php.memory_limit=4G -php.date.timezone=Europe/Berlin - -# php.display_errors=1 -# php.max_execution_time=600 -# php.post_max_size=50M -# php.upload_max_filesize=50M - -# FPM global (master) - -# fpm.global.process_control_timeout=5m -# fpm.global.process.max=100 - -# FPM pool - -# fpm.pool.pm=dynamic -# fpm.pool.pm.max_children=100 -# fpm.pool.pm.start_servers=20 -# fpm.pool.pm.min_spare_servers=10 -# fpm.pool.pm.max_spare_servers=33 -# fpm.pool.pm.process_idle_timeout=10s -# fpm.pool.pm.max_requests=1000 -# fpm.pool.request_terminate_timeout=5m \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml index 13f7d95..1653c8a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,7 +7,7 @@ - + src/ tests/ diff --git a/phpunit.xml b/phpunit.xml index 295e8e9..c971de6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,22 @@ + cacheDirectory=".phpunit.cache" + requireCoverageMetadata="true" + beStrictAboutCoverageMetadata="true" +> + tests - - - + + src - - + + diff --git a/src/DependencyInjection/ShapecodeHiddenEntityTypeExtension.php b/src/DependencyInjection/ShapecodeHiddenEntityTypeExtension.php index 7eaa949..e87e573 100755 --- a/src/DependencyInjection/ShapecodeHiddenEntityTypeExtension.php +++ b/src/DependencyInjection/ShapecodeHiddenEntityTypeExtension.php @@ -11,9 +11,7 @@ class ShapecodeHiddenEntityTypeExtension extends Extension { - /** - * @inheritDoc - */ + /** @inheritDoc */ public function load(array $configs, ContainerBuilder $container): void { $locator = new FileLocator(__DIR__ . '/../Resources/config'); diff --git a/src/Form/DataTransformer/ObjectToIdTransformer.php b/src/Form/DataTransformer/ObjectToIdTransformer.php index b428f30..2100d73 100755 --- a/src/Form/DataTransformer/ObjectToIdTransformer.php +++ b/src/Form/DataTransformer/ObjectToIdTransformer.php @@ -13,52 +13,40 @@ use function is_string; use function sprintf; -/** - * @template-extends Transformer - */ +/** @template-extends Transformer */ class ObjectToIdTransformer extends Transformer { - /** - * @phpstan-param object|null $entity - * - * @phpstan-return string|null - */ - public function transform($entity) + public function transform(mixed $value): mixed { - if ($entity === null) { + if ($value === null) { return null; } - Assert::isInstanceOf($entity, $this->class); + Assert::isInstanceOf($value, $this->class); $accessor = PropertyAccess::createPropertyAccessor(); $property = $this->getProperty(); - if (! $accessor->isReadable($entity, $property)) { + if (! $accessor->isReadable($value, $property)) { return null; } - $value = $accessor->getValue($entity, $property); + $valueObject = $accessor->getValue($value, $property); - if ($value === null) { + if ($valueObject === null) { return null; } - if (! is_string($value) && ! is_numeric($value)) { + if (! is_string($valueObject) && ! is_numeric($valueObject)) { throw new LogicException('id hast to be string or integer', 1653564596059); } - return (string) $value; + return (string) $valueObject; } - /** - * @phpstan-param string|null $id - * - * @phpstan-return object|null - */ - public function reverseTransform($id): ?object + public function reverseTransform(mixed $value): mixed { - if ($id === null) { + if ($value === null) { return null; } @@ -67,11 +55,14 @@ public function reverseTransform($id): ?object $class = $this->getClass(); $result = $repo->findOneBy([ - $property => $id, + $property => $value, ]); if ($result === null) { - throw new TransformationFailedException(sprintf('Can\'t find entity of class "%s" with property "%s" = "%s".', $class, $property, $id)); + throw new TransformationFailedException( + sprintf('Can\'t find entity of class "%s" with property "%s" = "%s".', $class, $property, $value), + 1701526691297, + ); } Assert::isInstanceOf($result, $this->class); diff --git a/src/Form/DataTransformer/ObjectsToIdTransformer.php b/src/Form/DataTransformer/ObjectsToIdTransformer.php index 6d3c4a8..d9da5cf 100755 --- a/src/Form/DataTransformer/ObjectsToIdTransformer.php +++ b/src/Form/DataTransformer/ObjectsToIdTransformer.php @@ -13,48 +13,36 @@ use function implode; use function sprintf; -/** - * @template-extends Transformer - */ +/** @template-extends Transformer */ class ObjectsToIdTransformer extends Transformer { - /** - * @phpstan-param object[]|null $entity - * - * @phpstan-return string|null - */ - public function transform($entity) + public function transform(mixed $value): mixed { - if ($entity === null) { + if ($value === null) { return null; } - Assert::allIsInstanceOf($entity, $this->class); + Assert::allIsInstanceOf($value, $this->class); $accessor = PropertyAccess::createPropertyAccessor(); $property = $this->getProperty(); - $value = []; + $valueIds = []; - foreach ($entity as $e) { + foreach ($value as $e) { if (! $accessor->isReadable($e, $property)) { continue; } - $value[] = $accessor->getValue($e, $property); + $valueIds[] = $accessor->getValue($e, $property); } - return implode(',', $value); + return implode(',', $valueIds); } - /** - * @phpstan-param string|null $id - * - * @phpstan-return object[] - */ - public function reverseTransform($id): array + public function reverseTransform(mixed $value): mixed { - if ($id === null) { + if ($value === null) { return []; } @@ -62,12 +50,15 @@ public function reverseTransform($id): array $property = $this->getProperty(); $class = $this->getClass(); - $ids = explode(',', $id); + $ids = explode(',', $value); $results = $repo->findBy([$property => $ids]); if (count($results) === 0) { - throw new TransformationFailedException(sprintf('Can\'t find entity of class "%s" with property "%s" = "%s".', $class, $property, $id)); + throw new TransformationFailedException( + sprintf('Can\'t find entity of class "%s" with property "%s" = "%s".', $class, $property, $value), + 1701526676576, + ); } Assert::allIsInstanceOf($results, $this->class); diff --git a/src/Form/DataTransformer/Transformer.php b/src/Form/DataTransformer/Transformer.php index ec6519e..1b1a6f3 100644 --- a/src/Form/DataTransformer/Transformer.php +++ b/src/Form/DataTransformer/Transformer.php @@ -23,29 +23,19 @@ */ abstract class Transformer implements DataTransformerInterface { - protected ManagerRegistry $registry; - - /** @var class-string */ - protected string $class; - - protected string $property; - - /** - * @param class-string $class - */ + /** @param class-string $class */ public function __construct( - ManagerRegistry $registry, - string $class, - string $property = 'id' + protected readonly ManagerRegistry $registry, + protected readonly string $class, + protected readonly string $property = 'id', ) { if (! class_exists($class)) { - throw new InvalidArgumentException(sprintf('Expected an existing class name. Got: "%s"', $class)); + throw new InvalidArgumentException( + sprintf('Expected an existing class name. Got: "%s"', $class), + 1701527124965, + ); } - $this->registry = $registry; - $this->class = $class; - $this->property = $property; - $this->validate(); } @@ -54,9 +44,7 @@ protected function getRepository(): ObjectRepository return $this->registry->getRepository($this->getClass()); } - /** - * @return class-string - */ + /** @return class-string */ protected function getClass(): string { return $this->class; @@ -75,7 +63,10 @@ protected function validate(): void $properties = $propertyInfo->getProperties($this->class) ?? []; if (! in_array($this->property, $properties, true)) { - throw new NoSuchPropertyException(sprintf('property %s is missing in class %s', $this->property, $this->class)); + throw new NoSuchPropertyException( + sprintf('property %s is missing in class %s', $this->property, $this->class), + 1701527107565, + ); } } } diff --git a/src/Form/Type/HiddenObjectType.php b/src/Form/Type/HiddenObjectType.php index e5d8009..e74a2d1 100644 --- a/src/Form/Type/HiddenObjectType.php +++ b/src/Form/Type/HiddenObjectType.php @@ -17,16 +17,12 @@ class HiddenObjectType extends AbstractType { - protected ManagerRegistry $registry; - - public function __construct(ManagerRegistry $registry) - { - $this->registry = $registry; + public function __construct( + protected readonly ManagerRegistry $registry, + ) { } - /** - * @param array $options - */ + /** @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options): void { $transformerClassName = $options['multiple'] === true ? ObjectsToIdTransformer::class : ObjectToIdTransformer::class; @@ -40,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $transformer = new $transformerClassName( $this->registry, $class, - $property + $property, ); $builder->addModelTransformer($transformer); diff --git a/tests/Form/DataTransformer/ObjectToIdTransformerTest.php b/tests/Form/DataTransformer/ObjectToIdTransformerTest.php index 42dd2a9..17d2299 100644 --- a/tests/Form/DataTransformer/ObjectToIdTransformerTest.php +++ b/tests/Form/DataTransformer/ObjectToIdTransformerTest.php @@ -7,6 +7,7 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectRepository; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Shapecode\Bundle\HiddenEntityTypeBundle\Form\DataTransformer\ObjectToIdTransformer; use Shapecode\Bundle\HiddenEntityTypeBundle\Tests\Model\TestFormModel; @@ -14,9 +15,7 @@ use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; -/** - * @covers \Shapecode\Bundle\HiddenEntityTypeBundle\Form\DataTransformer\ObjectToIdTransformer - */ +#[CoversClass(ObjectToIdTransformer::class)] class ObjectToIdTransformerTest extends TestCase { public function testValidTransformation(): void diff --git a/tests/Form/DataTransformer/ObjectsToIdTransformerTest.php b/tests/Form/DataTransformer/ObjectsToIdTransformerTest.php index 97589fc..6ef1db7 100644 --- a/tests/Form/DataTransformer/ObjectsToIdTransformerTest.php +++ b/tests/Form/DataTransformer/ObjectsToIdTransformerTest.php @@ -7,6 +7,7 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectRepository; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Shapecode\Bundle\HiddenEntityTypeBundle\Form\DataTransformer\ObjectsToIdTransformer; use Shapecode\Bundle\HiddenEntityTypeBundle\Tests\Model\TestFormModel; @@ -14,9 +15,7 @@ use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; -/** - * @covers \Shapecode\Bundle\HiddenEntityTypeBundle\Form\DataTransformer\ObjectsToIdTransformer - */ +#[CoversClass(ObjectsToIdTransformer::class)] class ObjectsToIdTransformerTest extends TestCase { public function testValidTransformation(): void diff --git a/tests/Form/Type/HiddenObjectTypeFailedTest.php b/tests/Form/Type/HiddenObjectTypeFailedTest.php index 7562b5c..836310f 100644 --- a/tests/Form/Type/HiddenObjectTypeFailedTest.php +++ b/tests/Form/Type/HiddenObjectTypeFailedTest.php @@ -6,6 +6,7 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectRepository; +use PHPUnit\Framework\Attributes\CoversClass; use Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenObjectType; use Shapecode\Bundle\HiddenEntityTypeBundle\Tests\Model\TestFormModel; use Symfony\Component\Form\FormExtensionInterface; @@ -14,9 +15,7 @@ use function array_keys; -/** - * @covers \Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenObjectType - */ +#[CoversClass(HiddenObjectType::class)] class HiddenObjectTypeFailedTest extends TypeTestCase { private ManagerRegistry $registry; @@ -36,9 +35,7 @@ protected function setUp(): void parent::setUp(); } - /** - * @return list - */ + /** @return list */ protected function getExtensions(): array { // create a type instance with the mocked dependencies diff --git a/tests/Form/Type/HiddenObjectTypeTest.php b/tests/Form/Type/HiddenObjectTypeTest.php index 5f9c12b..b1c45d7 100644 --- a/tests/Form/Type/HiddenObjectTypeTest.php +++ b/tests/Form/Type/HiddenObjectTypeTest.php @@ -6,6 +6,7 @@ use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectRepository; +use PHPUnit\Framework\Attributes\CoversClass; use Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenObjectType; use Shapecode\Bundle\HiddenEntityTypeBundle\Tests\Model\TestFormModel; use Shapecode\Bundle\HiddenEntityTypeBundle\Tests\Model\TestObject; @@ -16,9 +17,7 @@ use function array_keys; use function assert; -/** - * @covers \Shapecode\Bundle\HiddenEntityTypeBundle\Form\Type\HiddenObjectType - */ +#[CoversClass(HiddenObjectType::class)] class HiddenObjectTypeTest extends TypeTestCase { private ManagerRegistry $registry; @@ -45,9 +44,7 @@ protected function setUp(): void parent::setUp(); } - /** - * @return list - */ + /** @return list */ protected function getExtensions(): array { // create a type instance with the mocked dependencies diff --git a/tests/Form/Type/TestFormType.php b/tests/Form/Type/TestFormType.php index 9dfc986..2dec703 100644 --- a/tests/Form/Type/TestFormType.php +++ b/tests/Form/Type/TestFormType.php @@ -13,9 +13,7 @@ class TestFormType extends AbstractType { - /** - * @inheritDoc - */ + /** @inheritDoc */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('object', HiddenObjectType::class, [ diff --git a/tests/Model/TestFormModel.php b/tests/Model/TestFormModel.php index d1e43ec..f07b1a1 100644 --- a/tests/Model/TestFormModel.php +++ b/tests/Model/TestFormModel.php @@ -6,14 +6,14 @@ class TestFormModel { - private ?TestObject $object = null; + private TestObject|null $object = null; - public function getObject(): ?TestObject + public function getObject(): TestObject|null { return $this->object; } - public function setObject(?TestObject $object): void + public function setObject(TestObject|null $object): void { $this->object = $object; } diff --git a/tests/Model/TestObject.php b/tests/Model/TestObject.php index a6b1689..98e3dca 100644 --- a/tests/Model/TestObject.php +++ b/tests/Model/TestObject.php @@ -6,14 +6,14 @@ class TestObject { - protected ?string $name = null; + protected string|null $name = null; - public function getName(): ?string + public function getName(): string|null { return $this->name; } - public function setName(?string $name): void + public function setName(string|null $name): void { $this->name = $name; }