php-standard-library/psalm-plugin
Psalm plugin for PHP Standard Library (PSL) that improves type inference for PSL Type specifications (e.g., shape/optional), producing more precise array shapes and safer analysis. Install via Composer and enable with psalm-plugin.
Build vs. Buy: Static Analysis for PHP Standard Library (PSL) Adopting this plugin eliminates the need to build custom type inference logic for PSL, reducing development time and maintenance costs. It integrates seamlessly with Psalm, a widely adopted static analysis tool, aligning with industry standards and leveraging existing tooling. This supports a "buy" decision for PSL integration, ensuring scalability and long-term maintainability.
Roadmap: Enhancing Code Quality and Developer Productivity
Validator) with PSL shapes where Psalm can statically verify correctness, reducing runtime overhead and improving performance.Feature: Self-Documenting and Machine-Verifiable Contracts Use PSL shapes to define API contracts, request/response payloads, and data structures. This enables:
// Define a user shape for API contracts
$userShape = Type\shape([
'id' => Type\positive_int(),
'name' => Type\string(),
'email' => Type\email_address(),
]);
// Validate incoming request data
$requestData = $userShape->coerce($_POST);
/** @psalm-var array{id: positive-int, name: string, email: email-address} $requestData */
Use Case: High-Assurance Systems (Financial, Payments, Webhooks) Critical systems requiring zero tolerance for runtime type errors can leverage this plugin to:
$webhookShape = Type\shape([
'id' => Type\string(),
'object' => Type\literal('charge'),
'amount' => Type\positive_float(),
'currency' => Type\literal('usd'),
]);
$webhookData = $webhookShape->coerce($payload); // Psalm catches invalid types at dev time
Use Case: Replacing Eloquent Model Validation Logic Replace manual validation logic in Eloquent models with PSL shapes enforced by Psalm. This reduces boilerplate code, improves type safety, and ensures consistency across data validation layers. Example:
// Define a model shape using PSL
$userShape = Type\shape([
'name' => Type\string(),
'email' => Type\email_address(),
'age' => Type\positive_int(),
]);
// Validate model attributes statically
$userData = $userShape->coerce($request->all());
/** @psalm-var array{name: string, email: email-address, age: positive-int} $userData */
Adopt this package if:
Look elsewhere if:
"This plugin integrates seamlessly with our existing Psalm static analysis tool to supercharge type safety for the PHP Standard Library (PSL). By adopting it, we can:
"The PSL Psalm Plugin enables us to:
Validator) with PSL shapes enforced by Psalm, reducing boilerplate and improving type safety."This plugin makes working with PSL a breeze by:
How can I help you explore Laravel packages today?