Questions tagged [psalm-php]

Psalm is a static analysis tool that’s designed to improve large PHP codebases by identifying both obvious and hard-to-spot bugs.

In addition to more common singular and union types it also supports generics, shape arrays, enums and intersection types.

Psalm comes with a fixer that allows you to improve your code automatically, too.

You should use Psalm if you want to

  • prevent errors in a big refactor
  • maintain a consistent level of quality across a large team
  • guarantee that there won’t be any type-related runtime errors

Useful links:

56 questions
4
votes
2 answers

Psalm possibly null value provided with request->get()

I have the following code: $request->headers->get('Accept-Language', 'en'); I provide a default value but Psalm thinks it's potentially null since ->get() declares that return a nullable string: // vendor/symfony/http-foundation/HeaderBag.php /** …
michael
  • 421
  • 6
  • 21
4
votes
2 answers

How do I use Psalm's UnusedMethod Feature?

I'm trying to use the psalm static analysis tool for PHP. It's my understanding that this tool can tell me about unused methods in my codebase. However, if I create a simple test file #File: src/test.php
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
3
votes
1 answer

Laravel Factory Generics With Psalm

I'm struggling to get the generic to work for the Laravel 9 factories with psalm. Doc block: /** * @extends Factory */ class TripFactory extends Factory { ... } Use case: TripFactory::new()->create(); Error: InvalidTemplateParam -…
Carl Wirkus
  • 523
  • 1
  • 8
  • 19
2
votes
1 answer

PSALM: Docblock-defined class or interface does not exist

I have the following code: namespace Some\Space\Utility; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; /** * @psalm-template T */ class SomeAdapter { /** * Some method * *…
Zoltán Fekete
  • 504
  • 1
  • 6
  • 22
2
votes
0 answers

How to lint Namespace based on folder's name?

I know PHP CS Fixer and Psalm to lint PHP. I've been using both of them. But I can't find a way to lint / validate my namespace name based on the folder. For example, in folder Tests/Controllers/V1, I have MyControllerTest.php, and it has the…
rhzs
  • 516
  • 7
  • 24
2
votes
2 answers

Psalm: Handle method that returns multiple types

EDIT This is a Psalm thing, not a PHP MD thing. I am writing a Symfony console command. In its execute method I retrieve argument with the $input->getArgument('argument_name') method. And I pass this value to a service, which expects this value to…
george007
  • 609
  • 1
  • 7
  • 18
1
vote
2 answers

Psalm: Unable to determine the type that $x is being assigned to

I have the following code and want to get it through Psalm static checks: if ( empty($sessions[$token]->product) || !is_object($sessions[$token]->product) ) { continue; } if ( empty($sessions[$token]->product->view_list) ||…
Alex
  • 32,506
  • 16
  • 106
  • 171
1
vote
2 answers

Psalm stdClass with properties definition anotation error

What is the correct returning object definition in psalm? /** * @psalm-return \stdClass{foo?: string} */ function returnObject(): \stdClass { $item2 = new \stdClass(); $item2->foo = "asd"; return $item2; } returnObject(); ERROR:…
Vixtrime
  • 443
  • 1
  • 5
  • 15
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
0 answers

Is there an error baseline feature for Typescript

The PHP linter "Psalm" has a feature called baseline. It can be used to "ignore" errors that are tracked by a baseline file but prevent new errors from accumulating. This is useful in larger or legacy codebases where there are lots of errors that…
FabianTe
  • 516
  • 4
  • 22
1
vote
0 answers

PHP Psalm covariance on method parameter

I have a case in PHP psalm that can be represented by this snippet: https://psalm.dev/r/c31997fed4 The handleNode() method does not care about the type of node used, it should only care about that it is a Node. I have not been able to find a…
Jan Sverre
  • 4,617
  • 1
  • 22
  • 28
1
vote
1 answer

Proper type hinting for IteratorAggregate satisfying Psalm and PhpStorm

I have collections using IteratorAggregate interface. And I can't find a way how to type hint it properly that both Psalm and PhpStorm are satisfied. Here is a simplified example. It has an AbstractCollection and one Collection, but there are…
nickel715
  • 2,505
  • 1
  • 23
  • 28
1
vote
0 answers

Psalm - how to deal with optional classes

I have a cache factory that will use one of many cache drivers: File, Redis, Memcache, etc. Because it's either-or, my code can be used both with and without certain PHP extensions. eg. If you use Redis, you don't need ext-memcached installed. But…
HubertNNN
  • 1,727
  • 1
  • 14
  • 29
1
vote
2 answers

UnitEnum cannot be cast to string

I have a variable declared in config/services.yaml parameters: login_url: '%env(string:APP_FRONTEND_DOMAIN)%' I am accessing it in my controller like this: $loginUrl = (string) ($this->getParameter('login_url') ?? ""); Everything works fine,…
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
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
2 3 4