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
0
votes
1 answer

Psalm annotation for multiple-type template

I need to build a trait (or class for that matter) on which I can template multiple types; I've tried something like the following (also descriptive of the problem; the car context is just for illustrating the problem, I know a car is supposed to be…
Dinu
  • 1,374
  • 8
  • 21
0
votes
1 answer

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 that if I pass an object of type A to the…
Full
  • 455
  • 4
  • 13
0
votes
0 answers

Using psalm in .phtml files

I'm trying to make psalm work on .phtml files. Everything goes well, except for one thing. All classes (which should be autoloaded) are undefined. In normal php classes they get loaded by psalm. Following gives UndefinedDocblockClass for…
Quisse
  • 646
  • 1
  • 6
  • 24
0
votes
1 answer

How to make Psalm infer 100% of the codebase in PHP

Currently running Psalm gives me: Psalm was able to infer types for 94.8665% of the codebase How to debug where it couldn't infer the type? I'd like to have 100% type coverage. Here's my psalm.xml.dist config:
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
0
votes
1 answer

Does Psalm supprt @param-out for "this"?

Is it possible to manipulate phantom types in Psalm using @param-out for this or self? Example: /** * @template T */ class Foo { /** * @param T $t */ public function __construct($t) { $this->t = $t; } /** * WRONG: *…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
0
votes
1 answer

Psalm error for TwigFilter callable parameter

I get this error from psalm for a twig extension: ERROR: InvalidArgument - src/Twig/CartExtension.php:44:17 - Argument 2 of Twig\TwigFilter::__construct expects callable|null, array{App\Twig\CartExtension&static, string(getOfferDate)} provided The…
ivoba
  • 5,780
  • 5
  • 48
  • 55
0
votes
1 answer

How to create intersection type of arrays

A simplification of what I'm trying to implement is the following:
zerkms
  • 249,484
  • 69
  • 436
  • 539
0
votes
3 answers

How to type loop that iterates over an array of mixed

Given this code (https://psalm.dev/r/156e52eb66):
zerkms
  • 249,484
  • 69
  • 436
  • 539
0
votes
1 answer

How does one solve this TypeDoesNotContainType error?

This my code: $sftp = ssh2_sftp($connection); if ($sftp === false) { This is the psalm error: ERROR: TypeDoesNotContainType - src/MyFile.php:159:13 - resource does not contain false if ($sftp === false) { ..but the php manual says…
dataskills
  • 646
  • 7
  • 15
0
votes
1 answer

Adapting psalm output to Neomake

I'm trying to come up with the correct errorformat to pass to Neomake, for handling output from psalm (https://github.com/vimeo/psalm). Example output: || ERROR: UndefinedClass - application/libraries/PluginManager/PluginBase.php:58:13 - Class or…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
-1
votes
1 answer

Dynamic return types in PHP

I'm trying to implement something like the Repository of Doctrine does. I do not interact with a database but need to fetch data from sources. I have an AbstractRepository and want to have an autocompletion in PhpStorm based on the class given in…
Sauerbrei
  • 443
  • 4
  • 14
1 2 3
4