phpstan/phpstan-beberlei-assert

Assertion::isInstanceOf is not working as expected

robertfausk opened this issue · 7 comments

Given controller:

# src/Controller/EventController.php
<?php
declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use Assert\Assertion;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

class EventController
{
    /**
     * @param UserInterface|User $user
     *
     * @return array
     * @Route("/events", name="event_list")
     * @Template(template="event/list.html.twig")
     *
     */
    public function __invoke(UserInterface $user): array
    {
        Assertion::isInstanceOf($user, User::class);
        $team = $user->getTeams();
        ...

Given User class:

<?php
declare(strict_types=1);

namespace App\Entity;

...
use FOS\UserBundle\Model\User as BaseUser; # this one implements \Symfony\Component\Security\Core\User\UserInterface
...

class User extends BaseUser
{
    /**
     * @return Collection|Team[]
     */
    public function getTeams()
    {
        ...
    }

Given phpstan.neon

parameters:
    level: 3
    paths:
        - %currentWorkingDirectory%/src
    includes:
        - vendor/phpstan/phpstan-beberlei-assert/extension.neon
    excludes_analyse:
        - %currentWorkingDirectory%/src/Migrations/Version*.php
    parameters:
        symfony:
            container_xml_path: %rootDir%/../../../var/cache/dev/srcDevDebugProjectContainer.xml

Given phpstan-output:

 ------ ----------------------------------------------------------------- 
  Line   Controller/EventController.php                                   
 ------ ----------------------------------------------------------------- 
  25     Call to an undefined method                                      
         Symfony\Component\Security\Core\User\UserInterface::getTeams().  
 ------ ----------------------------------------------------------------- 

Expected phpstan-output:

 [OK] No errors  

Am I missing something?

PS: I have the same issue with https://github.com/phpstan/phpstan-webmozart-assert. I will open an issue there if this one gets confirmed and if it's not a misconfiguration or misuse on my side.

Hi, you’re using webmozart/assert but you have installed extension for beberlei/assert. Please install the right extension.
...
-- Ondřej Mirtes

Oh, I'm sorry. I tried both in my project and this was just a ticket creation issue. I have corrected the original post (and also tested it locally again - it's not working).

I sent you a pull request that fixes your problem (robertfausk/phpstan-assert-instance-of#1). There were two issues (that I fixed in separate commits)

  1. includes does not belong as part of parameters but as its own section. (robertfausk/phpstan-assert-instance-of@1dc541e)
  2. You still referenced webmozart/assert extension and not the right one. (robertfausk/phpstan-assert-instance-of@b3d616d)

Alright thanks. 😀

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.