Psalm checks PHP types, array shapes and call contracts using source declarations and PHPDoc refinements.
What It Does
Initialize a baseline configuration for the source directory. Precise array shapes help the analyzer reason about keys and values.
Requirements and Compatibility
Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.
Syntax, Inputs and Results
The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.
Example 1: Basic Use
Initialize a baseline configuration for the source directory.
composer require --dev vimeo/psalm
vendor/bin/psalm --init
Example 2: Another Case
Precise array shapes help the analyzer reason about keys and values.
<?php
/** @param array<string, int> $stocks */
function available(array $stocks, string $sku): int {
return $stocks[$sku] ?? 0;
}
echo available(['A1' => 5], 'A1');
Practical Example
Inspect type errors and informational diagnostics separately.
vendor/bin/psalm --no-progress
vendor/bin/psalm --show-info=true
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Project bootstrap and stub configuration affect analysis accuracy.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| Psalm vs PHPStan | Both provide static type checks but have distinct rule sets, type inference and configuration formats. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Silencing diagnostics instead of fixing contracts | Annotate arrays and returns accurately; suppress only well-understood false positives. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | Project bootstrap and stub configuration affect analysis accuracy. |
Key Takeaways
- Psalm checks PHP types, array shapes and call contracts using source declarations and PHPDoc refinements.
- Project bootstrap and stub configuration affect analysis accuracy.
- Keep application configuration, source code and validation tests under version control.
Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.