Using PHPStan in a Standalone PHP MVC

If you’re building a standalone PHP MVC framework (not Laravel), one of the easiest ways to improve code quality is adding PHPStan. It helps you catch bugs before they show up in the browser or production—without needing to run your whole app.

We have a git repo to demonstrate this. Run command: vendor/bin/phpstan analyse -l 6 src and see what it comes with!

What PHPStan Actually Does (and what it doesn’t)

PHPStan is a static analysis tool. That means it reads your PHP code and points out problems by analyzing structure and types.

PHPStan can catch things like:

  • Calling a method that doesn’t exist on a class
  • Passing the wrong type to a function (string vs int, etc.)
  • Using a variable that might be null
  • Returning the wrong type from a method
  • Accessing undefined properties

PHPStan does not:

  • Run your application
  • Hit routes/endpoints
  • Check “does this page return 200?”
  • Verify business logic output at runtime

PHPStan vs Pest/PHPUnit (Common confusion)

You asked if PHPStan is like Pest in Laravel. The answer is: they’re different tools used for different goals.

  • Pest / PHPUnit = Testing
    • Runs the code
    • Confirms behavior with assertions (expected vs actual)
  • PHPStan = Static analysis
    • Doesn’t run the code
    • Finds mistakes by inspecting code and types

Best practice

Use both if you can:

  • PHPStan to prevent obvious mistakes and type issues early
  • Tests (Pest/PHPUnit) to confirm real behavior and prevent regressions

Is PHPStan free?

Yes — PHPStan is free and open-source (MIT license).
You can install and use it in personal or commercial projects at no cost.

(There is a paid “Pro” offering, but the main PHPStan tool most people use is completely free.)

Do you need namespaces to use PHPStan?

No. PHPStan can analyze plain PHP files even if you don’t use namespaces.

That said, namespaces + proper autoloading help PHPStan understand your project better, especially in larger codebases. But they’re not required to get value from PHPStan.

Just run the command: vendor/bin/phpstan analyse and see what errors it comes up with! The benefit is that you did not executed the code yet! System prevent all possible bugs before executing the code!