diff --git a/.editorconfig b/.editorconfig index db09468..093e60e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,6 +18,7 @@ indent_size = 2 [*.md] trim_trailing_whitespace = false +indent_style = space [{*.txt,wp-config-sample.php}] end_of_line = crlf diff --git a/.github/workflows/format-php.yml b/.github/workflows/format-php.yml index bb9b7ad..16b1a38 100644 --- a/.github/workflows/format-php.yml +++ b/.github/workflows/format-php.yml @@ -6,20 +6,33 @@ on: - '**.php' jobs: - php-cs-fixer: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.head_ref }} - fetch-depth: 0 + php-cs-fixer: + name: php-cs-fixer + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 - - name: Run php-cs-fixer - uses: docker://oskarstark/php-cs-fixer-ga + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + tools: composer:v2 + coverage: none + env: + COMPOSER_TOKEN: ${{ secrets.YARD_BOT_PAT }} - - uses: stefanzweifel/git-auto-commit-action@v5 - with: - commit_message: '(auto): apply php-cs-fixer changes' - push_options: '--force' - env: - GITHUB_TOKEN: ${{ secrets.YARD_BOT_PAT }} + - name: Install composer dependencies + run: composer install --prefer-dist --no-interaction + + - name: Run PHPStan + run: ./vendor/bin/php-cs-fixer fix + + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: '(auto): apply php-cs-fixer changes' + push_options: '--force' + env: + GITHUB_TOKEN: ${{ secrets.YARD_BOT_PAT }} diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 859d85e..b8cdd46 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -29,8 +29,5 @@ jobs: - name: Install composer dependencies run: composer install --prefer-dist --no-interaction - - name: Dirty fix for previously defined function - run: sed -i -e 's#function __(#function ____(#' ./vendor/php-stubs/wordpress-stubs/wordpress-stubs.php - - name: Run PHPStan run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b605b44..3edfb4e 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -11,59 +11,22 @@ on: workflow_dispatch: jobs: - test: - runs-on: ${{ matrix.os }} - timeout-minutes: 5 - strategy: - fail-fast: true - matrix: - os: [ubuntu-latest] - php: [8.1, 8.2] - laravel: [10.*] - include: - - laravel: 10.* - testbench: 8.* - - name: PHP${{ matrix.php }} - Laravel${{ matrix.laravel }} - - permissions: - contents: read - pull-requests: write + ci: + runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 + - name: Checkout + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, xdebug - coverage: xdebug + php-version: 8.1 tools: composer:v2 - env: - COMPOSER_TOKEN: ${{ secrets.YARD_BOT_PAT }} - - - name: Setup problem matchers - run: | - echo "::add-matcher::${{ runner.tool_cache }}/php.json" - echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Install dependencies - run: | - composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --prefer-dist --no-interaction - - - name: List Installed Dependencies - run: composer show -D + coverage: xdebug - - name: Execute tests - run: vendor/bin/pest --coverage-clover=coverage.xml + - name: Install Dependencies + run: composer install --no-interaction --prefer-dist --optimize-autoloader - - name: Coverage Report as Comment (Clover) - if: github.event_name == 'pull_request' - uses: lucassabreu/comment-coverage-clover@main - with: - file: coverage.xml + - name: Tests + run: ./vendor/bin/pest --ci diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 946e1bf..23d73ff 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -2,7 +2,6 @@ declare(strict_types=1); -use PhpCsFixer\Config; use PhpCsFixer\Finder; # PHP CS Fixer can be run by using the composer script `composer format` @@ -15,60 +14,6 @@ ->ignoreVCS(true) ->exclude('public') ->exclude('node_modules') - ->exclude('build') -; + ->exclude('build'); -return (new Config) - ->setFinder($finder) - ->setRules([ - '@PSR2' => true, - 'indentation_type' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha', - ], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, - 'logical_operators' => true, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'phpdoc_var_without_name' => true, - 'phpdoc_single_line_var_spacing' => true, - 'unary_operator_spaces' => true, - 'phpdoc_trim' => true, - 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'align_multiline_comment' => true, - 'array_indentation' => true, - 'no_superfluous_elseif' => true, - 'single_blank_line_before_namespace' => true, - 'blank_line_after_opening_tag' => true, - 'no_blank_lines_after_phpdoc' => true, - 'phpdoc_separation' => true, - 'method_chaining_indentation' => true, - 'binary_operator_spaces' => [ - 'default' => 'single_space', - 'operators' => [ - '=>' => null, - '|' => 'no_space', - ], - ], - 'return_type_declaration' => [ - 'space_before' => 'none', - ], - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'full_opening_tag' => true, - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'yoda_style' => [ - 'always_move_variable' => true, - 'equal' => true, - 'identical' => true, - 'less_and_greater' => true, - ], - 'declare_strict_types' => true, - ]) - ->setLineEnding("\n") - ->setRiskyAllowed(true); +return \Yard\PhpCsFixerRules\Config::create($finder)->setRiskyAllowed(false)->setDefaultRules(); diff --git a/README.md b/README.md index b60f0f2..a6d40ae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Yard php-cs-fixer-rules +# php-cs-fixer-rules [![Code Style](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/format-php.yml/badge.svg?no-cache)](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/format-php.yml) [![PHPStan](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/phpstan.yml/badge.svg?no-cache)](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/phpstan.yml) @@ -6,7 +6,8 @@ [![Code Coverage Badge](https://github.com/yardinternet/php-cs-fixer-rules/blob/badges/coverage.svg)](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/badges.yml) [![Lines of Code Badge](https://github.com/yardinternet/php-cs-fixer-rules/blob/badges/lines-of-code.svg)](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/badges.yml) -Enables you to easily import the Yard PHP CS Fixer rules. +PHP CS Fixer rules used within the WordPress team for sites and packages. +This package centralizes formatting settings and allows for easy configuration of PHP CS Fixer. ## Installation @@ -29,6 +30,10 @@ To install this package using Composer, follow these steps: ## Usage +This package enhances the `PhpCsFixer\Config` class (see the [ConfigInterface](src/Interfaces/ConfigInterface.php)). One +can create a new configuration object by calling the static +`create(Finder $finder, string $name = 'default'): self` method. + ```php ignoreVCS(true) ->exclude('public') ->exclude('node_modules') - ->exclude('build') -; - -return \Yard\PhpCsFixerRules\Config::setFinder($finder) - ->mergeRules([ // allows you to add new rules or override rules from default - 'declare_strict_types' => false, - ]) - ->setRiskyAllowed(false) // override and disable risky setting for old projects - ->get(); + ->exclude('build'); + +return \Yard\PhpCsFixerRules\Config::create($finder); +``` + +### Removing rules + +`removeRules(array $rulesKeys): self` or `removeRule(string $ruleKey): self` allows you to remove rules. + +```php + +/** + * Default rules: + * + * [ + * 'method_chaining_indentation' => true, + * 'yoda_style' => [ + * 'always_move_variable' => true, + * 'equal' => true, + * 'identical' => true, + * 'less_and_greater' => true, + * ], + * 'binary_operator_spaces' => [ + * 'default' => 'single_space', + * 'operators' => [ + * '=>' => null, + * '|' => 'no_space', + * ], + * ], + * ] + */ + +$config = \Yard\PhpCsFixerRules\Config::create($finder) + ->removeRules(['yoda_style', 'binary_operator_spaces']); + +/** + * Expected rule set: + * + * [ + * 'method_chaining_indentation' => true, + * ] + */ + + +return $config->removeRule('method_chaining_indentation'); + +/** + * Expected rule set: [] + */ ``` + +### Add and override rules + +`mergeRules(array $rules): self` allows you to add and override rules. + +```php + +/** + * Default rules: + * + * [ + * 'yoda_style' => [ + * 'always_move_variable' => true, + * 'equal' => true, + * 'identical' => true, + * 'less_and_greater' => true, + * ], + * 'binary_operator_spaces' => [ + * 'default' => 'single_space', + * 'operators' => [ + * '=>' => null, + * '|' => 'no_space', + * ], + * ], + * ] + */ + +return \Yard\PhpCsFixerRules\Config::create($finder) + ->mergeRules([ + 'yoda_style' => [ + 'equal' => false, + ], + 'binary_operator_spaces' => [ + 'operators' => [ + '|' => 'single_space', + '<>' => null, + ] + ] + ]); + +/** + * Expected rule set: + * + * [ + * 'yoda_style' => [ + * 'always_move_variable' => true, + * 'equal' => false, // this setting changed! + * 'identical' => true, + * 'less_and_greater' => true, + * ], + * 'binary_operator_spaces' => [ + * 'default' => 'single_space', + * 'operators' => [ + * '=>' => null, + * '|' => 'single_space', // this setting changed! + * '<>' => null, // this setting was added! + * ], + * ], + * ] + */ + +``` + +### Calling native PHP CS Fixer config methods + +[Yard\PhpCsFixerRules\Config](src/Config.php) extends the PHP CS Fixer config object so all native methods are +available. Note that the native PHP CS Fixer methods return +a [PhpCsFixer\ConfigInterface](./vendor/friendsofphp/php-cs-fixer/src/ConfigInterface.php) type (instead +of [Yard\PhpCsFixerRules\Interfaces\ConfigInterface](src/Interfaces/ConfigInterface.php)). +Your linter may not like this. + +```php + +return \Yard\PhpCsFixerRules\Config::create($finder) + ->setRiskyAllowed(false) // native PHP CS Fixer method + ->mergeRules([ // Yard\PhpCsFixerRules\Config method + 'yoda_style' => [ + 'equal' => false, + ], + 'binary_operator_spaces' => [ + 'operators' => [ + '|' => 'single_space', + '<>' => null, + ] + ] + ]); +``` + +## Older projects and risky fixers + +`RiskyAllowed` is set to [true](config/rules.php) by default. In older projects you may need to disable it. + +```php +return \Yard\PhpCsFixerRules\Config::create($finder) + ->setRiskyAllowed(false); +``` + +### setDefaultRules() + +`setDefaultRules(): self` gets called by the static `create(Finder $finder, string $name = 'default'): self` method. +In normal use cases there is no need to call this method. diff --git a/composer.json b/composer.json index ac2f8ea..24dbea9 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ }, "require": { "php": "8.1.*", - "ckr/arraymerger": "^3.0" + "ckr/arraymerger": "^3.0", + "webmozart/assert": "^1.11" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.64", diff --git a/composer.lock b/composer.lock index 7691d24..b29d021 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "942fd7bb3614069dd173b436cb8314f6", + "content-hash": "241088b7ff81996d30967d7871d755b0", "packages": [ { "name": "ckr/arraymerger", @@ -48,6 +48,64 @@ "source": "https://github.com/ckressibucher/array_merger/tree/3.0.0" }, "time": "2022-04-23T20:26:40+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.11.0", + "source": { + "type": "git", + "url": "https://github.com/webmozarts/assert.git", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "php": "^7.2 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<4.6.1 || 4.6.2" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.13" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "support": { + "issues": "https://github.com/webmozarts/assert/issues", + "source": "https://github.com/webmozarts/assert/tree/1.11.0" + }, + "time": "2022-06-03T18:03:27+00:00" } ], "packages-dev": [ @@ -5180,64 +5238,6 @@ } ], "time": "2024-03-03T12:36:25+00:00" - }, - { - "name": "webmozart/assert", - "version": "1.11.0", - "source": { - "type": "git", - "url": "https://github.com/webmozarts/assert.git", - "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", - "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", - "shasum": "" - }, - "require": { - "ext-ctype": "*", - "php": "^7.2 || ^8.0" - }, - "conflict": { - "phpstan/phpstan": "<0.12.20", - "vimeo/psalm": "<4.6.1 || 4.6.2" - }, - "require-dev": { - "phpunit/phpunit": "^8.5.13" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.10-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "support": { - "issues": "https://github.com/webmozarts/assert/issues", - "source": "https://github.com/webmozarts/assert/tree/1.11.0" - }, - "time": "2022-06-03T18:03:27+00:00" } ], "aliases": [], diff --git a/config/rules.php b/config/rules.php new file mode 100644 index 0000000..cb34d35 --- /dev/null +++ b/config/rules.php @@ -0,0 +1,57 @@ + "\n", + 'risky_allowed' => true, + 'rules' => [ + '@PSR2' => true, + 'indentation_type' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'logical_operators' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'phpdoc_var_without_name' => true, + 'phpdoc_single_line_var_spacing' => true, + 'unary_operator_spaces' => true, + 'phpdoc_trim' => true, + 'phpdoc_trim_consecutive_blank_line_separation' => true, + 'align_multiline_comment' => true, + 'array_indentation' => true, + 'no_superfluous_elseif' => true, + 'single_blank_line_before_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'no_blank_lines_after_phpdoc' => true, + 'phpdoc_separation' => true, + 'method_chaining_indentation' => true, + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ], + ], + 'return_type_declaration' => [ + 'space_before' => 'none', + ], + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'full_opening_tag' => true, + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ], + 'yoda_style' => [ + 'always_move_variable' => true, + 'equal' => true, + 'identical' => true, + 'less_and_greater' => true, + ], + 'declare_strict_types' => true, + ], +]; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bd749f2..8fd7519 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,38 +1,37 @@ - - - tests - - - - - - - - - - - - - - - ./src - - + + + tests + + + + + + + + + + + + + + + ./src + + diff --git a/src/Config.php b/src/Config.php index b8b2582..dd94029 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,7 +4,57 @@ namespace Yard\PhpCsFixerRules; -class Config +use PhpCsFixer\Finder; +use Yard\PhpCsFixerRules\Interfaces\ConfigInterface; +use Yard\PhpCsFixerRules\Traits\Helpers; + +class Config extends \PhpCsFixer\Config implements ConfigInterface { + use Helpers; + + private function __construct(string $name) + { + parent::__construct($name); + } + + public static function create(Finder $finder, string $name = 'default'): self + { + $config = new self($name); + + $config->setFinder($finder); + $config->setDefaultRules(); + + return $config; + } + + public function setDefaultRules(): self + { + + $this->setRules($this->configRule('rules', [])) + ->setLineEnding($this->configRule('line_ending', "\n")) + ->setRiskyAllowed($this->configRule('risky_allowed', true)); + + return $this; + } + + public function mergeRules(array $rules): self + { + $this->setRules(\Ckr\Util\ArrayMerger::doMerge($this->getRules(), $rules)); + + return $this; + } + + public function removeRules(array $rulesKeys): self + { + $rules = $this->getRules(); + + $this->setRules(array_diff_key($rules, array_flip($rulesKeys))); + + return $this; + } + public function removeRule(string $ruleKey): self + { + return $this->removeRules([$ruleKey]); + } } diff --git a/src/Interfaces/ConfigInterface.php b/src/Interfaces/ConfigInterface.php new file mode 100644 index 0000000..644c9ec --- /dev/null +++ b/src/Interfaces/ConfigInterface.php @@ -0,0 +1,40 @@ +|bool> $rules + */ + public function mergeRules(array $rules): self; + + /** + * Unset matching rules + * + * @param array $rulesKeys + */ + public function removeRules(array $rulesKeys): self; + + /** + * Removes rule name + */ + public function removeRule(string $ruleKey): self; +} diff --git a/src/Traits/Helpers.php b/src/Traits/Helpers.php new file mode 100644 index 0000000..fc4fe68 --- /dev/null +++ b/src/Traits/Helpers.php @@ -0,0 +1,37 @@ +config('rules', $key, $default); + } +} diff --git a/tests/Feature/ConfigTest.php b/tests/Feature/ConfigTest.php new file mode 100644 index 0000000..576cab5 --- /dev/null +++ b/tests/Feature/ConfigTest.php @@ -0,0 +1,61 @@ +config = Config::create(Finder::create()); +}); + +it('can create config', function () { + expect($this->config)->toBeInstanceOf(ConfigInterface::class); +}); + +it('sets rules on create', function () { + expect($this->config->getRules())->not->toBeEmpty(); +}); + +it('sets line ending on create', function () { + expect($this->config->getLineEnding())->not->toBeEmpty(); +}); + +it('sets risky allowed on create', function () { + expect($this->config->getRiskyAllowed())->not->toBeEmpty(); +}); + +it('can reset to default rules using setDefaultRules()', function () { + $defaultRules = $this->config->getRules(); + $defaultLineEnding = $this->config->getLineEnding(); + $defaultRiskyAllowed = $this->config->getRiskyAllowed(); + + $this->config->setRules([]) + ->setLineEnding("\t\n") + ->setRiskyAllowed(false); + + $this->config->setDefaultRules(); + + expect($this->config->getRules())->toBe($defaultRules); + expect($this->config->getLineEnding())->toBe($defaultLineEnding); + expect($this->config->getRiskyAllowed())->toBe($defaultRiskyAllowed); +}); + +it('can override all rules', function () { + $this->config->setRules([]); + + expect($this->config->getRules())->toBeEmpty(); +}); + +it('can override line ending', function () { + $this->config->setLineEnding("\t\n"); + + expect($this->config->getLineEnding())->toBe("\t\n"); +}); + +it('can override risky allowed', function () { + $this->config->setRiskyAllowed(false); + + expect($this->config->getRiskyAllowed())->toBe(false); +}); diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 801bbce..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,7 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Pest.php b/tests/Pest.php index 4e6310e..174d7fd 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,47 +1,3 @@ extend(Tests\TestCase::class)->in('Feature'); - -/* -|-------------------------------------------------------------------------- -| Expectations -|-------------------------------------------------------------------------- -| -| When you're writing tests, you often need to check that values meet certain conditions. The -| "expect()" function gives you access to a set of "expectations" methods that you can use -| to assert different things. Of course, you may extend the Expectation API at any time. -| -*/ - -expect()->extend('toBeOne', function () { - return $this->toBe(1); -}); - -/* -|-------------------------------------------------------------------------- -| Functions -|-------------------------------------------------------------------------- -| -| While Pest is very powerful out-of-the-box, you may have some testing code specific to your -| project that you don't want to repeat in every file. Here you can also expose helpers as -| global functions to help you to reduce the number of lines of code in your test files. -| -*/ - -function something() -{ - // .. -} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index eccf277..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,12 +0,0 @@ -config = Config::create(Finder::create()); +}); + +it('can create config', function () { + expect($this->config)->toBeInstanceOf(ConfigInterface::class); +}); + +it('can merge rules', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->mergeRules([ + 'ordered_imports' => ['case_sensitive' => false], + 'array_syntax' => ['syntax' => 'short'], + 'phpdoc_scalar' => true, + ]); + + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => [ + 'sort_algorithm' => 'alpha', + 'case_sensitive' => false, + ], + 'array_syntax' => ['syntax' => 'short'], + 'phpdoc_scalar' => true, + ]); +}); + +it('can override rules', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ])->mergeRules([ + 'indentation_type' => false, + 'ordered_imports' => ['case_sensitive' => true], + ]); + + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => false, + 'ordered_imports' => ['case_sensitive' => true], + ]); +}); + +it('can merge and override', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->mergeRules([ + '@PSR2' => false, + 'ordered_imports' => ['case_sensitive' => false], + 'phpdoc_scalar' => true, + ]); + + expect($this->config->getRules())->toBe([ + '@PSR2' => false, + 'indentation_type' => true, + 'ordered_imports' => [ + 'sort_algorithm' => 'alpha', + 'case_sensitive' => false, + ], + 'phpdoc_scalar' => true, + ]); +}); + +it('can merge and override deep nested arrays', function () { + $this->config->setRules([ // reset rules list + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ], + ], + ])->mergeRules([ + 'binary_operator_spaces' => [ + 'default' => 'align', + 'operators' => [ + '|' => 'single_space', + '<>' => null, + ], + ], + ]); + + expect($this->config->getRules())->toBe([ + 'binary_operator_spaces' => [ + 'default' => 'align', + 'operators' => [ + '=>' => null, + '|' => 'single_space', + '<>' => null, + ], + ], + ]); +}); + +it('can merge empty arrays', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ])->mergeRules([]); + + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ]); +}); diff --git a/tests/Unit/ConfigRulesRemoveTest.php b/tests/Unit/ConfigRulesRemoveTest.php new file mode 100644 index 0000000..e37d09f --- /dev/null +++ b/tests/Unit/ConfigRulesRemoveTest.php @@ -0,0 +1,64 @@ +config = Config::create(Finder::create()); +}); + +it('can remove rules', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules([ + "@PSR2", "ordered_imports", + ]); + + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + ]); +}); + +it('can handle an request to remove nothing', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules([]); + + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); +}); + +it('can remove one rule', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRule("@PSR2"); + + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); +}); + +it('can remove a list with one rule', function () { + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules(["@PSR2"]); + + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); +}); diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index 801bbce..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,7 +0,0 @@ -toBeTrue(); -});