Known Issues / Errors
UnitEnum UndefinedDocblockClass
This issue might happen if you're running on PHP 8. To fix this, open the psalm.xml and look for the section wrapped by <issueHandlers> tags and add the following Code:
<issueHandlers>
<!-- known issue in php8: https://github.com/symfony/symfony/issues/45609#issuecomment-1056816975-->
<UndefinedDocblockClass>
<errorLevel type="suppress">
<referencedClass name="UnitEnum" />
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
Missing requires for 'ext-*' PHP extensions
If you encounter this error, you have 2 options.
- First, check if there's a require for PHP in the composer.json
- If the require exists, you can add the exact class, that is causing the error, as a seperate require.
This is the O N L Y time you do not have to specify a version - just add a "*" as the Version
InternalMethod Error
ONLY when unavoidable
Sometimes you're getting InternalMethod Errors - for example, Context::CreateDefaultContext() causes this. In some cases, like that one, we don't have the means of preventing the Error from happening.
In this case you can add a specific PHPDoc Tag, telling CQ to surpress this specific Error.
Again, this is your LAST RESORT
/**
* @psalm-suppress InternalMethod
*/
class MyClass
Skills
Type validation - regular
When psalm doesn't know which type a variable has, you have 2 options.
PHPDoc
This will mostly be enough
/** @var string $variable */
$variable = $object->getName();
assert()
assert() will make break your code if the type assertion isn't matching!
To assert a class: assert($variable instanceof MyClass);
To assert a variable type: assert(is_string($variable)) (or equivalent)
Type validation - own arrays
Type validation with custom arrays is a lot more complex - you have to basically rebuild your array structure in a PHPDoc comment. Let's assume you have this array:
$myArray = [
"id" =>"aifh212287jkhwkjwfh",
"translated"=> [
"name"=>"Mein Name"
]
]
Define a custom Type
First, you have to define the type. Do this in the PHPDoc comment above your class:
@psalm-type TMyArray = array {
id: string,
translated: array {
name: string
}
}
Here, we define the type TMyArray and we tell it what structure it has and what data types the different elements have.
Assign the type to your variable
You do this as with other, regular data types:
/** @var TMyArray $myArray */
$myArray = [
"id"=>"aifh212287jkhwkjwfh",
"translated"=> [
"name"=> "Mein Name"
]
];
Exclude folder - psalm
To exclude folders from psalm validation (for example, when .js files are being validated), add a <ignoreFiles> tag to the <projectFiles> part of your psalm.xml.
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="src/Resources/app" />
</ignoreFiles>
</projectFiles>
Exclude folder - phpcs
To exclude folders from phpcs validation, navigate to the phpcs: part of your .phpcq.yaml.dist file.
Here, we add the excluded: part.
phpcs:
plugin: phpcs
config: &phpcs-config
standard: PSR12
custom_flags:
- '--extensions=php'
excluded:
- 'src/Resources/app'