|
1 | 1 | # Mollie PHP Coding Standards |
2 | | - |
3 | 2 | Contains PHP coding standards like rules for PHP-CS-Fixer that serves for purpose of standardization. |
| 3 | + |
| 4 | +## Usage |
| 5 | +This package makes use of PHP-CS-Fixer. |
| 6 | + |
| 7 | +### Already familiar with PHP-CS-Fixer |
| 8 | + |
| 9 | +This package provides default rules to be used with PHP-CS-Fixer. |
| 10 | + |
| 11 | +You can find them in `Mollie\PhpCodingStandards\PhpCsFixer\Rules` which has methods specific to php version, |
| 12 | +which you can directly use in the `->setRules()` part of your config. For example, assuming PHP version 7.3: |
| 13 | + |
| 14 | +```php |
| 15 | +use Mollie\PhpCodingStandards\PhpCsFixer\Rules; |
| 16 | + |
| 17 | +$config->setRules(Rules::getForPhp73()); |
| 18 | +``` |
| 19 | + |
| 20 | +### New to PHP-CS-Fixer |
| 21 | + |
| 22 | +Place a file named `.php_cs.dist` that has following content in your project's root directory. |
| 23 | + |
| 24 | +```php |
| 25 | +use Mollie\PhpCodingStandards\PhpCsFixer\Rules; |
| 26 | +use PhpCsFixer\Config; |
| 27 | +use PhpCsFixer\Finder; |
| 28 | + |
| 29 | +$finder = Finder::create() |
| 30 | + ->name('.php_cs.dist') // Fix this file as well |
| 31 | + ->in(__DIR__); |
| 32 | + |
| 33 | +return Config::create() |
| 34 | + ->setFinder($finder) |
| 35 | + ->setRiskyAllowed(true) |
| 36 | + // use specific rules for your php version e.g.: getForPhp71, getForPhp72, getForPhp73 |
| 37 | + ->setRules(Rules::getForPhp71()); |
| 38 | +``` |
| 39 | + |
| 40 | +### Manual Triggering |
| 41 | +Run following command in your project directory, that will run fixer for every `.php` file. |
| 42 | +```bash |
| 43 | +vendor/bin/php-cs-fixer fix |
| 44 | +``` |
| 45 | + |
| 46 | +### Use via PhpStorm file watcher |
| 47 | +Please follow [official PhpStorm documentation](https://www.jetbrains.com/help/phpstorm/using-php-cs-fixer.html#f21a70ca) |
| 48 | + |
| 49 | +### Use via pre-commit git hook |
| 50 | +Place a file with the content of the following bash script into `.git/hooks` directory called pre-commit and make it executable |
| 51 | +you can find more details about git hooks on [official git manual](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). |
| 52 | + |
| 53 | +```bash |
| 54 | +#!/usr/bin/env bash |
| 55 | + |
| 56 | +EXCLUDE_DIRECTORIES_REGEX='^(directory_one/(sub_directory_one|sub_directory_two)|directory_two)/.*' |
| 57 | +git diff --diff-filter=ACMRTUXB --name-only --staged | grep -E '\.php(_cs\.dist)?$' | grep -vE $EXCLUDE_DIRECTORIES_REGEX | while read FILE; do |
| 58 | + STATUS="$(git status --porcelain ${FILE} | cut -c 1-2)" |
| 59 | + vendor/bin/php-cs-fixer fix --using-cache=no --quiet --dry-run ${FILE} |
| 60 | + |
| 61 | + # Not 0 means php-cs-fixer either errored or wanted to make changes |
| 62 | + if [ $? != 0 ] |
| 63 | + then |
| 64 | + # MM = staged & non-staged modification in same file |
| 65 | + if [ ${STATUS} = "MM" ] |
| 66 | + then |
| 67 | + echo -e "\033[31m┌────────────────────────────────────────────────────────────────────────────────┐" |
| 68 | + echo -e "│ Failure:\033[39m Codestyle violation(s) in file with both staged and unstaged changes. \033[31m│" |
| 69 | + echo -e "├────────────────────────────────────────────────────────────────────────────────┘" |
| 70 | + echo -e "└\033[33m File:\033[39m $FILE" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + vendor/bin/php-cs-fixer fix --using-cache=no --quiet ${FILE} |
| 75 | + git add ${FILE} |
| 76 | + fi |
| 77 | +done |
| 78 | +``` |
| 79 | + |
| 80 | +## Installation |
| 81 | +```bash |
| 82 | +composer require --dev mollie/php-coding-standards |
| 83 | +``` |
| 84 | + |
| 85 | +## Working at Mollie |
| 86 | +Mollie is always looking for new talent to join our teams. We’re looking for inquisitive minds with good ideas and |
| 87 | +strong opinions, and, most importantly, who know how to ship great products. Want to join the future of payments? |
| 88 | +[Check out our vacancies](https://jobs.mollie.com). |
| 89 | + |
| 90 | +## License |
| 91 | +[BSD (Berkeley Software Distribution) License](https://opensource.org/licenses/bsd-license.php). |
| 92 | +Copyright (c) 2013-2019, Mollie B.V. |
0 commit comments