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
1
vote
1 answer

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 create(string $class): object { ... some factory…
useless
  • 1,876
  • 17
  • 18
1
vote
1 answer

How to tell psalm to save getter state if property wasn't modified before the further checks?

class A { private ?string $x = null; public function getX(): ?null { return $this->x; } } class B { public function __construct(string $y) { // Property initialization... } } $a = new A(); if…
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
1
vote
2 answers

Psalm multiline associative array annotation

I'm using Psalm style annotations to document for instance return array types, such: /** * @return array{ key1: type1, key2: type2, key3: array{ nestedKey1: array } } */ This works like a charm for autocomplete / static analysis,…
Dinu
  • 1,374
  • 8
  • 21
1
vote
2 answers

How to annotate Laravel Collection elements during iteration

I was thinking about how to annotate types in PhpStorm. I believe PhpStorm is using Psalm to resolve types, but I can't find how to annotate type to get suggestions here: $row in my app will always be Collection object and I want to have it marked…
Eddy
  • 593
  • 8
  • 22
1
vote
2 answers

Php psalm configuration and syntax error reporting

I have a case with a simple syntax error. It goes like this class Foo { public function __construct($a,$b,$c,) {...} So, there is an syntax error after param $c -- an excess comma (im using php7.3). When i run psalm ./vendor/bin/psalm…
Alez
  • 95
  • 3
  • 8
1
vote
1 answer

Extend a generic of an interface using psalm

I'm getting used to use psalm, but I'm facing an issue. I already have this structure in C# and it worked out for me. I didn't really get how I could solve this using psalm. I have a ContextInterface and another implementing it. interface…
Zoltán Fekete
  • 504
  • 1
  • 6
  • 22
1
vote
0 answers

Template string as object or array of object

I'm currently trying to fix the annotations of a function of my project that looks like this : https://psalm.dev/r/7b974e8822 However, it always refuse something like A::class . '[]' as a class-string and would like only a string. But doing so I…
MinDBreaK
  • 153
  • 2
  • 16
1
vote
0 answers

Checking "PropertyNotSetInConstructor" on a custom function with Psalm

Is there a way I could tell Psalm to check other functions than __construct() methods for ensuring all properties are set when a class is instantiated ? In some PHP library, Psalm complains about PropertyNotSetInConstructor because it is not able to…
Emmanuel Guiton
  • 1,315
  • 13
  • 24
0
votes
1 answer

PossiblyUnusedMethod: Cannot find any calls to method in Repository

I'm working on a Symfony project and I'm starting using Psalm. Almost everything is fine, as I keep getting an error that I don't understand: ERROR: PossiblyUnusedMethod - src/Repository/PlaylistRepository.php:116:21 - Cannot find any calls to…
Xem
  • 23
  • 1
  • 4
0
votes
1 answer

Generics on Interfaces

I have an interface that implements a method which takes an array of format X and returns an array with the same format X. I tried using generics to express this, but apparently I can't do so because I'm getting "ImplementedReturnTypeMismatch" and…
0
votes
1 answer

How to solve a problem with a DocblockTypeContradiction in Psalm

I have the simplified code example from my library, to which psalm, for reasons I don't understand, outputs warnings. class Example { public const C_1 = 'val1'; public const C_2 = 'val2'; public const C_3 = 'val3'; /** * @param…
XYZ
  • 155
  • 1
  • 5
0
votes
1 answer

Does PhpStan and Psalm measures cyclomatic complexity?

I've read the documentation from PhpStan and Psalm, and i couldn't find the rules where it checks the Cyclomatic Complexity like Phpmd does here Also, is it possible to cusomise rules within levels for PhpStan and Psalm? Looking for a source.
Mali C.
  • 11
  • 4
0
votes
0 answers

PSALM / PHPStan / PHPDoc Types: using templates/generics as array keys (of multiple elements array)

I cannot figure how to annotate an array returned from API Response. Response have a structure: array{'totalCount': numeric-string, $vehicleType: array} Instead of variable $vehicleType there can be different keys. List of all possible keys…
ionov-e
  • 1
  • 1
0
votes
1 answer

Use template in var docblock php

I have a php class containing a collection of class. It uses an array with classname as key and instance as value. So I have a getter that takes a classname and returns the corresponding instance (or null if not find). I'm trying through docblock to…
Gashmob
  • 5
  • 4
0
votes
0 answers

Why can't psalm for Laravel Fortify CreateNewUser infer the return type?

I have a new Laravel installation and I'm trying to set up authentication with Fortify. When I ran psalm, I got into these issues: ERROR: MixedInferredReturnType - app/Actions/Fortify/CreateNewUser.php:22:43 - Could not verify return type…
Mcload
  • 288
  • 8
  • 18