Use interfaces instead of classes in typehints

More information: https://insight.symfony.com/what-we-analyse/symfony.dependency_injection.use_interface_type_hint

New rule! We've recently added this rule to Insight. Don't be surprised to see new suggestions even though the codebase didn't change.
  1. /**
  2. * @param FormFactory $formFactory
  3. * @param RequestStack $requestStack
  4. * @param bool $formCsrfEnabled
  5. */
  6. public function __construct(FormFactory $formFactory, RequestStack $requestStack, $formCsrfEnabled)

    You should use the interface Symfony\Component\Form\FormFactoryInterface instead of the class Symfony\Component\Form\FormFactory as a typehint.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Last edited by PéCé
  7. {
  8. $this->formFactory = $formFactory;
  9. $this->requestStack = $requestStack;
  10. $this->formCsrfEnabled = $formCsrfEnabled;
  11. }

Object parameters should be type hinted

More information: https://insight.symfony.com/what-we-analyse/php.object_parameter_not_type_hinted

  1. // ManyToMany association
  2. elseif (isset($assoc['joinTable'])) {
  3. $relatedItems = PropertyAccess::createPropertyAccessor()->getValue(
  4. $parentEntity, $parentEntityProperty
  5. );
  6. $itemIds = $relatedItems->map(function ($entity) {

    The parameter entity, which is an object, should be typehinted.

    Time to fix: about 1 hour
    Open Issue Permalink
    Last edited by PéCé
  7. return $entity->getId();
  8. });
  9. return ['entity.id' => $itemIds->toArray()];
  10. }

Unused method, property, variable or parameter

More information: https://insight.symfony.com/what-we-analyse/php.unused_local_variable_or_private_member

  1. // Build $operatorValues if this is the first call
  2. if (null === static::$operatorValues) {
  3. static::$operatorValues = [];
  4. $refClass = new \ReflectionClass(static::class);
  5. $classConstants = $refClass->getConstants();
  6. $className = $refClass->getShortName();

    This className local variable is declared but never used. You should remove it.

    Time to fix: about 15 minutes
    Open Issue Permalink
    Last edited by PéCé
  7. $constantPrefix = 'OPERATOR_';
  8. foreach ($classConstants as $key => $val) {
  9. if (\substr($key, 0, \strlen($constantPrefix)) === $constantPrefix) {
  10. static::$operatorValues[] = $val;