Back to Course |
Larastan: Catch Bugs with Static Analysis

Intro, Installation and Common Errors

So, what does Larastan actually do? It shows you potential errors in your code with just one terminal command! Or even better - right in your editor. Let's look at what it brings

We've run ./vendor/bin/phpstan analyze and got the following output:

Look how many issues it spotted! And it's not just random issues - these issues can cause bugs in your code. Scary, right?

So, now, let's start with Larastan and learn how to fix those issues.


Installation and Setup

First, install the package via composer:

composer require --dev nunomaduro/larastan

Then create a phpstan.neon file in the root of your project:

phpstan.neon

includes:
- ./vendor/nunomaduro/larastan/extension.neon
 
parameters:
paths:
- app/
 
# Level 9 is the highest level
level: 5

Don't mind the levels yet - we'll look into those soon!

Next on our list is to run the analysis. To do that, run the following command:

./vendor/bin/phpstan analyse

You should see something like this:

And that's it, that's how default Larastan works! But, of course, there's much more under the hood.

On the first run, you might see a huge list (which depends on the project size) of errors. Don't worry - you don't have to jump and fix them yet! Let's first take you through the basics of what you can expect to see and how to resolve the issues!

Let's start simple and grow from there.


First Common Errors You'll See

Before we dive deeper into the most common issues you'll see - let's make sure that we can read the errors properly. These errors are grouped by the file they are in with lines where you'll find the error exactly. For example:

------ ---------------------------------------------------------------
Line app/Http/Controllers/ExampleController.php
------ ---------------------------------------------------------------
12 Undefined variable: $example
------ ---------------------------------------------------------------

This error is in the app/Http/Controllers/ExampleController.php file on line 12. The error itself is Undefined variable: $example. Quite simple, right? Not that scary once we know what to look for.

Now let's look at the most common ones you could encounter:

Undefined Variable

------ ---------------------------------------------------------------
Line app/Http/Controllers/ExampleController.php
------ ---------------------------------------------------------------
12 Variable $example might not be defined.
------ ---------------------------------------------------------------

It might seem scary, but it actually found a variable in your code that you didn't define and yet it was used. This is a great example of how Larastan can help you find bugs in your code. To fix this, you can either define the variable or remove its usage of it.

Undefined Method

------ ---------------------------------------------------------------
Line app/Http/Controllers/ExampleController.php
------ ---------------------------------------------------------------
12 Call to an undefined method Illuminate\Http\Request::example().
------ ---------------------------------------------------------------

This is a similar error to the one above. It found a method that you called, but it doesn't exist. In this case, you can either define the method or remove the call.

Too Many Parameters Passed to A Method

------ ---------------------------------------------------------------
Line app/Http/Controllers/ExampleController.php
------ ---------------------------------------------------------------
12 Too many arguments for method Illuminate\Http\Request::example().
------ ---------------------------------------------------------------

This error indicates that somewhere in your code you passed too many arguments to a method. Remove them or add them to the method definition.

Many More Other Errors

There are a lot more issues you might see but the pattern will remain the same. You'll see a file, a line, and an error message. The error message will tell you what's wrong and how to fix it.