├── composer.json ├── src ├── PreCommitServiceProvider.php └── commands │ └── PreCommit.php └── README.md /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zabaala/laravel_precommit", 3 | "description": "A git pre-commit hook to Laravel apps.", 4 | "type": "library", 5 | "require": { 6 | "php": ">=7.0.0", 7 | "phpunit/phpunit": "~6.0", 8 | "squizlabs/php_codesniffer": "^3.1" 9 | }, 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Mauricio Rodrigues", 14 | "email": "mmauricio.vsr@gmail.com" 15 | } 16 | ], 17 | "autoload": { 18 | "psr-4": { 19 | "Zabaala\\PreCommit\\": "src/" 20 | } 21 | }, 22 | "extra": { 23 | "laravel": { 24 | "providers": [ 25 | "Zabaala\\PreCommit\\PreCommitServiceProvider" 26 | ] 27 | } 28 | }, 29 | "minimum-stability": "dev" 30 | } 31 | -------------------------------------------------------------------------------- /src/PreCommitServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerCommand(); 25 | } 26 | 27 | /** 28 | * Register the git:pre-commit command. 29 | */ 30 | private function registerCommand() 31 | { 32 | $this->app->singleton('command.git.pre-commit', function ($app) { 33 | return new PreCommit(); 34 | }); 35 | 36 | $this->commands('command.git.pre-commit'); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel pre-commit 2 | 3 | A Laravel package to analyze your PHP code and check your PHPUnit tests before commit your code. 4 | 5 | ## Requirements 6 | 7 | - PHP >= 7.0.0 8 | - phpunit/phpunit ~6.0 9 | - squizlabs/php_codesniffer ~6.0 10 | 11 | ## How to install 12 | 13 | ### 1. Include package dependencie into composer.json: 14 | 15 | ``` 16 | composer require zabaala/laravel-pre-commit 17 | ``` 18 | ### 2. Discover the package Service Provider 19 | 20 | If you're using Laravel 5.5 version, this package will be auto-discovered. But if you're using any other Laravel 5 21 | version, you will need add the PreCommitServiceProvider inside of your `config/app.php` file: 22 | 23 | ```php 24 | // ... 25 | \Zabaala\PreCommit\PreCommitServiceProvider::class, 26 | ``` 27 | 28 | ### 3. Create the pre-commit file 29 | 30 | Create a pre-commit file in: `.git/hooks/pre-commit` with the content below: 31 | 32 | ``` 33 | #!/bin/bash 34 | 35 | ./artisan git:pre-commit 36 | ``` 37 | 38 | ## Usage 39 | 40 | Modify and commit any file. 41 | 42 | ## License 43 | MIT 44 | -------------------------------------------------------------------------------- /src/commands/PreCommit.php: -------------------------------------------------------------------------------- 1 | checkDependencies(); 54 | 55 | // extract PHP files... 56 | $this->extractFilesToBeAnalysed(); 57 | 58 | // Run code sniffer... 59 | $this->runCodeSniffer(); 60 | 61 | // Run Code Beautifier and Fixer... 62 | $this->runPHPCBF(); 63 | 64 | // Run PHPUnit 65 | $this->runPHPUnit(); 66 | 67 | if ($this->exitCode) { 68 | $this->output->error('Something is wrong. Check Code Sniffer and PHPUnit log.'); 69 | } else { 70 | $this->output->success('Yeah!! Everything is alright.'); 71 | } 72 | 73 | exit($this->exitCode); 74 | } 75 | 76 | /** 77 | * Check if dependencies exists. 78 | */ 79 | private function checkDependencies() 80 | { 81 | $installedPackages = []; 82 | 83 | exec("composer show -N", $installedPackages); 84 | $installedPackages = collect($installedPackages); 85 | 86 | $continue = $installedPackages->contains("phpunit/phpunit") && 87 | $installedPackages->contains("squizlabs/php_codesniffer"); 88 | 89 | if (! $continue) { 90 | $this->output->error('The packages PHPUnit and PHP_CodeSniffer wasn\'t found.'); 91 | exit(1); 92 | } 93 | } 94 | 95 | /** 96 | * Extract PHP files to be analysed from HEAD. 97 | */ 98 | private function extractFilesToBeAnalysed() 99 | { 100 | exec("git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php", $this->files); 101 | } 102 | 103 | /** 104 | * Run Code Sniffer to detect PSR2 code standard. 105 | */ 106 | private function runCodeSniffer() 107 | { 108 | $process = $this->process( 109 | "./vendor/bin/phpcs --standard=PSR2 --encoding=utf-8 -n -p " . implode(" ", $this->files) 110 | ); 111 | $this->exitCode = $process->getExitCode(); 112 | } 113 | 114 | /** 115 | * Run Code Beautifier and Fixer. 116 | */ 117 | private function runPHPCBF() 118 | { 119 | $process = $this->process( 120 | "./vendor/bin/phpcbf --standard=PSR2 --encoding=utf-8 " . implode(" ", $this->files) 121 | ); 122 | $this->exitCode = $process->getExitCode(); 123 | } 124 | 125 | /** 126 | * Run PHP Unit test. 127 | */ 128 | private function runPHPUnit() 129 | { 130 | $process = $this->process("./vendor/bin/phpunit"); 131 | $this->exitCode = $process->getExitCode(); 132 | } 133 | 134 | /** 135 | * @param $command 136 | * @return Process 137 | */ 138 | private function process($command) 139 | { 140 | $process = new Process($command); 141 | 142 | $process->run(function ($type, $line) { 143 | $this->output->write($line); 144 | }); 145 | 146 | return $process; 147 | } 148 | } 149 | --------------------------------------------------------------------------------