How to perform static analysis of PHP code: tools and usage examples
Hello everyone! I've been writing PHP for about a year, and my project (an online store on a custom engine) has grown to 50+ files. I've noticed that strange errors appear in the code: sometimes a variable is not declared, sometimes a method is called on null, sometimes types don't match. Manually searching for such bugs is a nightmare.
I've heard that in other languages (e.g., TypeScript or Java) there is static code analysis — programs that check code before it runs. But how does it work in PHP? Is there something similar? I tried enabling strict types (declare(strict_types=1)) and error_reporting(E_ALL), but this doesn't catch logical errors.
For example, I have this code:
```php
function calculateDiscount($price, $percent) {
return $price - ($price * $percent / 100);
}
$total = calculateDiscount(1000, '10%'); // Typo
```
No answers yet.
Log in to leave an answer