Skip to content

Commit

Permalink
minor #42109 [Form] Add types to private properties (derrabus)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.0 branch.

Discussion
----------

[Form] Add types to private properties

| Q             | A
| ------------- | ---
| Branch?       | 6.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

There's a lot to backport here. 😓

Commits
-------

5a9e663 [Form] Add types to private properties
  • Loading branch information
Tobion committed Aug 5, 2021
2 parents 1d93104 + 5a9e663 commit 8258238
Show file tree
Hide file tree
Showing 93 changed files with 265 additions and 494 deletions.
16 changes: 6 additions & 10 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@
*/
class IdReader
{
private $om;
private $classMetadata;
private $singleId;
private $intId;
private $idField;

/**
* @var IdReader|null
*/
private $associationIdReader;
private ObjectManager $om;
private ClassMetadata $classMetadata;
private bool $singleId;
private bool $intId;
private string $idField;
private ?self $associationIdReader = null;

public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
* entities.
*
* This property should only be accessed through queryBuilder.
*
* @var QueryBuilder
*/
private $queryBuilder;
private QueryBuilder $queryBuilder;

public function __construct(QueryBuilder $queryBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
{
protected $registry;

private $cache = [];
private array $cache = [];

public function __construct(ManagerRegistry $registry)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ abstract class DoctrineType extends AbstractType implements ResetInterface
/**
* @var IdReader[]
*/
private $idReaders = [];
private array $idReaders = [];

/**
* @var EntityLoaderInterface[]
*/
private $entityLoaders = [];
private array $entityLoaders = [];

/**
* Creates the label for a choice.
Expand Down
20 changes: 8 additions & 12 deletions src/Symfony/Component/Form/AbstractExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,31 @@ abstract class AbstractExtension implements FormExtensionInterface
*
* @var FormTypeInterface[]
*/
private $types;
private array $types;

/**
* The type extensions provided by this extension.
*
* @var FormTypeExtensionInterface[][]
*/
private $typeExtensions;
private array $typeExtensions;

/**
* The type guesser provided by this extension.
*
* @var FormTypeGuesserInterface|null
*/
private $typeGuesser;
private ?FormTypeGuesserInterface $typeGuesser = null;

/**
* Whether the type guesser has been loaded.
*
* @var bool
*/
private $typeGuesserLoaded = false;
private bool $typeGuesserLoaded = false;

/**
* {@inheritdoc}
*/
public function getType(string $name)
{
if (null === $this->types) {
if (!isset($this->types)) {
$this->initTypes();
}

Expand All @@ -68,7 +64,7 @@ public function getType(string $name)
*/
public function hasType(string $name)
{
if (null === $this->types) {
if (!isset($this->types)) {
$this->initTypes();
}

Expand All @@ -80,7 +76,7 @@ public function hasType(string $name)
*/
public function getTypeExtensions(string $name)
{
if (null === $this->typeExtensions) {
if (!isset($this->typeExtensions)) {
$this->initTypeExtensions();
}

Expand All @@ -93,7 +89,7 @@ public function getTypeExtensions(string $name)
*/
public function hasTypeExtensions(string $name)
{
if (null === $this->typeExtensions) {
if (!isset($this->typeExtensions)) {
$this->initTypeExtensions();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/AbstractRendererEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface
/**
* @var array<array<int|false>>
*/
private $resourceHierarchyLevels = [];
private array $resourceHierarchyLevels = [];

/**
* Creates a new renderer engine.
Expand Down
17 changes: 3 additions & 14 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,9 @@
*/
class Button implements \IteratorAggregate, FormInterface
{
/**
* @var FormInterface|null
*/
private $parent;

/**
* @var FormConfigInterface
*/
private $config;

/**
* @var bool
*/
private $submitted = false;
private ?FormInterface $parent = null;
private FormConfigInterface $config;
private bool $submitted = false;

/**
* Creates a new button from a form configuration.
Expand Down
29 changes: 5 additions & 24 deletions src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,11 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
{
protected $locked = false;

/**
* @var bool
*/
private $disabled = false;

/**
* @var ResolvedFormTypeInterface
*/
private $type;

/**
* @var string
*/
private $name;

/**
* @var array
*/
private $attributes = [];

/**
* @var array
*/
private $options;
private bool $disabled = false;
private ResolvedFormTypeInterface $type;
private string $name;
private array $attributes = [];
private array $options;

/**
* @throws InvalidArgumentException if the name is empty
Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Form/CallbackTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@

class CallbackTransformer implements DataTransformerInterface
{
private $transform;
private $reverseTransform;
private \Closure $transform;
private \Closure $reverseTransform;

/**
* @param callable $transform The forward transform callback
* @param callable $reverseTransform The reverse transform callback
*/
public function __construct(callable $transform, callable $reverseTransform)
{
$this->transform = $transform;
$this->reverseTransform = $reverseTransform;
$this->transform = $transform instanceof \Closure ? $transform : \Closure::fromCallable($transform);
$this->reverseTransform = $reverseTransform instanceof \Closure ? $reverseTransform : \Closure::fromCallable($reverseTransform);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
*/
abstract class AbstractStaticOption
{
private static $options = [];
private static array $options = [];

/** @var bool|callable|string|array|\Closure|ChoiceLoaderInterface */
private $option;
private bool|string|array|\Closure|ChoiceLoaderInterface $option;

/**
* @param mixed $option Any pseudo callable, array, string or bool to define a choice list option
Expand All @@ -41,7 +40,7 @@ final public function __construct(FormTypeInterface|FormTypeExtensionInterface $
{
$hash = CachingFactoryDecorator::generateHash([static::class, $formType, $vary]);

$this->option = self::$options[$hash] ?? self::$options[$hash] = $option;
$this->option = self::$options[$hash] ??= $option instanceof \Closure || !\is_callable($option) ? $option : \Closure::fromCallable($option);
}

final public function getOption(): mixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
*/
class CachingFactoryDecorator implements ChoiceListFactoryInterface, ResetInterface
{
private $decoratedFactory;
private ChoiceListFactoryInterface $decoratedFactory;

/**
* @var ChoiceListInterface[]
*/
private $lists = [];
private array $lists = [];

/**
* @var ChoiceListView[]
*/
private $views = [];
private array $views = [];

/**
* Generates a SHA-256 hash for the given value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
*/
class PropertyAccessDecorator implements ChoiceListFactoryInterface
{
private $decoratedFactory;
private $propertyAccessor;
private ChoiceListFactoryInterface $decoratedFactory;
private PropertyAccessorInterface $propertyAccessor;

public function __construct(ChoiceListFactoryInterface $decoratedFactory, PropertyAccessorInterface $propertyAccessor = null)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
*/
class LazyChoiceList implements ChoiceListInterface
{
private $loader;
private ChoiceLoaderInterface $loader;

/**
* The callable creating string values for each choice.
*
* If null, choices are cast to strings.
*
* @var callable|null
*/
private $value;
private ?\Closure $value;

/**
* Creates a lazily-loaded list using the given loader.
Expand All @@ -50,7 +48,7 @@ class LazyChoiceList implements ChoiceListInterface
public function __construct(ChoiceLoaderInterface $loader, callable $value = null)
{
$this->loader = $loader;
$this->value = $value;
$this->value = null === $value || $value instanceof \Closure ? $value : \Closure::fromCallable($value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Form\ChoiceList\Loader;

use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;

/**
* @author Jules Pietri <jules@heahprod.com>
Expand All @@ -21,10 +21,8 @@ abstract class AbstractChoiceLoader implements ChoiceLoaderInterface
{
/**
* The loaded choice list.
*
* @var ArrayChoiceList
*/
private $choiceList;
private ArrayChoiceList $choiceList;

/**
* @final
Expand All @@ -45,7 +43,7 @@ public function loadChoicesForValues(array $values, callable $value = null)
return [];
}

if ($this->choiceList) {
if (isset($this->choiceList)) {
return $this->choiceList->getChoicesForValues($values);
}

Expand All @@ -66,7 +64,7 @@ public function loadValuesForChoices(array $choices, callable $value = null)
return array_map($value, $choices);
}

if ($this->choiceList) {
if (isset($this->choiceList)) {
return $this->choiceList->getValuesForChoices($choices);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
class CallbackChoiceLoader extends AbstractChoiceLoader
{
private $callback;
private \Closure $callback;

/**
* @param callable $callback The callable returning iterable choices
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
$this->callback = $callback instanceof \Closure ? $callback : \Closure::fromCallable($callback);
}

protected function loadChoices(): iterable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
class FilterChoiceLoaderDecorator extends AbstractChoiceLoader
{
private $decoratedLoader;
private $filter;
private ChoiceLoaderInterface $decoratedLoader;
private \Closure $filter;

public function __construct(ChoiceLoaderInterface $loader, callable $filter)
{
$this->decoratedLoader = $loader;
$this->filter = $filter;
$this->filter = $filter instanceof \Closure ? $filter : \Closure::fromCallable($filter);
}

protected function loadChoices(): iterable
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Form/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class DebugCommand extends Command
protected static $defaultName = 'debug:form';
protected static $defaultDescription = 'Display form type information';

private $formRegistry;
private $namespaces;
private $types;
private $extensions;
private $guessers;
private $fileLinkFormatter;
private FormRegistryInterface $formRegistry;
private array $namespaces;
private array $types;
private array $extensions;
private array $guessers;
private ?FileLinkFormatter $fileLinkFormatter;

public function __construct(FormRegistryInterface $formRegistry, array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [], array $extensions = [], array $guessers = [], FileLinkFormatter $fileLinkFormatter = null)
{
Expand Down
Loading

0 comments on commit 8258238

Please sign in to comment.