【问题标题】:Correct type hint error thrown by PHPStan on UserInterface in Symfony project纠正 PHPStan 在 Symfony 项目的 UserInterface 上抛出的类型提示错误
【发布时间】:2020-08-12 22:33:13
【问题描述】:

我最近开始在 Symfony 3.4 项目中使用 PHPStan(版本 0.12.19),但我遇到了一个错误,看起来应该很简单解决,但我正在努力弄清楚。

目前在第 7 级运行。这是我运行时遇到的错误:

vendor/bin/phpstan analyse

------ --------------------------------------------------------------------------------------------------------------
  Line   src/AppBundle/Controller/MapController.php
 ------ --------------------------------------------------------------------------------------------------------------
  94     Parameter #1 $user of static method AppBundle\Entity\MapMarker::createMapMarker() expects
         Symfony\Component\Security\Core\User\UserInterface, object given.

这是MapController.php的重要部分:

    $user = $this->getUser();

     $mapMarker = MapMarker::createMapMarker(
            $user,
            $latitude,
            $longitude
        );

getUser 方法是 Symfony 的,所以我无法更改这部分:https://github.com/symfony/symfony/blob/3.4/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php#L444:

/**
     * Get a user from the Security Token Storage.
     *
     * @return UserInterface|object|null
     *
     * @throws \LogicException If SecurityBundle is not available
     *
     * @see TokenInterface::getUser()
     *
     * @final since version 3.4
     */
    protected function getUser()
    {
        if (!$this->container->has('security.token_storage')) {
            throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
        }

        if (null === $token = $this->container->get('security.token_storage')->getToken()) {
            return null;
        }

        if (!\is_object($user = $token->getUser())) {
            // e.g. anonymous authentication
            return null;
        }

        return $user;
    }

以及MapMarker.php的重要部分:

/**
     * @param UserInterface $user
     * @param double $latitude
     * @param double $longitude
     */
    private function __construct(UserInterface $user, $latitude, $longitude) {
        $this->createdBy = $user;
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }

    /**
     * @param UserInterface $user
     * @param double $latitude
     * @param double $longitude
     * @return MapMarker
     */
    public static function createMapMarker(UserInterface $user, $latitude, $longitude): MapMarker
    {
        return new self($user, $latitude, $longitude);
    }

当我转储 $user instanceof UserInterface 时,它返回 true - 据我所知,它正在传递一个 UserInterface 对象,而不仅仅是 PHPStan 所指示的“对象”。

最后,这是我的 phpstan.neon 配置文件:

parameters:
    level: 7
    paths:
        - src
        - tests
    checkGenericClassInNonGenericObjectType: false
    checkMissingIterableValueType: false

我错过了什么?

【问题讨论】:

    标签: php symfony symfony-3.4 type-hinting phpstan


    【解决方案1】:

    我想你至少知道了问题所在:getUser 方法的返回类型是@return UserInterface|object|null,而createMapMarker 需要一个UserInterface 的实例。

    在调用createMapMarker 之前,您应该检查getUser 的返回值实际上是UserInterface 的一个实例,并且可能是一个简单的/** @var UserInterface $user */ 就在$user 变量声明的顶部将修复。

    【讨论】:

    • 哇..为什么我没想到我永远不会知道!大脑冻结:) 谢谢!
    猜你喜欢
    • 2018-07-27
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2022-07-18
    相关资源
    最近更新 更多