Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/config #3

Merged
merged 28 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
823868a
(chore): remove all WP and Laravel related files and deps, re-init pest
FreakyWizard Nov 14, 2024
75346f0
(chore): create interface, init Config class
FreakyWizard Nov 14, 2024
ce036c2
(chore): start working on tests
FreakyWizard Nov 14, 2024
53e96bb
(chore): installed library for deepmerge, finished tests
FreakyWizard Nov 14, 2024
6f77fa4
(chore): added 14 tests
FreakyWizard Nov 14, 2024
11768ab
(chore): php cs fixer use config, update docs, run cs fixer
FreakyWizard Nov 14, 2024
9c3d588
(chore): delete old classes
FreakyWizard Nov 14, 2024
07cf510
(chore): composer lock
FreakyWizard Nov 14, 2024
3a6cc0d
(chore): right php version, update deps
FreakyWizard Nov 15, 2024
f032277
(refactor): move doc blocks to interface, static to self
FreakyWizard Nov 15, 2024
15d3b0f
(refactor): run format
FreakyWizard Nov 15, 2024
b4c2804
(refactor): markdown
FreakyWizard Nov 15, 2024
e33bad2
(refactor): remove yard
FreakyWizard Nov 15, 2024
a48b791
(chore): new run test settings
FreakyWizard Nov 15, 2024
654257c
(chore): remove wp fix
FreakyWizard Nov 15, 2024
f55a516
(chore): tabs vs spaces!
FreakyWizard Nov 15, 2024
8eea660
(chore): empty space
FreakyWizard Nov 15, 2024
ed513ce
(chore): new format workflow
FreakyWizard Nov 15, 2024
c3960c7
(refactor): ; location, new tests
FreakyWizard Nov 15, 2024
3f57514
(refactor): create config and helper
FreakyWizard Nov 15, 2024
d990c13
(chore): added config, added new methode, added tests, made shortcut
FreakyWizard Nov 15, 2024
1bd3018
(chore): format and lint
FreakyWizard Nov 15, 2024
ad0e4a8
(chore): rename settings to rules
FreakyWizard Nov 15, 2024
c38fc88
(refactor): interface, function name
FreakyWizard Nov 15, 2024
7e47163
(chore): expanded README.md
FreakyWizard Nov 15, 2024
bf3c9ff
(chore): replace methode for method
FreakyWizard Nov 15, 2024
a820048
(chore): backtics
FreakyWizard Nov 15, 2024
59fdbcc
(chore): add beforeEach to tests
FreakyWizard Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ indent_size = 2

[*.md]
trim_trailing_whitespace = false
indent_style = space

[{*.txt,wp-config-sample.php}]
end_of_line = crlf
Expand Down
43 changes: 28 additions & 15 deletions .github/workflows/format-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
3 changes: 0 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 10 additions & 47 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 2 additions & 57 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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();
168 changes: 157 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# 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)
[![Tests](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/run-tests.yml/badge.svg?no-cache)](https://github.com/yardinternet/php-cs-fixer-rules/actions/workflows/run-tests.yml)
[![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

Expand All @@ -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` methode.

```php
<?php

Expand All @@ -42,13 +47,154 @@ $finder = Finder::create()
->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 methodes

[Yard\PhpCsFixerRules\Config](src/Config.php) extends the PHP CS Fixer config object so all native methodes are
available. Note that the native PHP CS Fixer methodes 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 methode
->mergeRules([ // Yard\PhpCsFixerRules\Config methode
'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` methode.
In normal use cases there is no need to call this methode.
Loading
Loading