Skip to content

Commit

Permalink
Added PHP stan + better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom32i committed Aug 19, 2022
1 parent 10cabeb commit a95fa2b
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 140 deletions.
26 changes: 15 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,24 @@
->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,

// @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5495
'binary_operator_spaces' => ['operators' => ['|' => null]]
])
;
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
26 changes: 7 additions & 19 deletions Builders/FormTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ class FormTreeBuilder
{
/**
* Form type with no children labels
*
* @var array
*/
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 @@ -53,17 +49,15 @@ public function getTree(FormView $view)
*
* @param array $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 +70,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 +90,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 +104,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
6 changes: 3 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,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 @@ -114,7 +114,7 @@ public function getConfigTreeBuilder()
public function addKeysConfig($key, $default = [])
{
$treeBuilder = new TreeBuilder($key);
$node = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root($key);
$node = $treeBuilder->getRootNode();

$node
->prototype('scalar')
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/ElaoFormTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -49,7 +49,7 @@ public function load(array $configs, ContainerBuilder $container)
* @param LoaderInterface $loader The loader
* @param array $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
2 changes: 1 addition & 1 deletion Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getExtendedType()
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
if ($this->autoGenerate) {
$resolver->setDefault('label', true);
Expand Down
2 changes: 1 addition & 1 deletion Form/Extension/ChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getExtendedType()
/**
* {@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
6 changes: 3 additions & 3 deletions Form/Extension/CollectionTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getExtendedType()
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
if ($this->autoGenerate) {
$resolver->setDefault('label_add', true);
Expand All @@ -50,9 +50,9 @@ public function configureOptions(OptionsResolver $resolver)
/**
* {@inheritdoc}
*/
public function finishView(FormView $view, FormInterface $form, array $options)
public function finishView(FormView $view, FormInterface $form, array $options): void
{
if ($this->treeBuilder && $this->keyBuilder && $options['allow_add'] && $options['prototype']) {
if (isset($this->treeBuilder) && isset($this->keyBuilder) && $options['allow_add'] && $options['prototype']) {
if ($view->vars['prototype']->vars['label'] == $options['prototype_name'] . 'label__') {
if (!isset($options['options']['label'])) {
$options['options']['label'] = $options['label'];
Expand Down
2 changes: 1 addition & 1 deletion Form/Extension/FormTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getExtendedType()
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
if ($this->autoGenerate) {
$resolver->setDefault('label', true);
Expand Down
Loading

0 comments on commit a95fa2b

Please sign in to comment.