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
2 answers

How to resolve Pslam MixedReturnTypeCoersion

ERROR: MixedReturnTypeCoercion - src/Entity/User.php:98:16 - The type 'non-empty-array' is more general than the declared return type 'array' for App\Entity\User::getRoles (see https://psalm.dev/197) …
virtualLast
  • 573
  • 1
  • 5
  • 19
0
votes
1 answer

Psalm and PHPStan unable to read Symfony entity string:class

I'm implementing PHPStan into my application and also testing Psalm for automatic fixes, however I'm unable to make them read entity class strings (with colon) fe: $repository = $this->em->getRepository('MyApp:EntityClass'); turns into Psalm…
0
votes
1 answer

phpstan and psalm ail because of an unidentifiable problem

For a php project, for some time my tests fail for phpstan and psalm, with unclear error messages. I tried to debug, to rebuild the commits, but without success. I can't understand or so. These are the output, the composer.json and the ci.yml. I can…
Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36
0
votes
0 answers

UndefinedDocblockClass psalm error when using a trait to mark magic properties as deprecated

I'm trying to use PhpDoc to hint all the deprecated magic properties set on a Laravel Model using the mixin described in this answer. I am using JetBrains PhpStorm 2022.1.1, PHP 8.1.6 and Psalm 4.23.0 I have a trait, defined in…
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
0
votes
1 answer

Is there a way to correctly use sanitize_text_field and wp_unslash that doesn't cause psalm to error with "expects string, possibly different type"

I am writing a WordPress plugin, and need to correctly unslash and sanitise a variable in PHP. I am using psalm as my static analysis tool, and it is identifying my code as containing a potential error: // Check your nonce. if ( isset(…
newtovaux
  • 151
  • 1
  • 7
0
votes
0 answers

Psalm stopped seeing errors

I'm here with the one of the weirdest questions in my life. Suddenly psalm stopped seeing any errors in my code. I was fixing some issues, and then all of a sudden I stopped getting errors from psalm. I know there are many of them, because I used to…
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
0
votes
1 answer

PHPDoc & Psalm - Annotate an "array of classes"

In my code I created an EventFactory like this: private array $events = [ 'post_created' => PostCreatedEvent::class, 'exercise_executed' => ExerciseExecutedEvent::class, ]; public function fromTopicAndData(string $topic, array $data) :…
0
votes
1 answer

In PHP how do I type hint an instance of a class accessed using a string as the class name

Using Laravel, I'm iterating through a series of classes and doing an operation on a subset of instances of each class. The classes are provided as strings, eg: $c = '\App\Models\Book'; $c::each(function($i) { echo $i->title . PHP_EOL; }); How…
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
0
votes
1 answer

Psalm check for calls to "die"

Is there a way to configure Psalm that calls to die or exit cause an error? I have trouble with a codebase that heavily uses these two commands and want to at least avoid people adding new die commands that should not exist.
FabianTe
  • 516
  • 4
  • 22
0
votes
1 answer

How to make Psalm recognize variables from an included file

I have a config file that's included in a function, like this: function getConnection() { include 'config.php'; return new Connection($config['host']); } The issue is to make Psalm recognize the $config variable from the config file. Possible?…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
0
votes
1 answer

Pagerfanta template causing error in psalm

I am trying to move from phpdoc to psalm and I am getting weird error with its template based returns. class Message {} interface FooInterface { /** @return Pagerfanta|Message[] */ public function getMessages(): Pagerfanta; } This is…
HubertNNN
  • 1,727
  • 1
  • 14
  • 29
0
votes
0 answers

PhpStorm attributes and Psalm

I have PHP 8 on my server, and I'm using attributes JetBrains\PhpStorm\ArrayShape and JetBrains\PhpStorm\Pure as PhpStorm suggests. When I'm running psalm it returning me the following error: ERROR: UndefinedAttributeClass - .....:66:7 - Attribute…
Eugene
  • 1
  • 1
  • 3
0
votes
1 answer

Psalm reports InvalidArrayOffset, but the value will be within bounds

When running Psalm on this simple "random string" generator: $letters = 'abcdefghjklmnpqrstuvwxyz'; $numbers = '23456789'; $number_count = \strlen($numbers); $letter_count = \strlen($letters); $pass = ''; while (\strlen($pass) < 9) { …
user12421590
0
votes
1 answer

Psalm: How to handle dedicated view files?

My set-up comprises a lib folder with classes and a view folder with PHP files, that produce output. The views are imported inside a View class similar to this: class View { public function render(string $basename, Array $params) : string { …
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
0
votes
1 answer

How To Declare Traversable Type?

There are multiple typehints in PHP, more now in PHP 8, and even more with Psalm. But what about declaring the type, rather than hinting the expected type? I have a interface Collection extends Traversable, and I need to declare that it is a…
XedinUnknown
  • 677
  • 9
  • 25