Questions tagged [phpstan]

PHPstan is an open-source static analyzer for PHP.

150 questions
1
vote
1 answer

php error Call to an undefined static method with PHPStan

I have phpstan in my proyect to analyze all my code. I´m working with laravel and php 8.2. My problem it´s that when i execute phpStan return any errors and i have one in my function that i´m calling a scope function. this call…
1
vote
1 answer

Yii2 + phpstan?

I have a project (PHP 8.2.6, Yii2, PHPStan). After running phpstan command, I got a list of errors that should be fixed for successful checking. Example of errors: Access to an undefined property yii\base\Application::$request. Access to an…
Fitstd
  • 189
  • 2
  • 9
1
vote
1 answer

PHPstan Cannot access property $children on App\Account|null

when I run PHPstan at level 8, I get for example with this code: /** * @return Collection */ public function getCustomersAttribute(): Collection { return $this->account->children; } I get the following…
1
vote
1 answer

How to tell PHP static code analysers to read the generic type hint from a callable, instead of expecting the class name as a string?

I'm trying to remove some code duplication that has proven to be prone to human errors. I created a working sample code at https://3v4l.org/QFA6m#v8.2.7 and a demo of PHPStan failing where expected,…
Jan Klan
  • 667
  • 6
  • 16
1
vote
2 answers

phpstan: class implements generic interface but does not specify its types error

Background I am building a class for a laravel system. It is for casting the Ramsey\Uuid\Uuid type in laravel models. I also use phpstan and I seem to be having problems with the generics/templating. in the DB, uuid is stored as binary when loading…
elb98rm
  • 670
  • 6
  • 21
1
vote
0 answers

Check code compatibility for different PHP versions

I am searching for a way to check my PHP code for compatibility with different PHP versions. Especially compatibility to PHP version 7.2 - 8.1 is what I am looking for. At first I tried phpcs but the results were not really good, so I tried…
maxhb
  • 8,554
  • 9
  • 29
  • 53
1
vote
3 answers

phpstan not using outcome of function

I have a function that checks if a value is null and after I call that function I use the value that is obviously not null. but phpstan still says it could be null. Do you guys have any solution for phpstan to know the outcome of this…
1
vote
1 answer

How to tell PHPStan that $entry contains an object with a dynamic type (Entity name from a variable)?

I'm running PHPStan in my project on level 9 and I'm closing in on zero errors now. One problem I can't find a solution for though is in the CRUD part of my project. With >50 complex entities that require permissions, have relations etc. of course I…
JamesApril
  • 11
  • 2
1
vote
1 answer

Is their a way of passing along typehint to callable in phpstan/intellisense? PHP

I have a factory method whose signature is similar to this: /** * @param class-string $class * @param callable? $callback * @return static */ public function instance(string $class, callable? $callback); From the first parameter, an instance…
Mark G
  • 85
  • 1
  • 5
1
vote
1 answer

PHPStan: Parameter #1 $length of function random_bytes expects int<1, max>, int given

I'm getting a PHPStan error that I don't understand how to fix: Parameter #1 $length of function random_bytes expects int<1, max>, int given. The function is very simple: private function generateEncryptionKey(int $bytes = 16): string { …
Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
1
vote
0 answers

PHPStan with array shape doc

I have an array like below: array [ "total" => 0, "seed" => 0, "stringKey1" => array, "stringKey2" => array, ... "stringKeyN" => array ] I have question about how to use…
anru
  • 1,335
  • 3
  • 16
  • 31
1
vote
0 answers

How to make PHPStan understand exception handling for child methods?

TL;DR My questions being : Is it a good practice to have exceptions inside an overriden methods ? If so, how should I document and handle exceptions so that PHPStan doesn't block my pipelines ? If not, how is the best way(s) to handle this kind of…
Joey Peau
  • 51
  • 4
1
vote
1 answer

Why PHPStan dectects these errors?

I use LARAVEL 9 and PHPSTAN I have this simple test method : public function createAndAuthenticatedAnUser(string $status = 'VALIDATED', bool $isAdmin = false): User { $user = User::factory()->create([ 'status' => $status, …
Dom
  • 2,984
  • 3
  • 34
  • 64
1
vote
1 answer

PHP, Static Analysis, and Recursive Type Checking

I'm looking at a Database ORM that uses an array to define the WHERE clause, e.g. $articles->find('all', [ 'OR' => [ 'category_id IS NULL', 'category_id' => $id, ], ]); The "array keys" become part of the SQL, so they must be…
Craig Francis
  • 1,855
  • 3
  • 22
  • 35
1
vote
1 answer

PHPStan : howto get child type returned, not parent

I spent time reading PHPStan documentation and trying, but I don't understand how to get the child class detected by PHPStan in this code, or even if it is possible...
Romaric
  • 11
  • 4
1 2
3
9 10