N°8796 - Add pre-commit hooks for auto PHP code style fixing#780
N°8796 - Add pre-commit hooks for auto PHP code style fixing#780Molkobain wants to merge 3 commits intosupport/3.2from
Conversation
|
Works on Linux with a PHP not located at default location |
|
This won't be done that way. Instead we choose to configure PHPStorm for each projet so it reformats code in all repositories, not only the iTop one. |
The contributing guide asks external contributors to follow the PHP coding standards but offers no local tooling to help. Relying on PHPStorm configuration won't help contributors using other editors. They'll only find out about style issues when CI fails, leading to the exact "fix style" commits this PR was trying to avoid. I don't think the two approaches are mutually exclusive. The PHPStorm config for the internal team and pre-commit hooks as an IDE-agnostic safety net could co-exist. Might be worth reconsidering, at least as an optional setup for non-PHPStorm users. |
Thanks for pointing this out @hakonharnes ! I don't think we'll keep the pre-commit approach as it required to include unnecessary third-party libs. But we can update the contributing guide to point to both static analysis and code style READMEs tools as they contain the instructions to run both tools locally. What do you think? |
|
I think that'd be a logical improvement :) |
|
Done, I've added a "Developers" section in the main README.md :) |

Base information
Objective (enhancement)
In a previous PR, we introduced the PHP code style tool (PHP CS Fixer), a re-formatting of the code base and a comformity check in the CI.
This PR aims at easing developer's experience by automatically running the PHP code style fixer on files about to be committed. Avoiding to manually launch the tool or to make a "Fix CI" commit after pushing non-comform code.
Proposed solution
What has been done:
pre-commithook to fix code style of staged PHP files<ITOP>/captainhook.json?pre-commithook it would either:check, block the commit, which was good but forced the developer to manually run PHP-CS-Fixer, then re-commit => Not OKfix, commit files anyway, then fix them for a second commit => Not OKfixcommand on files, then re-stage them for the current commit, which makes it a transparent operation for the developerHow to use it:
<ITOP>/tests/php-code-stylefoldercomposer install<ITOP>folder<ITOP>/captainhook.config.json.distinto<ITOP>/captainhook.config.jsonphp-pathline and adjust it to your environement (It's important to change this BEFORE running the next command, otherwise the Git hook will be installed with an incorrect path to PHP)composer installChecklist before requesting a review
Things to keep in mind
require-devjust like for some other dependencies (e.g. "web profiler"), it should be up to the Factory to clean these "dev dependencies" when building the final package.