Next, we will work on another Code quality tool - Larastan.
At first glance, it does the same job as our IDE - it shows errors and warnings in the code. But Larastan is more than that.
The idea behind Larastan is to catch errors that are not visible in the code but can happen during runtime (e.g., calling a method on a non-existing model attribute).
On top of that, it also checks for compatibility issues. For example, if you have a method that should return a string, but you return an integer, Larastan will catch that.
First, we create a GitHub issue to install Larastan:
Then, we create a new branch:
git checkout -b feature/larastan
Now we can install Larastan, we run the composer command:
composer require --dev "larastan/larastan:^3.0"
Then, we need to create a configuration file for Larastan. In this case, we want to use the highest level of strictness, which is Level 9.
Note: You are not required to go with Level 9; feel free to choose a lower level if you want to start with less strictness.
phpstan.neon
includes: - vendor/larastan/larastan/extension.neon - vendor/nesbot/carbon/extension.neon parameters: paths: - app/ # Level 9 is the highest level level: 9 # ignoreErrors:# - '#PHPDoc tag @var#'## excludePaths:# - ./*/*/FileToBeExcluded.php
And that's it - we are ready to run Larastan. So, let's push this code to GitHub:
git add .git commit -m "Installed Larastan at Level 9 - resolves #6"git push origin feature/larastan
Now, we can create a Pull Request and merge it into the dev
branch.
And create a new issue to run and apply Larastan suggestions:
Let's create another Branch for this:
git checkout -b improvement/larastan-code-changes
Note: We called this branch improvement
because we are not adding new features but improving the existing code.
Now, we can run Larastan locally:
./vendor/bin/phpstan analyse
This will output all the issues found by Larastan:
And at this stage, you have to manually fix all these issues. Some fixes might look like this:
Adding missing Return Types:
Telling Larastan more information about a Variable:
And so on. Larastan can catch many issues, and it's a good idea to fix them all.
Note: Different Levels find different issues. You can start with a lower level and then increase it as you fix the issues.
Once you are done, you can push the changes to GitHub:
git add .git commit -m "Larastan Level 9 run - resolves #7"git push origin improvement/larastan-code-changes
As the last step, we will add a GitHub Action to run Larastan on every Pull Request:
.github/workflows/larastan-check.yml
name: Static Analysis on: [ push, pull_request ] jobs: tests: name: Run Larastan runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: '8.2' extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv coverage: none - name: Run composer install run: composer install -n --prefer-dist - name: Prepare Laravel Application run: | cp .env.example .env php artisan key:generate - name: Run Larastan run: ./vendor/bin/phpstan analyse
This works the same way as previous GitHub Actions, but this time it runs Larastan.
Now, we can push this code to GitHub:
git add .git commit -m "Adding Larastan Workflow - resolves #7"git push origin improvement/larastan-code-changes
And create a Pull Request to merge this code into the dev
branch to see Larastan in action:
In the next lesson, we will work on a new feature - newsletter issues CRUD. The goal will be to practice what we've learned so far and repeat the same process for that new feature.