This configuration will help you maintain ideal code quality in Laravel and Vuejs application. The best part is this configuration will help you better the code quality day by day by each developer so you don't have to do code freeze for months just to improve code quality.
1. First let's start by adding "Duster" package to Laravel
composer require tightenco/duster --dev
2. Now let's also add PHPstan and Larastan for laravel
composer require --dev phpstan/phpstan
composer require --dev "larastan/larastan:^3.0"
Now add configuration file for phpstan with name of "phpstan.neon"
includes:
- vendor/larastan/larastan/extension.neon
- vendor/nesbot/carbon/extension.neon
parameters:
paths:
- app/
# Level 10 is the highest level
level: 5
# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
3. For Frontend eslint is enough so let's add that
npm install --save-dev husky eslint eslint-plugin-vue
Add the following lines to package.json
"scripts": {
"lint": "eslint . --fix --ignore-pattern .gitignore",
"prepare": "husky"
},
"lint-staged": {
"*.{vue,js}": "eslint --fix"
}
Now let's add eslint configuration for Vue in latest format
create a file on root with name "eslint.config.js"
import globals from "globals";
import pluginJs from "@eslint/js";
import pluginVue from "eslint-plugin-vue";
export default [
{ files: ["**/*.{js,mjs,cjs,vue}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...pluginVue.configs["flat/recommended"],
{
rules: {
'vue/html-indent': ['error', 2, {
attribute: 1,
baseIndent: 1,
closeBracket: 0,
alignAttributesVertically: false,
}],
"vue/html-closing-bracket-newline": ["error", {
"singleline": "never",
"multiline": "always"
}],
// override/add rules settings here, such as:
// 'vue/no-unused-vars': 0,
},
}
];
4. Finally let's configure Husky
The husky pre-commit file will only run above linting tools on staged files and will not stop commit if these command fails with errors.
If you want to fail commit if any error occurs then just remove "|| true" part.
npx husky init
# .husky/pre-commit
./vendor/bin/duster fix --dirty || true
npx lint-staged || true
# Get list of staged PHP files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=AM | grep '\.php$' | tr '\n' ' ')
if [ -n "$STAGED_FILES" ]; then
# Run PHPStan only on staged PHP files
composer phpstan $STAGED_FILES || true
else
echo "No PHP files to analyze."
fi
Top comments (0)