0
0

More than 3 years have passed since last update.

PHPStan の trait にある phpdoc type hint が効かない場合はキャッシュを削除する

Posted at

phpstan を使っていて、trait に書いたタイプヒントが効かない。

composer.json
{
    "name": "local/phpstan-test",
    "autoload": {
        "psr-4": {
            "": "src/"
        }
    },
    "require-dev": {
        "php": ">=7.2",
        "phpstan/phpstan": "^0.12"
    }
}
composer install
src/FooTrait.php
<?php

trait FooTrait
{
    /**
     * @param mixed $data
     */
    public function traitMethod($data): int
    {
        return 0;
    }
}
src/Foo.php
<?php

class Foo
{
    use FooTrait;

    public function run(): int
    {
        return $this->traitMethod("foo");
    }
}
vendor/bin/phpstan analyze -l8 src/
 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



 [OK] No errors                                                                 

初回はOK。
ここでコメントを追加する。

src/FooTrait.php
<?php

trait FooTrait
{
    /**
     * @param mixed $data
     * @return int
     */
    public function traitMethod($data): int
    {
        return 0;
    }
}
php7.2 vendor/bin/phpstan analyze -l8 src/

 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ---------------------------------------------------------------- 
  Line   FooTrait.php (in context of class Foo)                          
 ------ ---------------------------------------------------------------- 
  9      Method Foo::traitMethod() has parameter $data with no typehint  
         specified.                                                      
 ------ ---------------------------------------------------------------- 


 [ERROR] Found 1 error                                                          

そうするとエラーになる。
@param $data はそのままなのにエラーが出るのでハマった。

rm -rf /tmp/phpstan
vendor/bin/phpstan analyze -l8 src/
 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



 [OK] No errors                                                                 

キャッシュを消すとOKになる。

https://github.com/phpstan/phpstan/issues/1601
https://github.com/phpstan/phpstan/issues/1294#issuecomment-436951553

issue解消待ち。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0