What it checks
Syntax validation
The analyzer parses every PHP file in your plugin to detect syntax errors:- Uses PHP-Parser to build an abstract syntax tree (AST)
- Reports parse errors with line numbers
- Identifies malformed code that would cause runtime errors
Namespace declaration
Validates that files containing classes, interfaces, traits, or enums have proper namespace declarations:- Files with class definitions should declare a namespace
- Follows PSR-4 autoloading standards
- Prevents naming conflicts
- Classes
- Interfaces
- Traits
- Enums
Strict types declaration
Checks for the presence ofdeclare(strict_types=1) at the top of PHP files:
- Enforces strict type checking
- Improves type safety
- Catches type-related bugs early
While not required, strict types are strongly recommended for better code quality.
Unused imports
Identifiesuse statements that import classes/functions/constants but never use them:
- Detects imported classes not referenced in code
- Keeps code clean and maintainable
- Helps identify dead code paths
The unused import check may produce false positives for classes used only in PHPDoc comments.
Issues detected
Error severity
PHP syntax error
PHP syntax error
The file contains invalid PHP syntax that cannot be parsed.Common causes:
- Missing semicolons
- Unmatched braces or parentheses
- Invalid PHP keywords or constructs
Warning severity
Missing namespace declaration
Missing namespace declaration
A file contains a class, interface, trait, or enum without a namespace declaration.Impact:
- Classes exist in global namespace
- Potential naming conflicts
- Doesn’t follow PSR-4 conventions
Info severity
Missing strict types
Missing strict types
The file doesn’t include Fix: Add strict types declaration for better type safety.
declare(strict_types=1) at the top.Unused import
Unused import
A Fix: Remove the unused use statement or verify it is not used in PHPDoc comments.
use statement imports a class that is never used in the file.Code examples
Correct PHP file structure
Common issues
File scanning
The analyzer processes all PHP files in your plugin:- Retrieves list of PHP files from the analysis context
- Reads each file’s content
- Parses code into an abstract syntax tree
- Runs validation checks on the AST
- Reports all issues found
The analyzer skips files that cannot be parsed and reports the syntax error instead.
Best practices
Always use strict types
Start every PHP file with strict type declaration for better type safety.
Use meaningful namespaces
Organize code with namespaces that reflect your directory structure.
Keep imports clean
Only import classes you actually use. Remove unused imports regularly.
Follow PSR-4 standards
Place classes in appropriate directories matching their namespace.
Limitations
- Classes used only in PHPDoc comments (
@param,@return, etc.) - Classes referenced in string literals or dynamic code
- Classes used in reflection or dynamic class loading
Integration with other analyzers
The PHP file analyzer works alongside other Retina analyzers:- Plugin.yml analyzer - Validates manifest file
- Main class analyzer - Checks main class implementation
- Event handler analyzer - Validates event listeners
- PHPStan analyzer - Deep static analysis
The analyzer scans PHP files at
src/Analyzer/PhpFileAnalyzer.php:14-40