php-standard-library/psalm-plugin
Psalm plugin for PHP Standard Library (PSL) that improves type inference, especially for Psl\Type schemas (e.g., shape/optional), producing precise array shape types after coercion. Install via Composer and enable with psalm-plugin.
shape(), nullish(), non-empty-array) with Psalm’s static analysis, enabling zero-cost validation at compile time. This directly supports Laravel’s shift from runtime validation (e.g., Validator, FormRequest) to static contracts, reducing boilerplate and runtime overhead.array{...} syntax) or modernizing legacy codebases.User, Order), with Psalm enforcing type safety across services, repositories, and API boundaries. This aligns with Laravel’s Eloquent but with stricter static guarantees.psalm-plugin enable), requiring minimal configuration. Existing Psalm setups (v4/v5) are supported, with backward compatibility for PSL v1/v2.Psl\Type\shape() for User).^2.1 is used). Risk mitigated by the plugin’s explicit compatibility matrix.Psl\Iter\last) may generate noisy type inferences. Mitigate by:
suppress_errors for known edge cases.@psalm-suppress annotations sparingly.Validator calls).psalm.config.php to include PSL types:
<?php
return [
'plugins' => [
'Psl\Psalm\Plugin',
],
'types' => [
'Psl' => __DIR__ . '/vendor/php-standard-library/psl/src',
],
];
composer require php-standard-library/psl
FormRequest, Validator, Eloquent models). Example:
// Before: Runtime validation
public function rules() {
return ['email' => 'required|email'];
}
// After: Static validation with PSL
/** @var array{email: string} $validated */
public function validated(): array {
$shape = Psl\Type\shape(['email' => Psl\Type\string()]);
return $shape->coerce($this->input());
}
- name: Psalm
run: vendor/bin/psalm --init --no-cache
phpstorm.php config).Validator with static checks using @psalm-trace.User):
class User {
public function shape(): Psl\Type\Shape {
return Psl\Type\shape([
'id' => Psl\Type\int(),
'email' => Psl\Type\string(),
]);
}
}
FormRequest with PSL shapes.array{...} syntax).~2.0.0 for Psalm v4).@psalm-suppress for unannotated legacy classes.composer require php-standard-library/psalm-plugin --dev
vendor/bin/psalm-plugin enable php-standard-library/psalm-plugin
Validator failures in logs).psalm.config.php (e.g., adding new PSL types).Psl\Type\nullish() support (plugin v2.4.0+).Collection methods.FormRequest").suppress_errors or plugin config.node_modules or generated files.--no-cache for initial runs, then enable caching.How can I help you explore Laravel packages today?