Lint your staged files using composer and a pre-commit hook. This strategy mimics (sort of) npm husky and lint-staged packages.
This is a little experiment to test availability of using the same approach we have when developing JS using node and npm.
Alternatively, you can make use of the regular approach using such
nodepackages [reference].
- PHP application written using composer to manage packages.
- PHP version >=
8.1 - For now, it only runs with
composerinstalled globally and on*nixenvironments (MacOS, Linux and Windows WSL).
- Clone this repo or download the repository files
- Edit
composer.jsonfile to create your project.
Keep in mind the composer file contains dummy information, such as
name,descriptionandautoloadentries.
Composer configuratrion reference: https://getcomposer.org/doc/01-basic-usage.md
Case you have an ongoing project that you would like to implement this strategy, you need to perform the following steps
composer install --dev nikic/iter webmozart/globNote: These packages are necessary in order to run the php script that will lint your files when performing a commit.
Note: this is the core of this strategy, this folder contains the necessary configuration template and scripts used to install requirements and run the linter when performing a commit.
{
//...
"scripts": {
//...
"setup": "test ! -f composer.lock && php .config/post-install.php || echo 'Skipping setup...'",
"lint": ["php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php", "php psalm.phar"],
"pre-update-cmd": "composer run-script setup"
}
}Note: These scripts are necessary in order to install all the necessary tools in order to lint your files.
{
//...
"extra": {
"lint-staged": {
"**/*.php": [
"composer run-script lint"
]
}
}
}Note: This entry is necessary in order to tell the lint script which files and what commands it needs to run when performing a commit. The logic is pretty similar to
lint-stagednpm package. [reference]
It wil be necessary to remove your compose.lock file to force install the necessary dependencies and extra configuration.
> rm -f composer.lock && composer installRemoving the lock file will trigger the setup script and will run the post installation setup to copy relevant configuration and download the necessary utilities.
Note: if everything goes whell, you should notice
psalm.pharandphp-cs-fixer.pharutilities and their respective configuration files.
Make sure to check .php-cs-fixer.dist.php configuration to add/remove directories you might want to check, alongside with psalm.xml.
You can either run manually composer run-script lint to check and fix your entire directory or when performing a commit containing php files.
Case you are familiar with this strategy, you can change lint-staged entry on your configuration and add your conditions and what to execute. Just make sure to:
- Make sure to add the
extra.lint-stagedentry on your configuration and to create your own rules to execute. - You can skip adding
setupandpre-update-cmdscript entries. - Install the git
pre-commithook and make sure to call.config/lint-staged.phpscript. webmozart/globandnikic/iterpackages are still required in order to run the lint-staged script.
If you decided to use
psalmandphp-cs-fixer, make sure to add/edit.php-cs-fixer.dist.phpandpsalm.xmlfiles to check the rules and add/exclude directories.
We all have situations that we are working with "special" files/directories that would necessary to skip the lint validation.
In order to address this situation, the lint script will check if there is a .lingstaged-ignore (TODO: chose a better name) file and use it to check for these particular entries on the list of staged files.
Example:
.history
vendor
This entries will make sure to skip both .history and vendor directories.
Note: you won't be able to use patterns like a regular
.gitignorefile, such as negative lookup (!), just regular Ant-like globbing patterns (check Limitations).
Since this is a experiment, there is a considerable set of limitations to use this strategy, such as:
- Your local PHP setup: a configuration or a extension necessary could be disabled and your are not aware of it.
- The
lint-stagedentires: it needs to follow Ant-like globbing patterns, otherwise it may fail or produce wrong results. [reference] - The strategy used to ignore files using
lingstaged-ignoreis pretty simple. I didn't find any useful PHP package to act asgitdoes with.gitignore, neither to write my on implmementation of it. That is not the goal here. - Environment: as mentioned, it works only on *nix environments (WSL included).
- Utilities: Both
psalmandphp-cs-fixeras their own limitations and shenanigans. Configure it carefully.