PHPstan is an open-source static analyzer for PHP.
Questions tagged [phpstan]
150 questions
0
votes
1 answer
PHPSTAN: Laravel 9 mutators producing "Access to undefined property" error
I am trying to store a key into my database and I want it to be encrypted and decrypted.
So I use Laravel 9 mutator: https://laravel.com/docs/9.x/eloquent-mutators
protected function privateKey(): Attribute
{
return Attribute::make(
get:…

nwantiti
- 41
- 4
0
votes
0 answers
PHPStan does take into account submethod tests
I got the following problem with PHPStan :

ERO
- 55
- 4
0
votes
1 answer
PHPStan & WordPress Plugin Boilerplate - Parameter #2 $callback of function add_filter expects callable(): mixed, array{mixed, mixed} given?
I started using PHPStan with the WordPress Plugin Boilerplate project that can be found here.
I am stuck in the class-plugin-name-loader.php file validation
I solved these errors:
33 - Property Plugin_Name_Loader::$actions type has no value type…

MasterRed
- 83
- 1
- 7
0
votes
1 answer
return object models in array with PHPStan
Lets say you have 3 models like this
class User extends AbstractModel {
protected string $name = 'User';
}
class Car extends AbstractModel {
protected int $weels = 4;
}
class House extends AbstractModel {
protected string $address…

HashtagForgotName
- 651
- 1
- 7
- 23
0
votes
1 answer
How to make PHPStan analyze only the fresh lines of code from files
I have installed PHPStan in a project developed around 1.5 years ago. The files of project have both old and fresh code. I want PHPStan to analyze only the fresh code written from today onwards and ignore the old code written in any file. How this…

John Smith
- 1
- 1
0
votes
1 answer
phpstan - Validate an array which can be mixed
I am blocked with some phpstan validation, I have this array:
/** @var array> $normalizedImage */
$normalizedImage = $this->normalizer->normalize($data);
$normalizedImage['@id'] =…

Vincent Decaux
- 9,857
- 6
- 56
- 84
0
votes
0 answers
PHP8.2 Refactoring: How do I refactor dynamic parameters in PHP 8.2 since they are deprecated?
I have used an interface and a trait to fulfill the interface.
interface Attachable {
public function getAttachment();
public function setAttachment($attachment);
}
trait HasAttachment {
public $attachment = null;
public function…

Aashan Ghimire
- 5
- 1
- 4
0
votes
0 answers
Parameter #1 $object of method expects x given y - abstract entities
I have a abstract class User:
abstract class User
and a child of User, "Administrator":
class Administrator extends User
When i create a new Administrator and want to persist with this signature:
public function persist(Administrator $object):…

Thomas Baier
- 434
- 3
- 8
0
votes
1 answer
Why PHPStan can't find the class installation by pear?
PHPStan can't find the classes I installed with pear.
Why? How can I fix it?
Steps
Run: pear install HTTP_Request2-2.5.1
Check: pear config-get php_dir
/usr/share/pear
Check: php -i | grep include_path
include_path =>…

YujiSoftware
- 1,281
- 14
- 15
0
votes
1 answer
phpstan:: Method (methodname) return type with generic interface (interfacename) does not specify its types
I have defined an interface for an ordered list. The class docblock looks like this:
/**
* Interface ListOrderedInterface
* @template ListOrderedElement
*/
Within the method docblocks for this interface, ListOrderedElement is used to ensure…

Doug Wilbourne
- 125
- 7
0
votes
0 answers
PHPStan "Call to an undefined method" errors with symfony/phpunit-bridge
PHPStan analysis combined with symfony/phpunit-bridge raises Call to an undefined method errors.
Is it possible to ignore these errors with simple PHPStan configuration?

David
- 2,603
- 4
- 18
- 28
0
votes
0 answers
Phpstan is not recognizing my abstract class
I have a request with an authorize method that makes use of:
Illuminate\Http\Request::user()
And it gives me a phpstan error that says
phpstan: Cannot call method hasAnyRole() on App\Models\User|null.
And that makes sense.
So I made an abstract…

Collin Ter Steege
- 49
- 2
0
votes
1 answer
PHPStan reports possibly undefined variable, but it's defined in included script
I have an application where I define some variables in a file (a.php), then include this from another file (b.php). PHPStan is complaining about possibly undefined variables.
Simplified example:
a.php:
$config['foo'] = 'bar';
b.php:

Grodriguez
- 21,501
- 10
- 63
- 107
0
votes
1 answer
PHPStan: Bound types in different namespaces
I've just started using PhpStan. I want to bind a specific base interface to a template variable, but the namespace is ignored. Whatever I try, PhpStand looks in namespace A\...
namespace A;
use B\FooInterface;
/**
* @template T of FooInterface
…

S. Parton
- 73
- 1
- 7
0
votes
2 answers
How to disable warning for specific exceptions?
I do get a warning whenever I throw an exception in my code without explicitly stating it in the docblocs:
I know this can be fixed by adding @throw tags, but the warning is actually not 100% true, as I do handle the exception in…

Adam
- 25,960
- 22
- 158
- 247