diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index d5ef888..2b834e8 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -34,8 +34,5 @@ 'simplified_null_return' => false, 'void_return' => true, 'yoda_style' => [], - - // @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5495 - 'binary_operator_spaces' => ['operators' => ['|' => null]] ]) ; diff --git a/Builders/FormTreeBuilder.php b/Builders/FormTreeBuilder.php index 722c60c..da282fc 100644 --- a/Builders/FormTreeBuilder.php +++ b/Builders/FormTreeBuilder.php @@ -23,6 +23,8 @@ class FormTreeBuilder { /** * Form type with no children labels + * + * @var array $noChildren */ private array $noChildren = ['date', 'time', 'datetime', 'choice']; @@ -47,7 +49,7 @@ public function getTree(FormView $view): FormTree /** * Set form type that should not be treated as having children * - * @param array $types An array of types + * @param array $types An array of types */ public function setNoChildren(array $types): void { diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index df365d5..6261b6d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -106,12 +106,11 @@ public function getConfigTreeBuilder(): TreeBuilder /** * Add Keys Config * - * @param string $key - * @param array $default + * @param array $default * * @return ArrayNodeDefinition|NodeDefinition */ - public function addKeysConfig($key, $default = []) + public function addKeysConfig(string $key, array $default = []) { $treeBuilder = new TreeBuilder($key); $node = $treeBuilder->getRootNode(); diff --git a/DependencyInjection/ElaoFormTranslationExtension.php b/DependencyInjection/ElaoFormTranslationExtension.php index c2af423..2d7b125 100644 --- a/DependencyInjection/ElaoFormTranslationExtension.php +++ b/DependencyInjection/ElaoFormTranslationExtension.php @@ -47,7 +47,7 @@ public function load(array $configs, ContainerBuilder $container): void * * @param ContainerBuilder $container The container builder * @param LoaderInterface $loader The loader - * @param array $config An array of config keys + * @param array $config An array of config keys */ private function loadTreeConfig(ContainerBuilder $container, LoaderInterface $loader, array $config): void { diff --git a/Form/Extension/ButtonTypeExtension.php b/Form/Extension/ButtonTypeExtension.php index 6780038..0e1dd2f 100644 --- a/Form/Extension/ButtonTypeExtension.php +++ b/Form/Extension/ButtonTypeExtension.php @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable return [ButtonType::class]; } - /** - * {@inheritdoc} - */ - public function getExtendedType() - { - return self::getExtendedTypes()[0]; - } - /** * {@inheritdoc} */ diff --git a/Form/Extension/ChoiceTypeExtension.php b/Form/Extension/ChoiceTypeExtension.php index 5d4dd38..46da940 100644 --- a/Form/Extension/ChoiceTypeExtension.php +++ b/Form/Extension/ChoiceTypeExtension.php @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable return [ChoiceType::class]; } - /** - * {@inheritdoc} - */ - public function getExtendedType() - { - return self::getExtendedTypes()[0]; - } - /** * {@inheritdoc} */ diff --git a/Form/Extension/CollectionTypeExtension.php b/Form/Extension/CollectionTypeExtension.php index b2ae0b9..c685d87 100644 --- a/Form/Extension/CollectionTypeExtension.php +++ b/Form/Extension/CollectionTypeExtension.php @@ -28,14 +28,6 @@ public static function getExtendedTypes(): iterable return [CollectionType::class]; } - /** - * {@inheritdoc} - */ - public function getExtendedType() - { - return self::getExtendedTypes()[0]; - } - /** * {@inheritdoc} */ diff --git a/Form/Extension/FormTypeExtension.php b/Form/Extension/FormTypeExtension.php index eb79acb..a491f51 100644 --- a/Form/Extension/FormTypeExtension.php +++ b/Form/Extension/FormTypeExtension.php @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable return [FormType::class]; } - /** - * {@inheritdoc} - */ - public function getExtendedType() - { - return self::getExtendedTypes()[0]; - } - /** * {@inheritdoc} */ diff --git a/Form/Extension/TreeAwareExtension.php b/Form/Extension/TreeAwareExtension.php index d85edae..46c3d4b 100644 --- a/Form/Extension/TreeAwareExtension.php +++ b/Form/Extension/TreeAwareExtension.php @@ -35,6 +35,8 @@ abstract class TreeAwareExtension extends AbstractTypeExtension /** * Buildable keys list + * + * @var array */ protected array $keys; @@ -67,7 +69,7 @@ public function setAutoGenerate(bool $enabled): void * * @param FormTreeBuilder $treeBuilder The FormKeyBuilder */ - public function setTreeBuilder(FormTreeBuilder $treeBuilder = null): void + public function setTreeBuilder(FormTreeBuilder $treeBuilder): void { $this->treeBuilder = $treeBuilder; } @@ -77,7 +79,7 @@ public function setTreeBuilder(FormTreeBuilder $treeBuilder = null): void * * @param FormKeyBuilder $keyBuilder The FormKeyBuilder */ - public function setKeyBuilder(FormKeyBuilder $keyBuilder = null): void + public function setKeyBuilder(FormKeyBuilder $keyBuilder): void { $this->keyBuilder = $keyBuilder; } @@ -85,7 +87,7 @@ public function setKeyBuilder(FormKeyBuilder $keyBuilder = null): void /** * Set buildable keys * - * @param array $keys Array of keys + * @param array $keys Array of keys */ public function setKeys(array $keys): void { @@ -132,7 +134,10 @@ protected function generateKey(FormView &$view, $key, $value): void $this->setVar($view->vars, $key, $this->keyBuilder->buildKeyFromTree($view->vars['tree'], $value)); } - protected function setVar(array &$vars, string $key, $value): void + /** + * @param array &$vars + */ + protected function setVar(array &$vars, string $key, mixed $value): void { if ($this->getPropertyAccessor()->isWritable($vars, $key)) { $this->getPropertyAccessor()->setValue($vars, $key, $value); @@ -141,7 +146,10 @@ protected function setVar(array &$vars, string $key, $value): void } } - protected function optionEquals(array $options, string $key, $value): bool + /** + * @param array $options + */ + protected function optionEquals(array $options, string $key, mixed $value): bool { if ($this->getPropertyAccessor()->isReadable($options, $key)) { return $this->getPropertyAccessor()->getValue($options, $key) === $value; diff --git a/Makefile b/Makefile index d6f34e8..94a8061 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,30 @@ -.PHONY: test - -PHP_CS_FIXER_VERSION=v3.10.0 +.SILENT: +.PHONY: test build ########### # Helpers # ########### +## Colors +COLOR_RESET = \033[0m +COLOR_INFO = \033[32m +COLOR_COMMENT = \033[33m + +## Help +help: + printf "${COLOR_COMMENT}Usage:${COLOR_RESET}\n" + printf " make [target]\n\n" + printf "${COLOR_COMMENT}Available targets:${COLOR_RESET}\n" + awk '/^[a-zA-Z\-\_0-9\.@]+:/ { \ + helpMessage = match(lastLine, /^## (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")); \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + printf " ${COLOR_INFO}%-16s${COLOR_RESET} %s\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) + define message_error printf "$(COLOR_ERROR)(╯°□°)╯︵ ┻━┻ $(strip $(1))$(COLOR_RESET)\n" endef @@ -13,39 +32,51 @@ endef php8: @php -r "exit (PHP_MAJOR_VERSION == 8 ? 0 : 1);" || ($(call message_error, Please use PHP 8) && exit 1) +########### +# Install # +########### + +## Install application +install: + # Composer + composer install --verbose + +############ +# Security # +############ + +## Run security checks +security: + symfony check:security + +security@test: export APP_ENV = test +security@test: security + ######## # Lint # ######## -lint: lint-phpcsfixer lint-phpstan lint-composer +## Run linters +lint: lint.phpcsfixer lint.phpstan lint.composer -lint-composer: +lint.composer: composer validate --strict -php-cs-fixer.phar: - wget --no-verbose https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/${PHP_CS_FIXER_VERSION}/php-cs-fixer.phar - chmod +x php-cs-fixer.phar - -update-php-cs-fixer.phar: - rm -f php-cs-fixer.phar - make php-cs-fixer.phar - -lint-phpcsfixer: php8 -lint-phpcsfixer: php-cs-fixer.phar -lint-phpcsfixer: - ./php-cs-fixer.phar fix --dry-run --diff +lint.phpcsfixer: export PHP_CS_FIXER_IGNORE_ENV = true +lint.phpcsfixer: php8 + vendor/bin/php-cs-fixer -fix-phpcsfixer: php8 -fix-phpcsfixer: php-cs-fixer.phar -fix-phpcsfixer: - ./php-cs-fixer.phar fix +fix.phpcsfixer: export PHP_CS_FIXER_IGNORE_ENV = true +fix.phpcsfixer: php8 + vendor/bin/php-cs-fixer fix -lint-phpstan: +lint.phpstan: vendor/bin/phpstan analyse --memory-limit=-1 ######## # Test # ######## +## Run tests test: vendor/bin/simple-phpunit diff --git a/Model/FormTree.php b/Model/FormTree.php index 76192f5..23ab5b6 100644 --- a/Model/FormTree.php +++ b/Model/FormTree.php @@ -14,11 +14,16 @@ * A form Tree * * @author Thomas Jarrand + * + * @implements \Iterator + * @implements \ArrayAccess */ class FormTree implements \Iterator, \Countable, \ArrayAccess { /** * The FormTreeNode elements + * + * @var array $nodes */ private array $nodes; @@ -27,6 +32,9 @@ class FormTree implements \Iterator, \Countable, \ArrayAccess */ private int $position = 0; + /** + * @param array $nodes + */ public function __construct(array $nodes = []) { $this->nodes = $nodes; @@ -126,7 +134,7 @@ public function offsetExists($offset): bool * @param mixed $offset */ #[\ReturnTypeWillChange] - public function offsetGet($offset): FormTreeNode + public function offsetGet($offset): ?FormTreeNode { return $this->offsetExists($offset) ? $this->nodes[$offset] : null; } diff --git a/Tests/Builders/FormTreeBuilderTest.php b/Tests/Builders/FormTreeBuilderTest.php index 486b13f..5c53466 100644 --- a/Tests/Builders/FormTreeBuilderTest.php +++ b/Tests/Builders/FormTreeBuilderTest.php @@ -33,13 +33,16 @@ public function testSimpleTreeBuilder(): void $barTree = $treeBuilder->getTree($formView['bar']); $this->assertEquals(1, \count($fooTree)); + $this->assertNotNull($fooTree[0]); $this->assertEquals('foo', $fooTree[0]->getName()); $this->assertEquals(true, $fooTree[0]->hasChildren()); $this->assertEquals(2, \count($barTree)); + $this->assertNotNull($barTree[0]); $this->assertEquals('foo', $barTree[0]->getName()); - $this->assertEquals('bar', $barTree[1]->getName()); $this->assertEquals(true, $barTree[0]->hasChildren()); + $this->assertNotNull($barTree[1]); + $this->assertEquals('bar', $barTree[1]->getName()); $this->assertEquals(false, $barTree[1]->hasChildren()); } @@ -64,11 +67,13 @@ public function testCollectionTreeBuilder(): void $formView = $form->createView(); $tree = $treeBuilder->getTree($formView['bar']->vars['prototype']); + $this->assertNotNull($tree[1]); $this->assertEquals(true, $tree[1]->hasChildren()); $this->assertEquals(true, $tree[1]->isCollection()); $this->assertEquals(false, $tree[1]->isPrototype()); $this->assertEquals(3, \count($tree)); + $this->assertNotNull($tree[2]); $this->assertEquals(false, $tree[2]->hasChildren()); $this->assertEquals(false, $tree[2]->isCollection()); $this->assertEquals(true, $tree[2]->isPrototype()); diff --git a/Tests/FormTranslationTestCase.php b/Tests/FormTranslationTestCase.php index 946802a..085de3e 100644 --- a/Tests/FormTranslationTestCase.php +++ b/Tests/FormTranslationTestCase.php @@ -13,13 +13,12 @@ use Elao\Bundle\FormTranslationBundle\Form\Extension; use PHPUnit\Framework\TestCase; use Symfony\Component\Form\Forms; +use Symfony\Component\Form\FormFactoryInterface; +use Symfony\Component\Form\FormTypeExtensionInterface; abstract class FormTranslationTestCase extends TestCase { - /** - * @var \Symfony\Component\Form\FormFactoryInterface - */ - protected $factory; + protected FormFactoryInterface $factory; /** * {@inheritdoc} @@ -34,17 +33,15 @@ protected function setUp(): void /** * Get Form Type Extensions * - * @return array + * @return array */ - protected function getTypeExtensions() + protected function getTypeExtensions(): array { - $extensions = [ + return [ new Extension\ButtonTypeExtension(), new Extension\ChoiceTypeExtension(), new Extension\CollectionTypeExtension(), new Extension\FormTypeExtension(), ]; - - return $extensions; } } diff --git a/composer.json b/composer.json index 10e0929..e81694e 100644 --- a/composer.json +++ b/composer.json @@ -16,10 +16,10 @@ } ], "require": { - "php": ">=7.4", - "symfony/framework-bundle": "~4.4|^5.1|^6.0", - "symfony/form": "~2.8|~3.0|~4.4|^5.1|^6.0", - "symfony/property-access": "~4.4|^5.1|^6.0" + "php": "^7.4|^8.0", + "symfony/framework-bundle": "^5.4|^6.0|^7.0", + "symfony/form": "^5.4|^6.0|^7.0", + "symfony/property-access": "^5.4|^6.0|^7.0" }, "require-dev": { "ekino/phpstan-banned-code": "^1.0", @@ -41,5 +41,10 @@ "branch-alias": { "dev-master": "4.x-dev" } + }, + "config": { + "allow-plugins": { + "phpstan/extension-installer": true + } } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ee007f0..4af2a3b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 5 + level: 8 paths: - "./"