226 questions
3
votes
1
answer
91
views
PHPStan impure function that modifies a parent property
Given the code below, PHPStan reports an error that the comparison on line 36 always evaluates to true, and therefore thinks that the method always throws an exception. But if I run the code, no ...
2
votes
1
answer
61
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
107
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
103
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
200
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
82
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
98
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
142
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
308
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
103
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
318
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
1k
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
168
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
109
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
685
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 ...
1
vote
0
answers
142
views
phpstan: assign.readOnlyProperty
The following class
<?php
namespace App;
use Illuminate\Support\Str;
use stdClass;
class Payload
{
private(set) readonly array $validation;
private array $ids = [];
public ...
0
votes
1
answer
195
views
PHPStan Configuration Error in TYPO3 Extension: Alias Loader Already Registered
I’m encountering an issue while configuring PHPStan for a TYPO3 extension (TYPO3 v13.4.1, PHP 8.2, DDEV environment).
I have a custom extension and need to configure PHPStan to use rules from an ...
0
votes
1
answer
1k
views
Phpstan Call to an undefined method Illuminate\Database\Eloquent\Builder::join() [closed]
My PHP version is 7.1.31 and Laravel version is 4.2. I want to use phpstan as my static analysis tool.
When I run it, it outputs so many errors.
Examples are:
Call to an undefined method
Illuminate\...
0
votes
1
answer
128
views
PHP Stan error with return Union type in PHP v7, laravel
According to some conditions, I need to return int or Collection (the table data from mysql query or his count), Here is code what I did implemented.
public function myMethod(
Term $term,
...
0
votes
1
answer
571
views
PHPStan: Doctrine Repository @method tags with no value type specified in iterable type array
After upgrading to phpstan-doctrine version 1.5.2 it is required to do the following changes:
diff --git a/src/Repository/TagRepository.php b/src/Repository/TagRepository.php
index 89bc22a..6f65925 ...
2
votes
1
answer
102
views
PHPStan / @return all possible children that extend specific class
I have a problem with @return types. It seems that either it's a bug in PHPStan or I'm doing something wrong.
Error:
abstract class Aclass {}
final class Bclass extends Aclass {}
final class Cclass ...
1
vote
0
answers
181
views
Define dynamic array key with PHPStan
I have a function that looks like this
protected function getAllData(string $tableName, string $collectionKey): array
{
// Some logic here to get records from database and store them in $records......
1
vote
0
answers
46
views
CakePHP 4 JSON response gives phpstan error
According to the CakePHP 4 docs
// If you want a json response
$response = $response->withType('application/json')
->withStringBody(json_encode(['Foo' => 'bar']));
If you run phpstan on ...
3
votes
1
answer
664
views
@phpstan-ignore does not work as expected
I'm working on a Laravel project and there happens to be a code snippet like this:
sprintf("%s %s", __('foo.bar.buzz'), __('foo.bar.baz'));
Note that the __ function is a helper function ...
-1
votes
1
answer
1k
views
Issues with PHPStan Validation for Array Conversion to Integers
I'm new to using PHPStan/Larastan and configured it based on their documentation. When I run PHPStan at level 8, it passes without errors However, when I set PHPStan to the maximum level, it generates ...
0
votes
0
answers
98
views
How to Enforce Correct Method Chaining Style in PHP?
I'm looking to enforce a specific method chaining style in PHP, particularly to differentiate between what I consider the 'wrong' and 'good' styles. Is there a tool or method that can help check and ...
0
votes
2
answers
68
views
PHPStan - append lists from current and included config file
Let's say I have two configuration NEON files:
config1.neon
parameters:
excludePaths:
- path1
config2.neon
includes:
- "config1.neon"
parameters:
excludePaths:
- ...
0
votes
1
answer
2k
views
PHPStan cannot access property on mixed
I have a section of code as follows
$decodedCredentials = json_decode($credentials, false);
if (!$this->isValidCredentials($decodedCredentials)) {
return null;
}
$credential = new ...
0
votes
1
answer
116
views
phpstan with custom ruleset does not find all errors
I want to configure phpstan such that it reports use of undefined method calls
<?php
namespace App;
class Testeroni
{
public function asdf()
{
$this->iDoNotExist(); // Ok
...
0
votes
1
answer
289
views
How can i typehint a string as a method name on a class for static analysis with phpstan / phpstorm
I am currently upgrading a project to use phpstan to better catch type errors, @template and class-string have been particularly helpfull
However, I have a method on the projects router, that takes ...
0
votes
1
answer
991
views
Facing an issue with PHPstan
I am a novice in PHP and I am working on a legacy project where I am trying to upgrade PHP with Version 8.2 + ubuntu environment. The PHP code is written in ZEND Framework. The legacy code is written ...
0
votes
1
answer
279
views
cannot get rid of phpstan errors since switching to php attributes for OpenApi - has no value type specified in iterable type array
I am using swagger to create an OpenApi documentation.
I am switching now to php attributes and phpstan in level 6 complain about missing specific array declaration, which worked totally fine as long ...
2
votes
1
answer
212
views
can PHPStan detect that a @param can be an array of int, an array of string but not a mix of int and string?
readonly class Identifier extends Regexp
{
/**
* @param array<int,int>|array<int,string> $possibleValues
*/
public function __construct(array $possibleValues = [])
{
...
0
votes
1
answer
2k
views
PHPStan keeps saying "No value type specified in iterable type" but it is specified
When using php type declarations, PHPStan seems to think that I should specify the array members in both the PHPDoc as well as the actual declaration, but correct me if I'm wrong PHP (at least PHP 7) ...
1
vote
1
answer
161
views
How to write the return type of this method?
I am using laravel 11 + phpstan for the quality code.
In this method :
/**
* @param array<string> $request
* @return LengthAwarePaginator<App\Models\User>
*/
public ...
0
votes
1
answer
828
views
Why larastan raised error on relation method?
In laravel 11 with larastan/larastan 2.9 app I with request :
$managers = ModelHasPermission
::getByPermissionId(PERMISSION_APP_MANAGER)
->with('user') // ERROR POINTING THIS LINE 37
-&...
0
votes
0
answers
42
views
Throw php generic from generics trace
I have some method with generic:
/**
* @param class-string<T> $class
* @return T
* @throws ClientException
*
* @template T
*/
private function doSomething(string $content, string $class): ...
0
votes
2
answers
608
views
How to apply generics correctly to PHP?
Background
I do have classes (here: ClassA and ClassB) that shall be tested. As these classes are similar, I created an abstract test-class that implements an interface.
Problem
At level 7, PhpStan is ...
-1
votes
2
answers
2k
views
phpstan Unexpected item 'parameters › symfony' but extension installed
TL;DR: I see 'phpstan/extension-installer: Extensions installed' message but phpstan act like if it wasn't.
In an existing project, I want to add phpstan with symfony & doctrine extension.
When I ...
0
votes
0
answers
341
views
Error phpstan.neon: Bad indentation on line 3, column 5
I created a .neon file from the documentation.
https://phpstan.org/user-guide/discovering-symbols
I put the .neon file in the project root folder where I am running the phpstan command from. I added ...
1
vote
1
answer
117
views
How to return a generic interface in PHP / PHPStan?
I'm using generics to type hint some classes and interfaces in php/phpstan. What i want to achieve is that a function returns a an interface object but with generic hinting:
class Car
{
}
/**
* @...
-2
votes
1
answer
765
views
Can I run the PHPStan setup script manually?
I am using the devspaces tool as part of a project with a few dozen developers. It's great, but in order to make certain things work locally in my IDE, I still run Composer on my local machine ...
0
votes
2
answers
335
views
Type hinting a property with var annotation
In Symfony, if you want to autowire some dependencies in a service, many times you use an interface instead of a concrete class. For example, you might have:
public function __construct(
...
0
votes
1
answer
626
views
phpstan undefined (magic?) method
In CakePHP; the ORM supports an SQL function abstraction layer; so you can use functions like "SUM" and "MAX". These functions are defined in the FunctionBuilder. Cake also lets ...
0
votes
1
answer
493
views
Phpstan: how to set array key types?
Is there a way to document an array, where every key is a string and every item is an object of specific types? I tried this
@param array{string, MyObj} $accounts
but apparently Phpstan thinks of it ...
1
vote
3
answers
2k
views
How to tell PHPStan about method that exists in class implementation but not in it's interface?
I have two interfaces (ClienInterface, ClientFactoryInterface) and two classes implementing them (ConcreteClient, ConcreteApiClientFactory). ConcreteClient has method not definded in ClienInterface.
...
0
votes
2
answers
308
views
Custom PHPStan rul lto allow calling class method only from specific class
I'm trying to write a custom phpstan rule to not allow other devs to call Cart::save() ($cart->save()) from everywhere in the codebase, but to call only from CartRepository, and if it's called ...
0
votes
1
answer
641
views
phpstan - return a generic
I feel stupid asking such a simple question, but I am clearly missing something basic.
The CollectionOrdered class below is meant to "carry a payload" and the point of HasPayloadInterface is ...
0
votes
1
answer
566
views
Larastan doesn't resolve aliase of facades
I'm using larastan on a Laravel project.
When I use a facade like \Auth::check() like code below, larastan can't resolve the return type of this method.
function index()
{
if(\Auth::check()) { // as ...