Skip to content

Add and improve docblock typing for static analysis #40783

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

Closed
wants to merge 13 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements DataTransformerInterface<Collection, array>
*/
class CollectionToArrayTransformer implements DataTransformerInterface
{
/**
* Transforms a collection into an array.
*
* @return mixed An array of entities
* @return mixed[] An array of entities
*
* @throws TransformationFailedException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -338,6 +339,13 @@ protected function createAccessDeniedException(string $message = 'Access Denied.

/**
* Creates and returns a Form instance from the type of the form.
*
* @template TData
* @template TFormType of FormTypeInterface<TData>
*
* @param class-string<TFormType> $type
*
* @return FormInterface<TData>
*/
protected function createForm(string $type, $data = null, array $options = []): FormInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Config/Resource/GlobResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @implements \IteratorAggregate<mixed, \SplFileInfo>
*
* @final
*/
class GlobResource implements \IteratorAggregate, SelfCheckingResourceInterface
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Helper/HelperSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* HelperSet represents a set of helpers to be used with a command.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @implements \IteratorAggregate<Helper>
*/
class HelperSet implements \IteratorAggregate
{
Expand Down Expand Up @@ -89,7 +91,7 @@ public function getCommand()
}

/**
* @return Helper[]
* @return \Traversable<Helper>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether we should use \Traversable<Helper> or \Traversable<Helper>|Helper[]. Technically the first is correct but I believe that the latter is a common workaround for IDEs not being able to make sense of the generic \Traversable. Though I'm not sure this applies to the getIterator method in case the method is not called but the object is iterated over in a foreach.

*/
public function getIterator()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Crawler eases navigation of a list of \DOMNode objects.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @implements \IteratorAggregate<\DOMNode>
*/
class Crawler implements \Countable, \IteratorAggregate
{
Expand Down Expand Up @@ -1109,7 +1111,7 @@ public function count()
}

/**
* @return \ArrayIterator|\DOMNode[]
* @return \Traversable<\DOMNode>
*/
public function getIterator()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/EventDispatcher/GenericEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Encapsulates events thus decoupling the observer from the subject they encapsulate.
*
* @author Drak <drak@zikula.org>
*
* @implements \IteratorAggregate<string, mixed>
*/
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
{
Expand Down Expand Up @@ -161,7 +163,7 @@ public function offsetExists($key)
/**
* IteratorAggregate for iterating over the object like an array.
*
* @return \ArrayIterator
* @return \Traversable<string, mixed>
*/
public function getIterator()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @implements \IteratorAggregate<SplFileInfo>
*/
class Finder implements \IteratorAggregate, \Countable
{
Expand Down Expand Up @@ -601,7 +603,7 @@ public function in($dirs)
*
* This method implements the IteratorAggregate interface.
*
* @return \Iterator|SplFileInfo[] An iterator
* @return \Traversable<SplFileInfo> An iterator
*
* @throws \LogicException if the in() method has not been called
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T
* @implements FormTypeInterface<T>
*/
abstract class AbstractType implements FormTypeInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/AbstractTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T
* @implements FormTypeExtensionInterface<T>
*/
abstract class AbstractTypeExtension implements FormTypeExtensionInterface
{
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* A form button.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T
*
* @implements FormInterface<T>
*/
class Button implements \IteratorAggregate, FormInterface
{
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* A builder for {@link Button} instances.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T
*
* @implements FormBuilderInterface<T>
*/
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
{
Expand Down Expand Up @@ -135,7 +139,7 @@ public function all()
/**
* Creates the button.
*
* @return Button The button
* @return FormInterface<T> The button
*/
public function getForm()
{
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Form/CallbackTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@

namespace Symfony\Component\Form;

/**
* @template T
* @template R
*
* @implements DataTransformerInterface<T, R>
*/
class CallbackTransformer implements DataTransformerInterface
{
private $transform;
private $reverseTransform;

/**
* @param callable $transform The forward transform callback
* @param callable $reverseTransform The reverse transform callback
* @param callable(T):R $transform The forward transform callback
* @param callable(R):T $reverseTransform The reverse transform callback
*/
public function __construct(callable $transform, callable $reverseTransform)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Represents a group of choices in templates.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements \IteratorAggregate<self|ChoiceView>
*/
class ChoiceGroupView implements \IteratorAggregate
{
Expand All @@ -35,7 +37,7 @@ public function __construct(string $label, array $choices = [])
/**
* {@inheritdoc}
*
* @return self[]|ChoiceView[]
* @return \Traversable<self|ChoiceView>
*/
public function getIterator()
{
Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/Form/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* Transforms a value between different representations.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T
* @template R
*/
interface DataTransformerInterface
{
Expand Down Expand Up @@ -53,9 +56,9 @@ interface DataTransformerInterface
* of the first data transformer outputs NULL, the second must be able to
* process that value.
*
* @param mixed $value The value in the original representation
* @param T $value The value in the original representation
*
* @return mixed The value in the transformed representation
* @return R The value in the transformed representation
*
* @throws TransformationFailedException when the transformation fails
*/
Expand All @@ -82,9 +85,9 @@ public function transform($value);
* By convention, reverseTransform() should return NULL if an empty string
* is passed.
*
* @param mixed $value The value in the transformed representation
* @param R $value The value in the transformed representation
*
* @return mixed The value in the original representation
* @return T The value in the original representation
*
* @throws TransformationFailedException when the transformation fails
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Event/PostSetDataEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* This event is dispatched at the end of the Form::setData() method.
*
* This event is mostly here for reading data after having pre-populated the form.
*
* @template T
* @extends FormEvent<T>
*/
final class PostSetDataEvent extends FormEvent
{
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Event/PreSetDataEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* It can be used to:
* - Modify the data given during pre-population;
* - Modify a form depending on the pre-populated data (adding or removing fields dynamically).
*
* @template T
* @extends FormEvent<T>
*/
final class PreSetDataEvent extends FormEvent
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/Event/PreSubmitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* It can be used to:
* - Change data from the request, before submitting the data to the form.
* - Add or remove form fields, before submitting the data to the form.
*
* @extends FormEvent<array<string, mixed>>
*/
final class PreSubmitEvent extends FormEvent
{
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Event/SubmitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* transforms back the normalized data to the model and view data.
*
* It can be used to change data from the normalized representation of the data.
*
* @template T
* @extends FormEvent<T>
*/
final class SubmitEvent extends FormEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements DataTransformerInterface<array|null, array|null>
*/
class ArrayToPartsTransformer implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\InvalidArgumentException;

/**
* @template T
* @template R
*
* @implements DataTransformerInterface<T, R>
*/
abstract class BaseDateTimeTransformer implements DataTransformerInterface
{
protected static $formats = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org>
*
* @implements DataTransformerInterface<bool, string>
*/
class BooleanToStringTransformer implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements DataTransformerInterface<mixed, string>
*/
class ChoiceToValueTransformer implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements DataTransformerInterface<array, string[]>
*/
class ChoicesToValuesTransformer implements DataTransformerInterface
{
Expand All @@ -28,7 +30,7 @@ public function __construct(ChoiceListInterface $choiceList)
}

/**
* @return array
* @return string[]
*
* @throws TransformationFailedException if the given value is not an array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Passes a value through multiple value transformers.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements DataTransformerInterface<mixed, mixed>
*/
class DataTransformerChain implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Transforms between a normalized date interval and an interval string/array.
*
* @author Steffen Roßkamp <steffen.rosskamp@gimmickmedia.de>
*
* @implements DataTransformerInterface<\DateInterval, array>
*/
class DateIntervalToArrayTransformer implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Transforms between a date string and a DateInterval object.
*
* @author Steffen Roßkamp <steffen.rosskamp@gimmickmedia.de>
*
* @implements DataTransformerInterface<\DateInterval, string>
*/
class DateIntervalToStringTransformer implements DataTransformerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Transforms between a DateTimeImmutable object and a DateTime object.
*
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com>
*
* @implements DataTransformerInterface<\DateTimeImmutable|null, \DateTime|null>
*/
final class DateTimeImmutableToDateTimeTransformer implements DataTransformerInterface
{
Expand Down
Loading