Every great tool can be hard to use. Especially if they are an inconvenience or something you have to remember each time.
For example, you wouldn't want to go into your terminal and run ./vendor/bin/phpstan analyse
every time you want to run PHPStan. It would become tedious, and you might often forget it. That's why it's important to integrate it into your existing code editor or IDE.
While we could write a guide on how to set up PHPStan in PHPStorm, we don't have to. PHPStorm has a great guide on how to set it up. You can find it here.
But to summarize it, you need to install PHPStan globally, and then you can set it up in PHPStorm:
Settings
> PHP
> Quality Tools
> PHPStan
phpstan.neon
And you should be good to go. Once you open a file that has an error inside - you should see it is marked:
That's it, it should work. If you have any issues, you can check the official PHPStorm guide.
A question that might rise from this, especially given extensive PHPStorm capabilities - what reports will I start to see?
Let's look at a few examples:
Let's take a look at the following line - it's not marked by PHPStorm by default:
But if you install PHPStan, you'll see that it's marked as an error:
Another quick example is PHPStorm not marking the following code as containing errors:
public function index(){ abort_if(Gate::denies('project_access'), Response::HTTP_FORBIDDEN, '403 Forbidden'); $projects = Project::with(['client', 'status'])->get(); return view('admin.projects.index', compact('projects'));}
Even if it should in have a return type of View
. PHPStan will mark it as an error:
The last obvious example is using relationships in the wrong way. For example, if you try to access a relationship that doesn't exist, PHPStorm won't mark it as an error:
But PHPStan will:
To set it up, you need to install PHPStan globally, and then you can set it up in VSCode:
Of course, there are many more extensions but this seems to be the most downloaded one for now. You can take a look at others too
Another way to get informed about potential issues in your code is having a GitHub Action with PHPStan. We'll take the most basic setup for GitHub Actions and add PHPStan to it.
.github/workflows/PHPStan.yml
name: PHPStan inspections on: push: branches: [ "*" ] pull_request: branches: [ "*" ]jobs: phpstan-inspections: runs-on: ubuntu-latest steps: - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e with: php-version: '8.1' - uses: actions/checkout@v3 - name: Copy .env run: php -r "file_exists('.env') || copy('.env.example', '.env');" - name: Install Dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: Generate key run: php artisan key:generate - name: Directory Permissions run: chmod -R 777 storage bootstrap/cache - name: Create Database run: | mkdir -p database touch database/database.sqlite - name: Launch PHPStan inspection run: vendor/bin/phpstan analyze
And after committing the file to GitHub - it should automatically run PHPStan and you should see the results in the Actions
tab:
Once the run is complete - you might see a red cross next to the commit. If you click on it, you should see the details of the run:
And that's it in this course about Larastan, that's all the basics you need to know! But if you have any more questions, ask them in the comments and we may upgrade the lessons in the future with more content and examples.