Skip to content

PHPStan Rules 14.19

Choose a tag to compare

@TomasVotruba TomasVotruba released this 14 Oct 08:44
· 11 commits to main since this release
df19d02

A batch of new rules - Doctrine Collection typing, repository return types, Symfony controller actions, nullable getters and mocked return types - plus two new rule-set groups you can toggle.

New features 🎉

New modern and collections groups

Two new toggleable rule sets, both enabled by default (#360, #363):

parameters:
    symplify:
        # enabled by default
        modern: true
        collections: true
  • modern groups trait, reference, dynamic-name and foreach-ception rules.
  • collections groups the Doctrine Collection typing rules below.

Doctrine Collection typing rules

Four new rules keep Doctrine Collection usage strictly typed (#363).

CollectionGetterMustReturnCollectionRule - getter of a to-many association must return Collection:

 use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;

 #[ORM\Table]
 #[ORM\Entity]
 class Product
 {
     #[ORM\OneToMany(targetEntity: Item::class, mappedBy: 'product')]
     private $items;

-    public function getItems()
+    public function getItems(): Collection
     {
         return $this->items;
     }
 }

CollectionReturnMustHaveGenericDocblockRule, CollectionParamMustHaveGenericDocblockRule, CollectionPropertyMustHaveGenericDocblockRule - no bare Collection, always a generic docblock:

 use Doctrine\Common\Collections\Collection;

+/** @var Collection<int, Item> */
 private Collection $items;

+/** @param Collection<int, Item> $items */
 public function setItems(Collection $items): void
 {
 }

+/** @return Collection<int, Item> */
 public function getItems(): Collection
 {
 }

RepositoryMethodMustHaveReturnTypeRule

Public repository methods must declare a return type (#363):

 use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

 final class ProductRepository extends ServiceEntityRepository
 {
-    public function findActive()
+    public function findActive(): array
     {
         return $this->findBy(['active' => true]);
     }
 }

NullablePropertyGetterMustReturnNullableRule

Getter of a nullable property must return a nullable type (#363):

 private ?string $name = null;

-public function getName(): string
+public function getName(): ?string
 {
     return $this->name;
 }

NoReadonlyApiParamRule

An @api param may be swapped via reflection in tests, so it must not be readonly (#363):

 /**
  * @param string $service @api
  */
 public function __construct(
-    private readonly string $service,
+    private string $service,
 ) {
 }

NoPublicNonActionMethodInControllerRule

Controllers expose only route actions; helpers stay protected/private (#363):

 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Attribute\Route;

 final class ProductController extends AbstractController
 {
     #[Route('/products')]
     public function list(): Response
     {
         // ...
     }

-    public function helper()
+    private function helper()
     {
     }
 }

MockedMethodReturnMustMatchNativeTypeRule

willReturn() value must match the mocked method's native return type (#363):

class SomeObject
{
    public function getName(): string
    {
        return '';
    }
}
 use PHPUnit\Framework\MockObject\MockObject;

 public function run(SomeObject&MockObject $mock): void
 {
-    $mock->method('getName')->willReturn(123);
+    $mock->method('getName')->willReturn('name');
 }

PreferGenericTemplateOnSameUnionTypeRule (rector)

When a param and return share the same class-type union, add a @template to narrow the return to the passed type (#349):

 use PhpParser\Node\Stmt\ClassMethod;
 use PhpParser\Node\Stmt\Function_;

+/**
+ * @template TNode as ClassMethod|Function_
+ * @param TNode $node
+ * @return TNode
+ */
 public function process(ClassMethod|Function_ $node): ClassMethod|Function_
 {
     return $node;
 }

Bugfixes 🐛

  • Detect test class via TestCase reflection in NoAssertFuncCallInTestsRule (#361)
  • [symfony] Skip #[AsDoctrineListener] in NoListenerWithoutContractRule (#358)
  • Allow array method call on non-public methods in ForbiddenArrayMethodCallRule (#357)
  • [doctrine] Skip traits in RequireQueryBuilderOnRepositoryRule (#355)
  • Skip traits in NoAssertFuncCallInTestsRule (#356)
  • Move FormTypeClassNameRule to naming set (#354)
  • Remove curl_* and array_walk from forbidden functions (#353)
  • [symfony] Skip Laravel commands in CommandMustHaveAsCommandAttributeRule (#351)
  • Inline rule identifier constants into each rule (#348)
  • Restore optional ForbiddenNodeRule (#346, #347)
  • Add README tip to prefer group toggles over specific rules (#366)

Removed 💀

  • Remove ExplicitExpectsMockMethodRule as too specific (#364)
  • Remove NoMockOnlyTestRule as not relevant to check (#365)
  • Remove NoRepositoryCallInDataFixtureRule (#362)
  • Remove NoDuplicateNonRepeatableAttributeRule (#359)