Skip to content

Commit

Permalink
Symfony 7 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom32i committed Feb 23, 2024
1 parent a95fa2b commit a58960a
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 83 deletions.
3 changes: 0 additions & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
])
;
4 changes: 3 additions & 1 deletion Builders/FormTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class FormTreeBuilder
{
/**
* Form type with no children labels
*
* @var array<string> $noChildren
*/
private array $noChildren = ['date', 'time', 'datetime', 'choice'];

Expand All @@ -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<string> $types An array of types
*/
public function setNoChildren(array $types): void
{
Expand Down
5 changes: 2 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ public function getConfigTreeBuilder(): TreeBuilder
/**
* Add Keys Config
*
* @param string $key
* @param array $default
* @param array<string,string> $default
*
* @return ArrayNodeDefinition|NodeDefinition
*/
public function addKeysConfig($key, $default = [])
public function addKeysConfig(string $key, array $default = [])
{
$treeBuilder = new TreeBuilder($key);
$node = $treeBuilder->getRootNode();
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/ElaoFormTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed> $config An array of config keys
*/
private function loadTreeConfig(ContainerBuilder $container, LoaderInterface $loader, array $config): void
{
Expand Down
8 changes: 0 additions & 8 deletions Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable
return [ButtonType::class];
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return self::getExtendedTypes()[0];
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 0 additions & 8 deletions Form/Extension/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable
return [ChoiceType::class];
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return self::getExtendedTypes()[0];
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 0 additions & 8 deletions Form/Extension/CollectionTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ public static function getExtendedTypes(): iterable
return [CollectionType::class];
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return self::getExtendedTypes()[0];
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 0 additions & 8 deletions Form/Extension/FormTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ public static function getExtendedTypes(): iterable
return [FormType::class];
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return self::getExtendedTypes()[0];
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 13 additions & 5 deletions Form/Extension/TreeAwareExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class TreeAwareExtension extends AbstractTypeExtension

/**
* Buildable keys list
*
* @var array<string,string>
*/
protected array $keys;

Expand Down Expand Up @@ -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;
}
Expand All @@ -77,15 +79,15 @@ 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;
}

/**
* Set buildable keys
*
* @param array $keys Array of keys
* @param array<string,string> $keys Array of keys
*/
public function setKeys(array $keys): void
{
Expand Down Expand Up @@ -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<string,mixed> &$vars
*/
protected function setVar(array &$vars, string $key, mixed $value): void
{
if ($this->getPropertyAccessor()->isWritable($vars, $key)) {
$this->getPropertyAccessor()->setValue($vars, $key, $value);
Expand All @@ -141,7 +146,10 @@ protected function setVar(array &$vars, string $key, $value): void
}
}

protected function optionEquals(array $options, string $key, $value): bool
/**
* @param array<string,mixed> $options
*/
protected function optionEquals(array $options, string $key, mixed $value): bool
{
if ($this->getPropertyAccessor()->isReadable($options, $key)) {
return $this->getPropertyAccessor()->getValue($options, $key) === $value;
Expand Down
75 changes: 53 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,51 +1,82 @@
.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

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
10 changes: 9 additions & 1 deletion Model/FormTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
* A form Tree
*
* @author Thomas Jarrand <thomas.jarrand@gmail.com>
*
* @implements \Iterator<FormTreeNode>
* @implements \ArrayAccess<int,FormTreeNode>
*/
class FormTree implements \Iterator, \Countable, \ArrayAccess
{
/**
* The FormTreeNode elements
*
* @var array<FormTreeNode> $nodes
*/
private array $nodes;

Expand All @@ -27,6 +32,9 @@ class FormTree implements \Iterator, \Countable, \ArrayAccess
*/
private int $position = 0;

/**
* @param array<FormTreeNode> $nodes
*/
public function __construct(array $nodes = [])
{
$this->nodes = $nodes;
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/Builders/FormTreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
Expand Down
15 changes: 6 additions & 9 deletions Tests/FormTranslationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -34,17 +33,15 @@ protected function setUp(): void
/**
* Get Form Type Extensions
*
* @return array
* @return array<FormTypeExtensionInterface>
*/
protected function getTypeExtensions()
protected function getTypeExtensions(): array
{
$extensions = [
return [
new Extension\ButtonTypeExtension(),
new Extension\ChoiceTypeExtension(),
new Extension\CollectionTypeExtension(),
new Extension\FormTypeExtension(),
];

return $extensions;
}
}
Loading

0 comments on commit a58960a

Please sign in to comment.