225 questions
1
vote
1
answer
2k
views
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()->...
0
votes
1
answer
3k
views
How to declare typed Collection inside Symfony Entity in PHP 8.1
I receive an error from PHPStan:
Property App\Entity\Product::$productArticles type mapping mismatch: property can contain Doctrine\Common\Collections\Collection but database
expects ...
1
vote
0
answers
3k
views
PHPStan error: @return type is incompatible with native type (generics and static)
I'm struggling to understand the cause of PHPStan error on method FooArray::this_doesnt_work.
PHPDoc tag @return with type FooArray<int|string, TValueIn> is incompatible with native type static(...
2
votes
1
answer
871
views
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,
],
]);
...
-2
votes
1
answer
79
views
How to catch php spelling errors with static analyzers?
I was made a mistake yesterday and spent hours to fix it. I have method like this
{
if (isset($data['y'])) {
$this->y = $data['y'];
}
if (isset($data['z'])) {
$this->...
1
vote
1
answer
660
views
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...
<?php declare(strict_types = ...
4
votes
2
answers
3k
views
PHPStan doesn't use custom entity repository
I am using PHPStan with its Doctrine extension.
I have a custom entity repository called App\Repository\Doctrine\UserRepository with the @extends doc block:
/**
* @extends \Doctrine\ORM\...
3
votes
1
answer
5k
views
Make PHPStan understand Laravel Eloquent Builder query()
I am having a hard time making larastan / phpstan understand that query() should be based on Company model and not Eloquent\Model. What am I missing?
<?php
namespace App\Repositories;
use App\...
5
votes
1
answer
5k
views
Using Enum as string in PHP with PhpStan
I'm experimenting with PHP enums in a Symfony 6 application and I thought I found a very nice use case for those. Everything works, but phpstan keeps complaining about the type I return.
------ ------...
-1
votes
1
answer
264
views
What is the return type of a method that returns Heredoc?
I have some PHP code that looks like this:
class MyClass
{
public static function classPhpdoc($paramA)
{
return <<<PHP
/** @template-extends \\$baseClass */
PHP;
}
}
It'...
1
vote
1
answer
3k
views
Handle a phpstan error level 9 about array
I've created a function in my repository :
/**
* @return array<int, array<int, string>>
*/
public function findByBuildingGroupByCompany(string $buildingId): array
{
...
0
votes
1
answer
278
views
The annotation "@apiDefine" in class App\Controller\UserController was never imported
I am using both phpstan and apidoc, but i have an error on phpstan that i can't find a way to solve or ignore.
Error message "Internal error: [Semantical Error] The annotation "@apiDefine&...
0
votes
1
answer
84
views
How would I translate this weird PHP line to something less compact?
I have this code, part of a function I use to determine if a given string is a reserved PHP keyword (since PHP has no function for this built in):
$tokens = token_get_all('<?php ' . $string . '; ?&...
3
votes
1
answer
3k
views
defining generic return for a method/function in php with phpdocblock
Is there a way to specify that a function will return an object of a specific type, where the type is the string of one of the parameters?
e.g.
/**
* @return object<$class>
*/
public function ...
0
votes
2
answers
2k
views
Typehint a property to a more specific class than the interface returned by the function used to initialize it
I’m trying to up my phpstan setup to level 3, but I’m getting an error like this:
Property Something::$repository (SpecificRepository) does not accept RepositoryInterface.
On a class that looks like ...
0
votes
1
answer
718
views
Phpstan doctrine database connection error
I'm using doctrine in a project (not symfony). In this project I also use phpstan, i installed both phpstan/phpstan-doctrine and phpstan/extension-installer.
My phpstan.neon is like this:
parameters:
...
2
votes
0
answers
1k
views
Larastan Query Builder instance understanding orWhere preceeding Query Scope
I'm using Larastan to eliminate some of the static analysis errors I have going on in my codebase. I have several errors involving the the Eloquent Higher Order Messaging Proxy known as orWhere.
I'm ...
0
votes
0
answers
97
views
PHPStan do not like logical if shorthand?
Why does phpstan dislike the definition of foo.
That's not a wrong code, or is it?
Although logical if shorthands are difficult to read and are very error-prone.
<?php declare(strict_types = 1);
...
-1
votes
3
answers
615
views
How do typehint a parameter of a function in php [duplicate]
I am having issue getting the return type for this function as I have mixed types in the switch. I have used mixed, it blew up. I have used string|bool and several type for union type.
* @param $...
4
votes
2
answers
3k
views
What is the format for PHPStan's "phpVersion" config option?
I've been searching and reading and hunting and pulling my hair. This is the only mention of phpVersion: https://phpstan.org/config-reference#phpversion
If you want to analyse a codebase as if it was ...
2
votes
1
answer
15k
views
How to make PHPStan ignore specific code snippets (not certain errors)?
I have a lot of while (1) and if (1) etc. in my code. PHPStan thinks these are errors, saying things such as While loop condition is always true. or If condition is always true..
I don't want to turn ...
3
votes
3
answers
12k
views
Why do I get error when try to convert Carbon to DateTime?
I am developing a Laravel project. I try to create a DateTime object by using Carbon. This is what I tried:
Carbon::createFromFormat('Y-m-d H:i:s', '2021-10-01T00:01:00')->toDateTime();
But my ...
0
votes
0
answers
1k
views
Parameter #2 $string of function explode expects string, SimpleXMLElement|null given
I've tried looking at a lot of solutions and still can't seem to find a fix for my issue. I keep getting the following error:
Parameter #2 $string of function explode expects string, SimpleXMLElement|...
10
votes
4
answers
12k
views
PHPStan and Doctrine: $id is never written, only read
I am using PHP8, symfony5 and doctrine2 with phpstan and getting these errors:
Property App\Entity\User::$id is never written, only read.
The code:
<?php
namespace App\Entity;
use App\...
2
votes
1
answer
2k
views
PHP Laminas PHPStan - Call to an undefined method Laminas\Stdlib\RequestInterface::isPost()
We are running phpstan on a laminas project and running into errors.
As an example, in the controller we have some standard code which works fine.
$request = $this->getRequest();
if ($request->...
1
vote
0
answers
592
views
Method ... should return array<a|b|c|d...,mixed> but returns array('key1'=>SomeObject|null,'key2'=>SomeObject2|null)
I am new to PHPStan and I encountered an issue that I can't understand.
So I have a function, that supposed to return an array that looks like this: array<a|b|c|d..., mixed> (a,b,c,d are all ...
1
vote
1
answer
822
views
Set multiple bootsrap files with PHPStan
I have this structure :
apps
- project_1
- app
- vendor
- project_2
- app
- vendor
- project_3
- app
- vendor
phpstan.neon
composer.json
I can’t run PHPStan at the root of "...
5
votes
2
answers
3k
views
PHP 7.4+ class property typing
I'm sure that question has been asked numerous times but I can't seem to find a good/satisfying answer so please bare with me.
Using PHP 7.4+, I tend to type everything I can. But I have some problems ...
1
vote
1
answer
1k
views
Resolve Laravel Auth Authenticatable to User model to address static analysis issues
We have a Laravel 8 application.
We're using the standard Laravel Auth facade to retrieve the authenticated user.
Our User model has a few custom functions, the most important of which is a shorthand ...
3
votes
2
answers
7k
views
PHPStan ignore errors regex
Problem
I recently started using PHPStan to analyse my code for my Laravel projects but I keep getting the following error for all my models:
------ ---------------------------------------------------...
1
vote
1
answer
102
views
How to pass phpStan with a customLoginLinkNotification?
I'm using the login_link (docs) to authenticate an user who forgot his password.
Following the docs, I customized the email and I override the htmlTemplate but in real life phpStan stop me saying :
...
0
votes
1
answer
270
views
PHPStan configuration for FuelPHP
I am having a lot of trouble getting PHPStan to see the FuelPHP core classes. It would appear this sort of thing causes it grief:
namespace Fuel\Core;
/**
* Template Controller class
*
* A base ...
0
votes
1
answer
614
views
PHPStan caching strategy per branch
I'm using Gitlab to deploy my codebase.
Each time, I deploy a branch (in fact multiple times by day), my CI do the following things (very simplified) :
#1 Create a specific deploy folder with my ...
0
votes
1
answer
1k
views
CakePHP with PHPStan: Property does not accept Cake\ORM\Table
Working my way through the levels of PHPStan with a new applicaton, I got to level 3 and started getting error messages from all my test fixtures for models. The basic format is as follows:
------
...
0
votes
1
answer
3k
views
PHPStan with Doctrine and find() - Unable to resolve the template type T
I have this particular code:
<?php declare(strict_types = 1);
interface ObjectManager
{
/**
* @param string $className The class name of the object to find.
* @param mixed $id ...
0
votes
0
answers
158
views
Which PHPStan version to use to accomodate Symfony 2.8, phpstan-symfony and phpstan-doctrine?
I'm fixing a platform which uses Symfony 2.8, with the help of PHPStan (PHP 7.1).
I started with the last version of it (0.12.91), and it was great for the first two levels (0, 1).
Level 2 wants to ...
1
vote
1
answer
927
views
Can phpstan detect "Typed property ... must not be accessed before initialization" Errors?
I have a PHP program,
<?php
class Zap {
}
class Zip {
public Zap $zap;
}
$object = new Zip;
var_dump(
$object->zap
);
This program produces an error because of an initialized non-...
1
vote
1
answer
1k
views
Failed to load codeclimate report, but report exists
in gitlab, I have a pipeline which generate a code climate report, supposed to be displayed in the gitlab widget.
The report is successfully generated (I added cat code-quality.json but I have the ...
1
vote
2
answers
1k
views
How can I configure return type of third party method with phpstan?
I'm working on a Laravel application. In Laravel you can get the currently authenticated user with Auth::user(). The return type of Auth::user() is Authenticatable (which is an interface provided by ...
1
vote
1
answer
86
views
phpstan not pass over strip_tags
i have string of html which i want to explode by <br>, but sometimes it can be inside of other tags.
if (in_array($param, $customOrdering, true) && $value) {
$ordering = array_search(...
0
votes
0
answers
1k
views
PhpStan and CakeDC/Enum, how to fix the "call to an undefined function ..:enum()"
I know it's probably a silly question, but I'm struggling with it since a few hours and I can't manage to fix this error.
I am using https://github.com/CakeDC/Enum in one of my projects.
In my src/...
4
votes
1
answer
7k
views
How to get phpstan to infer the type for my Laravel Collection pipeline?
Given my class
<?php
declare(strict_types=1);
use Illuminate\Support\Collection;
use stdClass;
class PhpstanIssue
{
/**
* @param Collection<Collection<stdClass>> $collection
...
2
votes
1
answer
407
views
How to say phpstan that class is replaced by child
I have a laravel package for logging. It's used like this.
activity()
->performedOn($model)
->causedBy($user)
->log('Look, I logged something');
The activity() helper returns the ...
1
vote
1
answer
17k
views
"return type has no value type specified in iterable type array." error does not reflect reality?
I am on php 7.4 and using phpstan action in github (latest) actions-x/phpstan@v1
I am getting the following error
return type has no value type specified in iterable type array
on the following ...
0
votes
0
answers
99
views
How to correct PHPstan error link to ObjectManager
I have a problem with an error that phpstan brings me up. It's about a load method in a datafixtures.php files. Here is the error :
Parameter #1 $manager (Doctrine\Persistence\ObjectManager) of ...
3
votes
0
answers
487
views
Cakephp 3.x => 4.0 migration: Fatal error in PHPStan\Analyser\MutatingScope
When I run the rector script bin/cake upgrade rector --rules cakephp40 ../project/src I get the following error:
Notice: Undefined property: PhpParser\Node\Stmt\ClassMethod::$attrGroups in phar:///...
0
votes
1
answer
956
views
How to write generics for factories using psalm and phpstan
I'm trying out phpstan and psalm for php and I would like to write a class that can take different type of objects and return the right one based on the factory to call.
What I'm trying to achieve is ...
5
votes
1
answer
2k
views
How can I have phpstan find my custom extension class?
To implement checks for some magic _get() functions, I'm trying to implement a "class reflection extension". The manual mentions how to write the extension files themselves, and to install ...
0
votes
2
answers
1k
views
Phpstan Class_parent not found
I am trying to get static PHP code analysis to work on my OXID 6 eshop.
I am, however, hitting a brick wall when it comes to this:
When I run vendor/bin/phpstan analyse --configuration phpstan.neon ...
0
votes
1
answer
582
views
PHPSTAN : catch an exception without throwing it
I everyone, I have this simple try catch snippet, written in PHP :
try {
...
// some code
...
} catch (Throwable $e) {
$response = ['message' => $e->getMessage(...