├── README.md ├── composer.json ├── resources └── pre-commit ├── LICENSE.md └── src └── PhpcsPlugin.php /README.md: -------------------------------------------------------------------------------- 1 | # Composer plugin for code style checking 2 | 3 | ## Installation 4 | 5 | ``` 6 | composer require --dev wujunze/composer-phpcs-plugin 7 | ``` 8 | 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Composer plugin for code style checking before commit using php-cs-fixer", 3 | "name": "wujunze/composer-phpcs-plugin", 4 | "license": "MIT", 5 | "type": "composer-plugin", 6 | "require": { 7 | "composer-plugin-api": "^1.1", 8 | "friendsofphp/php-cs-fixer": "^2.15" 9 | }, 10 | "autoload": { 11 | "psr-4": { 12 | "leap\\composer\\": "src/" 13 | } 14 | }, 15 | "extra": { 16 | "class": "leap\\composer\\PhpcsPlugin" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /resources/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PHP_CS_FIXER="vendor/bin/php-cs-fixer" 4 | HAS_PHP_CS_FIXER=false 5 | 6 | if [ -x vendor/bin/php-cs-fixer ]; then 7 | HAS_PHP_CS_FIXER=true 8 | fi 9 | 10 | if $HAS_PHP_CS_FIXER; then 11 | git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do 12 | $PHP_CS_FIXER fix "$line"; 13 | git add "$line"; 14 | done 15 | else 16 | echo "" 17 | echo "php-cs-fixer was not found, please run:" 18 | echo "" 19 | echo " composer update --dev" 20 | echo "" 21 | fi 22 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Leap-Inc Ltd. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/PhpcsPlugin.php: -------------------------------------------------------------------------------- 1 | 'installGitPreHook', 21 | 'post-update-cmd' => 'installGitPreHook' 22 | ); 23 | } 24 | 25 | public function activate(Composer $composer, IOInterface $io) 26 | { 27 | $this->io = $io; 28 | } 29 | 30 | public function installGitPreHook() 31 | { 32 | $hooksDir = '.git/hooks'; 33 | if (!is_dir($hooksDir)) { 34 | $this->io->isVeryVerbose() && $this->io->writeError("No .git found, not in vcs?"); 35 | return; 36 | } 37 | if (file_exists($hookFile = $hooksDir . '/pre-commit')) { 38 | $this->io->isVeryVerbose() && $this->io->writeError("pre-commit hook file exists, skip"); 39 | return; 40 | } 41 | copy(__DIR__.'/../resources/pre-commit', $hookFile); 42 | chmod($hookFile, 0755); 43 | $this->io->write("Install git hook script $hookFile"); 44 | } 45 | } 46 | --------------------------------------------------------------------------------