PHPstan is an open-source static analyzer for PHP.
Questions tagged [phpstan]
150 questions
17
votes
2 answers
PHPStan: Property with generic class does not specify its types: TKey, T
I'm running PHPStan on a Symfony project where I have the following relation in a Doctrine entity:
/**
* @ORM\OneToMany(targetEntity="App\Entity\Course\Slide", mappedBy="chapter", cascade={"persist"}, orphanRemoval=true)
*…

AymDev
- 6,626
- 4
- 29
- 52
8
votes
2 answers
phpstan complains about Doctrine Migrations in Symfony 3.4 with Flex project
I have a project built on Symfony 3.4 with Flex and I've added phpstan to it for static analysis.
It is complaining about not finding my migration classes:
Class DoctrineMigrations\Version20180831185050 was not found while trying to analyse it -…

Radu C
- 1,340
- 13
- 31
8
votes
1 answer
PHPStan, exclude all and specify files to check
Im trying to setup PHPStan on a older, bigger, codebase. How could i exclude everything and than maybe by config define what to analyse.
I have thought about using a separate folder for it, but that would mean constantly moving files which might…

Michal
- 1,010
- 2
- 8
- 18
7
votes
3 answers
PHPStan and Doctrine: $id is never written, only read
I am using PHP8, symfony5 and doctrine2 with phpstan and getting these errors:
Property App\Entity\User::$id is never written, only read.
The code:

ivoba
- 5,780
- 5
- 48
- 55
6
votes
1 answer
How to configure PHPStan to avoid false positives caused by specific classes and/or magic methods?
While using PHP RedBean on a project, PHPStan reports problems like this one:
87 Access to an undefined property RedBeanPHP\OODBBean::$token.
This is happening because RedBean's OODBBean class uses magic methods to logically bind class…

Diogo Melo
- 1,735
- 3
- 20
- 29
6
votes
2 answers
Undefined variable in PHPStan but it's declared elsewhere
How do you deal with these scenarios in PHPStan:
Say you're using plain PHP as template engine. E.g.,
// view.php
=$foo?>
Say you have 2 files a.php and b.php
// a.php
$foo = 'bar';
// b.php
require 'a.php';
echo…

IMB
- 15,163
- 19
- 82
- 140
5
votes
1 answer
How can I avoid repetition of array shape declaration for method return value?
If, for example, I have a class like this:
class Foo
{
/**
* @var array
*/
private array $things;
/**
* @return array
*/
public function getThings(): array
{
return…

Mark G
- 85
- 1
- 5
5
votes
1 answer
Using Enum as string in PHP with PhpStan
I'm experimenting with PHP enums in a Symfony 6 application and I thought I found a very nice use case for those. Everything works, but phpstan keeps complaining about the type I return.
------…

Julien B.
- 3,023
- 2
- 18
- 33
5
votes
1 answer
How can I have phpstan find my custom extension class?
To implement checks for some magic _get() functions, I'm trying to implement a "class reflection extension". The manual mentions how to write the extension files themselves, and to install them by adding a service part to the neon file.
I've written…

Nanne
- 64,065
- 16
- 119
- 163
4
votes
5 answers
Larastan complains about collection methods paramaters after upgrading to Laravel 10
After upgrading to Laravel 10 I'm having hard times with Larastan errors.
The following code which was perfectly fine until 1 hour ago:
return $this->articleRepository->getDefaultArticles($organizationId)
->toBase()
->map(function (Article…

Fabio Sasso
- 145
- 1
- 12
4
votes
1 answer
PHPStan is not interpreting a Symfony EntityRepository as a generic
I'm having trouble making sense of this PHPStan error. PHPStan is saying I need to provide the class string of an EntityRepository of an object. I am providing the class string of a ServiceEntityRepository (which extends EntityRepository) of class…

amacrobert
- 2,707
- 2
- 28
- 37
4
votes
1 answer
PHPStan doesn't use custom entity repository
I am using PHPStan with its Doctrine extension.
I have a custom entity repository called App\Repository\Doctrine\UserRepository with the @extends doc block:
/**
* @extends \Doctrine\ORM\EntityRepository<\App\Entity\User>
*/
class UserRepository…

amacrobert
- 2,707
- 2
- 28
- 37
4
votes
2 answers
PHP 7.4+ class property typing
I'm sure that question has been asked numerous times but I can't seem to find a good/satisfying answer so please bare with me.
Using PHP 7.4+, I tend to type everything I can. But I have some problems with Doctrine entities properties.
If I type…

Julien B.
- 3,023
- 2
- 18
- 33
4
votes
1 answer
PHPStan throws undefined static method when using custom rules in Respect/Validation
I'm using Respect/Validation class and I have custom rule CustomRule() which works fine:
use Respect\Validation\Validator as v;
// ...
'email' => v::CustomRule()->email()->setName('email');
But this causes PHPStan to throw an error:
Call to an…

IMB
- 15,163
- 19
- 82
- 140
3
votes
0 answers
How to pass this rule with larastan?
I am using Laravel 10 + larastan to inspect my code, at level 9 (maximum).
I have this error :
Access to an undefined property App\Models\User::$full_name.
The code is :
$model->created_by = Auth::user() ? Auth::user()->full_name : '?';
And the…

Dom
- 2,984
- 3
- 34
- 64