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

Added PHP stan + better typing + Symfony 7 support #42

Merged
merged 2 commits into from
Mar 6, 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
24 changes: 16 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ jobs:
- name: 'Setup PHP'
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.3'

- name: 'Install dependencies'
run: make php-cs-fixer.phar
run: |
make install
vendor/bin/simple-phpunit --version

- name: 'Check style'
run: ./php-cs-fixer.phar fix --dry-run --no-interaction --diff
run: make lint

test:
name: ${{ matrix.name }}
Expand All @@ -40,10 +42,10 @@ jobs:
matrix:
include:
# Lowest deps
- name: 'Test lowest deps [Linux, PHP 7.4]'
- name: 'Test lowest deps Symfony 5.4 [Linux, PHP 7.4]'
os: 'ubuntu-latest'
php: '7.4'
symfony: '4.4.*@dev'
symfony: '5.4.*@dev'
composer-flags: '--prefer-lowest'
allow-unstable: true

Expand All @@ -60,11 +62,17 @@ jobs:
symfony: '6.0.*@dev'
allow-unstable: true

- name: 'Test next Symfony 6.4 [Linux, PHP 8.2]'
os: 'ubuntu-latest'
php: '8.2'
symfony: '6.4.*@dev'
allow-unstable: true

# Bleeding edge (unreleased dev versions where failures are allowed)
- name: 'Test next Symfony [Linux, PHP 8.1] (allowed failure)'
- name: 'Test next Symfony [Linux, PHP 8.3] (allowed failure)'
os: 'ubuntu-latest'
php: '8.1'
symfony: '6.1.*@dev'
php: '8.3'
symfony: '7.0.*@dev'
composer-flags: '--ignore-platform-req php'
allow-unstable: true
allow-failure: true
Expand Down
23 changes: 12 additions & 11 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
EOF;

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__
])
->in([__DIR__])
;

return (new PhpCsFixer\Config())
Expand All @@ -20,18 +18,21 @@
->setFinder($finder)
->setRules([
'@Symfony' => true,
'php_unit_namespaced' => true,
'psr_autoloading' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'phpdoc_summary' => false,
'header_comment' => ['header' => $header],
'native_function_invocation' => ['include' => ['@compiler_optimized']],
'ordered_imports' => true,
'php_unit_namespaced' => true,
'php_unit_method_casing' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_summary' => false,
'phpdoc_order' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'psr_autoloading' => true,
'single_line_throw' => false,
'simplified_null_return' => false,
'header_comment' => ['header' => $header],
'void_return' => true,
'yoda_style' => [],
'native_function_invocation' => ['include' => ['@compiler_optimized']],
'single_line_throw' => false,
])
;
26 changes: 11 additions & 15 deletions Builders/FormKeyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,23 @@ class FormKeyBuilder
{
/**
* Separator te be used between nodes
*
* @var string
*/
protected $separator;
protected string $separator;

/**
* Prefix at the root of the key
*
* @var string
*/
protected $root;
protected string $root;

/**
* Prefix for children nodes
*
* @var string
*/
protected $children;
protected string $children;

/**
* Prefix for prototype nodes
*
* @var string
*/
protected $prototype;
protected string $prototype;

/**
* Constructor
Expand All @@ -55,8 +47,12 @@ class FormKeyBuilder
* @param string $children Prefix for children nodes
* @param string $prototype Prefix for prototype nodes
*/
public function __construct($separator = '.', $root = 'form', $children = 'children', $prototype = 'prototype')
{
public function __construct(
string $separator = '.',
string $root = 'form',
string $children = 'children',
string $prototype = 'prototype'
) {
$this->separator = $separator;
$this->root = $root;
$this->children = $children;
Expand All @@ -71,7 +67,7 @@ public function __construct($separator = '.', $root = 'form', $children = 'child
*
* @return string The key
*/
public function buildKeyFromTree(FormTree $tree, $parent)
public function buildKeyFromTree(FormTree $tree, $parent): string
{
$key = [];

Expand Down
28 changes: 9 additions & 19 deletions Builders/FormTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ class FormTreeBuilder
/**
* Form type with no children labels
*
* @var array
* @var array<string>
*/
private $noChildren = ['date', 'time', 'datetime', 'choice'];
private array $noChildren = ['date', 'time', 'datetime', 'choice'];

/**
* Get the full tree for a given view
*
* @param FormView $view The FormView
*
* @return array
*/
public function getTree(FormView $view)
public function getTree(FormView $view): FormTree
{
if ($view->parent !== null) {
$tree = $this->getTree($view->parent);
Expand All @@ -51,19 +49,17 @@ public function getTree(FormView $view)
/**
* 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)
public function setNoChildren(array $types): void
{
$this->noChildren = $types;
}

/**
* Create a FormTreeNode for the given view
*
* @return FormTreeNode
*/
private function createNodeFromView(FormView $view)
private function createNodeFromView(FormView $view): FormTreeNode
{
$haschildren = $this->hasChildrenWithLabel($view);
$isCollection = $haschildren ? $this->isCollection($view) : false;
Expand All @@ -76,10 +72,8 @@ private function createNodeFromView(FormView $view)
* Test if the given form view has children with labels
*
* @param FormView $view The FormView
*
* @return bool
*/
private function hasChildrenWithLabel(FormView $view)
private function hasChildrenWithLabel(FormView $view): bool
{
if (!isset($view->vars['compound']) || !$view->vars['compound']) {
return false;
Expand All @@ -98,10 +92,8 @@ private function hasChildrenWithLabel(FormView $view)
* Test if the given form view is a collection
*
* @param FormView $view The FormView
*
* @return bool
*/
private function isCollection(FormView $view)
private function isCollection(FormView $view): bool
{
if ($view->parent === null || !$view->vars['compound']) {
return false;
Expand All @@ -114,10 +106,8 @@ private function isCollection(FormView $view)
* Test if the given form view is a prototype in a collection
*
* @param FormView $view The FormView
*
* @return bool
*/
private function isPrototype(FormView $view)
private function isPrototype(FormView $view): bool
{
return $view->parent && $this->isCollection($view->parent);
}
Expand Down
18 changes: 9 additions & 9 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('elao_form_translation');
$rootNode = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('elao_form_translation');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->info('<info>Activate the Form Tree component (used to generate label translation keys)</info>')
Expand Down Expand Up @@ -78,18 +75,22 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('children')
->defaultValue('children')
->treatNullLike(false)
->info('Prefix for children nodes (string|false)')
->end()
->scalarNode('prototype')
->defaultValue('prototype')
->treatNullLike(false)
->info('Prefix for prototype nodes (string|false)')
->end()
->scalarNode('root')
->defaultValue('form')
->treatNullLike(false)
->info('Prefix at the root of the key (string|false)')
->end()
->scalarNode('separator')
->defaultValue('.')
->treatNullLike(false)
->info('Separator te be used between nodes (string|false)')
->end()
->end()
Expand All @@ -106,15 +107,14 @@ public function getConfigTreeBuilder()
/**
* 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 = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root($key);
$node = $treeBuilder->getRootNode();

$node
->prototype('scalar')
Expand Down
13 changes: 5 additions & 8 deletions DependencyInjection/ElaoFormTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
*/
class ElaoFormTranslationExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand All @@ -45,11 +42,11 @@ public function load(array $configs, ContainerBuilder $container)
/**
* Load Tree configuration
*
* @param ContainerBuilder $container The container builder
* @param LoaderInterface $loader The loader
* @param array $config An array of config keys
* @param ContainerBuilder $container The container builder
* @param LoaderInterface $loader The loader
* @param array<string,mixed> $config An array of config keys
*/
private function loadTreeConfig(ContainerBuilder $container, LoaderInterface $loader, array $config)
private function loadTreeConfig(ContainerBuilder $container, LoaderInterface $loader, array $config): void
{
// Set up the Key Builder
$container
Expand Down
16 changes: 1 addition & 15 deletions Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,12 @@
*/
class ButtonTypeExtension extends TreeAwareExtension
{
/**
* {@inheritdoc}
*/
public static function getExtendedTypes(): iterable
{
return [ButtonType::class];
}

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

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
if ($this->autoGenerate) {
$resolver->setDefault('label', true);
Expand Down
16 changes: 1 addition & 15 deletions Form/Extension/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,12 @@
*/
class ChoiceTypeExtension extends TreeAwareExtension
{
/**
* {@inheritdoc}
*/
public static function getExtendedTypes(): iterable
{
return [ChoiceType::class];
}

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

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
if ($this->defaultTranslationDomain !== null) {
$resolver->setDefault('choice_translation_domain', $this->defaultTranslationDomain);
Expand Down
Loading
Loading