Vimeo/Psalm is a powerful open-source static analysis tool for PHP, developed by Vimeo. Its primary purpose is to find errors in PHP applications without actually running the code. By analyzing code at rest, Psalm helps developers catch a wide range of potential issues, from simple type mismatches to complex logical errors, thereby significantly improving code quality, reliability, and maintainability.\n\nKey Features and Benefits:\n\n* Static Type Checking: Psalm excels at type inference and validation. It leverages PHP's native type hints (scalar, return, property types) and PHPDoc annotations (like `@param`, `@return`, `@var`, `@template`, `@psalm-type`, `@psalm-param`, etc.) to build a robust understanding of the data flow and types within an application. It can detect type mismatches, nullability issues, and incorrect usage of classes or interfaces.\n* Error Detection: It can identify a vast array of errors including undefined properties or methods, incorrect method arguments, unused code, dead code paths, and potential runtime exceptions (e.g., trying to access an array offset on a non-array).\n* Code Quality Enforcement: Beyond just finding errors, Psalm can enforce coding standards and best practices, encouraging developers to write more robust and predictable code.\n* Support for Complex Types: Psalm supports advanced type features like generics, union types, intersection types, template types, and more, allowing it to perform highly granular analysis even in complex codebases.\n* Performance and Scalability: Designed for large applications, Psalm is performant and can be configured to analyze specific parts of a codebase or ignore certain files/issues.\n* Integration: It integrates well into CI/CD pipelines, pre-commit hooks, and IDEs (via Language Server Protocol - LSP), providing immediate feedback to developers.\n* Baselines: For existing projects, Psalm allows you to generate a "baseline" of existing issues, enabling you to fix new issues while gradually addressing older ones.\n* Plugin System: Extend Psalm's functionality with custom checks and integrations.\n\nHow it Works:\n\n1. Parsing: Psalm first parses the PHP source code into an Abstract Syntax Tree (AST), which is a hierarchical representation of the program's structure.\n2. Type Inference: It then traverses the AST, using all available type information (native type hints, PHPDoc, assignments, control flow) to infer the most precise types for variables, properties, and return values throughout the application.\n3. Validation: With the inferred types, Psalm validates operations and assignments. If an operation expects a certain type but receives a potentially incompatible one, Psalm flags it as an issue.\n4. Reporting: Finally, it compiles and reports all identified issues, categorizing them by severity and providing clear explanations.\n\nIn essence, Psalm acts as an extra pair of eyes for your code, catching potential problems before they reach production, thereby leading to more stable and maintainable PHP applications.
Example Code
<?php\n\nclass Calculator\n{\n /\n * @param int $a\n * @param int $b\n * @return int\n */\n public function add(int $a, int $b): int\n {\n return $a + $b;\n }\n\n /\n * This method has a potential type error that Psalm would catch.\n * It expects an array but tries to return a string.\n * Psalm would report: "Method \'processItems\' has a declared return type \'array\' but the method body returns \'string\'."\n *\n * @param array<string> $items\n * @return array<string> \n */\n public function processItems(array $items): array\n {\n // Psalm would detect that 'implode' returns a string,\n // but the return type hint (and docblock) expects an array.\n return implode(', ', $items);\n }\n\n /\n * A corrected version that matches the return type.\n *\n * @param array<string> $items\n * @return string\n */\n public function processItemsCorrected(array $items): string\n {\n return implode(', ', $items);\n }\n}\n\n$calc = new Calculator();\necho "5 + 3 = " . $calc->add(5, 3) . "\n";\n\n// If Psalm were run on this file, it would flag an error in processItems.\n// To run Psalm (after installing via Composer):\n// composer require vimeo/psalm --dev\n// vendor/bin/psalm\n\n// Correct usage for the corrected method\necho "Processed items (corrected method): " . $calc->processItemsCorrected(['apple', 'banana']) . "\n";\n








Vimeo/Psalm