225 questions
2
votes
1
answer
46
views
PHPStan complains about @var Builder<ProductTemplate> in Laravel Eloquent scope
I'm trying to define a Laravel Eloquent model with a custom scope that filters categories by a related product's vendor_id. Here's my setup:
class Category extends Model
{
public function ...
3
votes
1
answer
101
views
PHP Rector + PHPStan conflict in rules
I migrated project from PHP 7.4 to 8.3.
I have left with 1 error in rector that I am not able to solve.
$namesArray = explode($separator, $names);
$namesArray = explode($separator, (string) $...
2
votes
1
answer
102
views
How to check if a class has child classes without loading the whole project?
I'm writing a custom PHPStan rule that suggests marking a class as readonly (introduced in PHP 8.2) when it meets all the necessary conditions.
One of the requirements for safely applying readonly is ...
1
vote
1
answer
191
views
Why isn't PHPStan complaining about invalid array keys?
Here's the code:
<?php declare(strict_types=1);
/**
* @param array{key?: string} $options
*/
function hello($options)
{
var_dump($options);
}
hello([
'WRONG_KEY' => '...',
]);
I ...
0
votes
2
answers
78
views
PHPstan making bad assumption about array key
PHPstan seems to incorrectly narrow type after if (empty(...)).
\PHPStan\dumpType($_GET); // array<mixed>
if (empty($_GET['ssid'])) { /* do nothing */ }
\PHPStan\dumpType($_GET); // non-...
0
votes
2
answers
91
views
Insufficient Property Type in StdClass
I've got various instances of code, building options for a dropdown dynamically with a preceding empty field:
return State::all()
->map(fn (State $state) => (object) [
...
1
vote
1
answer
129
views
Overriding PhpDocs Argument of a Method in a Parent Class
My question to get code completion working in a set of classes that I have.
+ abstract Model :: make(array{...})
+- SubModel
+- AnotherSubModel
+- ...
I have an abstract Model class, and ...
0
votes
1
answer
300
views
phpstan generics and factory return type
I do not understand why phpstan (at level 9+) flags the concrete factory's makeCollection method (the last class) as returning Collection.
<?php
declare (strict_types=1);
/**
* @template ...
1
vote
1
answer
102
views
How to describe anonymous classes and methods?
I need to run tests on a certain class, but adding a method that was not originally present.
Previously this need could be satisfied using MockBuilder::addMethods(), but this method has been ...
0
votes
1
answer
306
views
Carbon macro not validated by PHPStan
I'm registering a custom Carbon macro in my Laravel's AppServiceProvider
Carbon::macro('itFormat', static function () {
return ucwords(self::this()->translatedFormat('l d/m/Y'));
});
But ...
1
vote
1
answer
967
views
Specifying keys in array shape in PHPstan
I want to annotate function, tat takes array with at least key "a" and returns the same array shape with newly added key "x". I tried using type intersection like this:
/**
* @...
0
votes
1
answer
101
views
Writing a less brittle test for a custom PHPStan rule
I wrote a custom PHPStan rule, and a test for it. The test analyses a sample PHP class:
class CustomRuleTestData
{
public function hasViolations() {
//
}
public function ...
2
votes
0
answers
158
views
PHPStan type for json-like structure
Json-like structures are quite common in php applications that deal some sort of json api. The precise type of such a structure is recursive:
/**
* @return null|scalar|array< null|scalar|array< ...
1
vote
0
answers
105
views
PHPStan not recognizing type specification for generic type
PHPStan is not recognizing my type specification in my docblock preceeding a method.
Here's the code:
/**
* Process (resize and save) all versions of an uploaded file
*
* @param UploadedFileInterface ...
2
votes
1
answer
676
views
Why phpstan returns an error for a basic view?
I am using larastan 3.0 with laravel. The level is 9.
In this code :
return view('auth.change-password', [
'user' => $user,
]);
I have this error :
Parameter #1 $view of function view ...