225 questions
0
votes
1
answer
2k
views
PhpStorm with PHPStan in Docker doesn't work
When using the System PHP interpreter for PHPStan, I get an error that the PHP version mismatches with the one being used in the Docker, see picture.
But when I set PHPStan to use the PHP interpreter ...
2
votes
0
answers
1k
views
PSALM / PHPStan / PHPDoc Types: using templates/generics as array keys (of multiple elements array)
I cannot figure how to annotate an array returned from API Response.
Response have a structure:
array{'totalCount': numeric-string, $vehicleType: array}
Instead of variable $vehicleType there can be ...
0
votes
1
answer
730
views
denoting optional parameters via annotations for phpstan
I have a class that has a __call method. The first parameter to the __call method, per the PHP docs, is the method name. If the method name is unsupported I throw an exception.
The problem with this ...
3
votes
2
answers
5k
views
How to ignore "access to an undefined property" using PHPStan and Laravel
I am using Laravel 10 + PHPStan.
When I use Resource with the Laravel code, then PHPStan sends me this error:
18 Access to an undefined property App\Http\Resources\LocationResource::$id.
19 ...
0
votes
1
answer
291
views
PHPstan does not like preg_match parameters
PHPstan gives this error
Parameter #4 $flags of function preg_match expects TFlags of 0|256|512|768, int given.
to this code
function my_preg_match(
string $pattern,
string $...
10
votes
1
answer
6k
views
Fix Laravel scope warnings of phpstan (or larastan)?
How to deal with scopes in Laravel when analyzing code with phpstan/larastan?
I'm getting this error:
Call to an undefined method Illuminate\Database\Eloquent\Builder::active().
Do I need to typehint ...
1
vote
1
answer
2k
views
PhpStorm configuration of PHPstan for Symfony with Docker
I am trying to make PHPstan work for a dockerized Symfony project using this template. The project use PHP 8.1.16. Works fine.
Now I want to push it a little bit to fully use the PHP CLI installed in ...
3
votes
2
answers
4k
views
Describe an array containing an object (stdClass) with @phpstan-return in PHP DocBlock? - PHPStan
I have an example function in PHP (8.2) that returns an array. Inside this array there is also an object, more precisely stdClass.
I use PHPStan as a static analyzer tool. How can I specify this (...
0
votes
1
answer
1k
views
why is phpstan reporting a lot more errors on exact same src on macos vs ubuntu?
I've forked CodeIgniter 3 to work on PHP 8.2 compatibility. I'm using PHPStan for static analysis to try and identify all the referencees to undefined class properties, which seems to be the primary ...
5
votes
2
answers
9k
views
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.
...
1
vote
1
answer
2k
views
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 ...
0
votes
1
answer
1k
views
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(): ...
0
votes
1
answer
706
views
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 ...
0
votes
1
answer
942
views
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 ...
0
votes
1
answer
2k
views
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 ...
1
vote
3
answers
987
views
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 ...
-1
votes
1
answer
2k
views
phpstan - Validate an array which can be mixed
I am blocked with some phpstan validation, I have this array:
/** @var array<string, string|array<string>> $normalizedImage */
$normalizedImage = $this->normalizer->normalize($...
5
votes
1
answer
2k
views
PHPStan cast to more specific array shape
In my code, I call a method whose return value is typed as follows:
/**
* @return array<int, array<string,mixed>>
*/
public function fetchAllAssociative(): array;
The subtype array<...
0
votes
0
answers
220
views
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 {
...
0
votes
0
answers
128
views
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 ...
0
votes
1
answer
728
views
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 ...
1
vote
1
answer
3k
views
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 ...
1
vote
0
answers
598
views
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?
1
vote
1
answer
2k
views
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:...
0
votes
1
answer
241
views
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\...
0
votes
2
answers
801
views
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, ...
0
votes
1
answer
697
views
Use different PHP version in VSCode workspace PHPStan
I would like to use a different PHP version than my default in my current VScode workspace for PHPstan.
PHPstan keeps using my default PHP version from my windows commandline.
The error I get is;
PHP ...
1
vote
1
answer
3k
views
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 ...
0
votes
1
answer
3k
views
PHPstan: Cannot access property $work on array|EntityInterface|null
I am developing a web app, trying to keep PHPstan's suggestions in check.
I am having some difficulties with this method:
/**
* AJAX: deletes a work file
*
* @return \Cake\Http\Response|false
*/
...
2
votes
0
answers
930
views
How to open PHPstan file URL in PhpStorm's terminal?
Current situation: I'm using PhpStorm and PHPstan. I want to start analysis via PhpStorm and want the output in PhpStorm's terminal. So far so good, that's not a problem.
Now I want to use PHPstan's ...
8
votes
1
answer
2k
views
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<string, array{name: string, age: int}>
*/
private array $things;
/**
* @return array
*/
...
0
votes
1
answer
2k
views
PHPStan misses interface implementation when inferring return type
I recently started playing with PHPStan, and encountered a roadblock that I just cannot get past. Some visual aid might make this easier for me to explain. Class names are simplified to keep things ...
1
vote
1
answer
2k
views
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? $...
2
votes
1
answer
1k
views
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 ...
0
votes
1
answer
1k
views
Psalm and PHPStan unable to read Symfony entity string:class
I'm implementing PHPStan into my application and also testing Psalm for automatic fixes, however I'm unable to make them read entity class strings (with colon) fe:
$repository = $this->em->...
1
vote
1
answer
814
views
Symfony Authenticator error inside PHPStan
I have the following problem. When I run analyse on my src folder, I get this error
------ -------------------------------------------------------------------------------------------------------------...
0
votes
1
answer
4k
views
PHPStan Symfony: no value type specified in iterable type array
I wanted to test my little symfony project today with PHPStan, and I got this error when I want to test User Entity (Entity/User.php), which I don't have clue what does it mean:
89 Method App\...
0
votes
0
answers
758
views
PHPStan inheritance & property types
I have trouble passing PHPStan tests because of parent/child classes (besides, code runs well).
These two kind of errors always show up:
Parameter #1 $a of method xxx expects ConcreteChildClass, ...
0
votes
0
answers
1k
views
How to understand this phpStan error (has parameter $astTreeData with no value type specified in iterable type array.)
Say I have a formatter function like this:
function prepareValue(mixed $value): string
{
...
return "{$value}";
}
/**
* @param array<int, array> $astTreeData
* @return ...
0
votes
1
answer
556
views
phpstan and psalm ail because of an unidentifiable problem
For a php project, for some time my tests fail for phpstan and psalm, with unclear error messages.
I tried to debug, to rebuild the commits, but without success. I can't understand or so.
These are ...
-1
votes
1
answer
2k
views
Configure phpstan with symfony
I am getting a lot of errors related to phpunit like
Class MyTest extends unknown class PHPUnit\Framework\TestCase
or
Call to an undefined method MyTest::assertFalse().
I am on Symfony 6 and use ...
0
votes
1
answer
1k
views
Laravel 9 phpstan
I'm getting this error:
Cannot call method getClientOriginalExtension() on array<int, Illuminate\Http\UploadedFile>|Illuminate\Http\UploadedFile|null.
when I run phpstan
this's code:
$...
-2
votes
1
answer
3k
views
laravel 9 executting php phpstan
I'm running
php ./vendor/bin/phpstan analyse --error-format github
I get this issues:
::error file=app/Models/User.php,line=55,col=0::Method App\Models\User::roles() return type with generic class ...
1
vote
1
answer
1k
views
Doctrine PHPStan entity relation Collection never written, only read
I am attempting to use the correct typings according to Doctrine and PHPStan, however with an entity relation, I can't seem to make it right.
I am using PHP 8.1.6, Doctrine 2.6.3, PHPStan 1.7.3 and ...
1
vote
0
answers
1k
views
PHPStan with array shape doc
I have an array like below:
array [
"total" => 0,
"seed" => 0,
"stringKey1" => array<string, string>,
"stringKey2" => array<...
1
vote
0
answers
700
views
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 ...
0
votes
1
answer
1k
views
PHPStan Extension: Dynamic Return Types and a Variadic Function Parameter
Since PHPStan 1.6, it's possible to use Conditional Return Types, where I've been able to do things like:
/**
* @param string $x
* @return ($x is literal-string ? literal-string : string)
*/
public ...
0
votes
0
answers
528
views
CakePHP Entity vs EntityInterface
Because PHPStan is driving me nuts about this, I have to ask and hope someone has an answer: when querying the database, the result set is a list of EntityInterface objects. Then when I attempt to ...
-1
votes
1
answer
851
views
Phpstan fails with "Syntax error, unexpected T_STRING, expecting ';' " in definition of function
I am using Phpstan on a Drupal project that needs to be updated; I have solved all the issues revealed by Phpstan except one and I don´t know the solution to that one.
The error message is:
Line ...
4
votes
1
answer
2k
views
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 ...