Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wayofdev/php-cs-fixer-config

Package adds custom rule-sets to php-cs-fixer.
https://github.com/wayofdev/php-cs-fixer-config

code-standards code-style php php-cs-fixer static-analysis

Last synced: about 1 month ago
JSON representation

Package adds custom rule-sets to php-cs-fixer.

Awesome Lists containing this project

README

        







WayOfDev Logo





Build

Build Status



Project

Total Downloads
Latest Stable Version
Commits since latest release
PHP Version Require



Quality

Codecov
Mutation testing badge
PHP Stan Level 9 of 9



Community

Discord
Follow on Twitter (X)


# PHP CS Fixer Config

Wrapper with pre-defined rules around the [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) package โ€” A tool to automatically fix PHP Coding Standards issues.

This repository aims to provide a standardized way to apply coding standards across multiple projects, ensuring consistency and adherence to best practices. By using predefined rulesets, it simplifies the setup process and allows teams to quickly integrate PHP-CS-Fixer into their development workflow.


If you **like/use** this package, please consider โญ๏ธ **starring** it. Thanks!


## ๐Ÿ“œ Custom Rulesets

**```WayOfDev\PhpCsFixer\Config\RuleSets\DefaultRuleset::class```**

Based on **`@Symfony`** ruleset

* Used by [`@wayofdev`](https://github.com/wayofdev) organization

**`WayOfDev\PhpCsFixer\Config\RuleSets\ExtendedPERSet::class`**

Based on **`@PER-CS2.0`** ruleset

* Used by [`@buggregator`](https://github.com/buggregator) and [`@cycle`](http://github.com/cycle) organizations


## ๐Ÿ’ฟ Installation

### โ†’ Using composer

Require as dependency:

```bash
composer req --dev wayofdev/cs-fixer-config
```


## ๐Ÿ›  Configuration

### โ†’ Setup

* Create PHP file and name it `.php-cs-fixer.dist.php` and place it inside root directory of project. It will be recognized by PHP CS Fixer automatically.

* Example contents of `.php-cs-fixer.dist.php` file:

```php
inDir(__DIR__ . '/src')
->inDir(__DIR__ . '/tests')
->addFiles([__FILE__])
->getConfig()
;

$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/php-cs-fixer.cache');

return $config;
```

### โ†’ Composer Script

* Add `scripts` section to `composer.json`:

```diff
{
"scripts": {
+ "cs:diff": "php vendor/bin/php-cs-fixer fix --dry-run -v --diff",
+ "cs:fix": "php vendor/bin/php-cs-fixer fix -v"
}
}
```

### โ†’ Git

* Place `.build` folder file into `.gitignore`

```diff
+/.build/
/vendor/
```

### โ†’ Makefile

* If you are using [`Makefile`](https://www.gnu.org/software/make/manual/make.html#Introduction), create a `Makefile` with a `lint-php` and `lint-diff` targets:

```diff
+APP_RUNNER ?= php
+APP_COMPOSER ?= $(APP_RUNNER) composer
+
+prepare:
+ mkdir -p .build/php-cs-fixer
+.PHONY: prepare

+lint-php: prepare ## Fixes code to follow coding standards using php-cs-fixer
+ $(APP_COMPOSER) cs:fix
+.PHONY: lint-php

+lint-diff: prepare ## Runs php-cs-fixer in dry-run mode and shows diff which will by applied
+ $(APP_COMPOSER) cs:diff
+.PHONY: lint-diff
```

Or, you can check for one of our pre-configured `Makefile` from any of these repositories:

### โ†’ GitHub Actions

* To use this package in [GitHub Actions](https://github.com/features/actions), add a `coding-standards.yml` workflow to your repository:

```yaml
---

on: # yamllint disable-line rule:truthy
pull_request:
branches:
- master
push:
branches:
- master

name: ๐Ÿงน Fix PHP coding standards

jobs:
coding-standards:
timeout-minutes: 4
runs-on: ${{ matrix.os }}
concurrency:
cancel-in-progress: true
group: coding-standards-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
strategy:
matrix:
os:
- ubuntu-latest
php-version:
- '8.1'
dependencies:
- locked
permissions:
contents: write
steps:
- name: โš™๏ธ Set git to use LF line endings
run: |
git config --global core.autocrlf false
git config --global core.eol lf

- name: ๐Ÿ› ๏ธ Setup PHP
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php-version }}
extensions: none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter
ini-values: error_reporting=E_ALL
coverage: none

- name: ๐Ÿ“ฆ Check out the codebase
uses: actions/[email protected]

- name: ๐Ÿ› ๏ธ Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: ๐Ÿค– Validate composer.json and composer.lock
run: composer validate --ansi --strict

- name: ๐Ÿ” Get composer cache directory
uses: wayofdev/gh-actions/actions/composer/[email protected]

- name: โ™ป๏ธ Restore cached dependencies installed with composer
uses: actions/[email protected]
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-

- name: ๐Ÿ“ฅ Install "${{ matrix.dependencies }}" dependencies with composer
uses: wayofdev/gh-actions/actions/composer/[email protected]
with:
dependencies: ${{ matrix.dependencies }}

- name: ๐Ÿ› ๏ธ Prepare environment
run: make prepare

- name: ๐Ÿšจ Run coding standards task
run: composer cs:fix
env:
PHP_CS_FIXER_IGNORE_ENV: true

- name: ๐Ÿ“ค Commit and push changed files back to GitHub
uses: stefanzweifel/[email protected]
with:
commit_message: 'style(php-cs-fixer): lint php files and fix coding standards'
branch: ${{ github.head_ref }}
commit_author: 'github-actions '
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

Or, you can check for one of our pre-configured workflows from any of these repositories:


## ๐Ÿ’ป Usage

Fix coding standards by simply running console command:

### โ†’ Directly

```bash
vendor/bin/php-cs-fixer fix -v
```

### โ†’ Via Composer Script

To use via composer script commands:

* Fixes code to follow coding standards using php-cs-fixer:

```bash
composer cs:diff
```

* Runs php-cs-fixer in dry-run mode and shows diff which will by applied:

```bash
composer cs:fix
```

### โ†’ Using Makefile

**To use with `Makefile`**

* Fixes code to follow coding standards using php-cs-fixer:

```bash
make lint-php
```

* Runs php-cs-fixer in dry-run mode and shows diff which will by applied:

```bash
make lint-diff
```


## ๐Ÿ”’ Security Policy

This project has a [security policy](.github/SECURITY.md).


## ๐Ÿ™Œ Want to Contribute?

Thank you for considering contributing to the wayofdev community! We are open to all kinds of contributions. If you want to:

* ๐Ÿค” [Suggest a feature](https://github.com/wayofdev/php-cs-fixer-config/issues/new?assignees=&labels=type%3A+enhancement&projects=&template=2-feature-request.yml&title=%5BFeature%5D%3A+)
* ๐Ÿ› [Report an issue](https://github.com/wayofdev/php-cs-fixer-config/issues/new?assignees=&labels=type%3A+documentation%2Ctype%3A+maintenance&projects=&template=1-bug-report.yml&title=%5BBug%5D%3A+)
* ๐Ÿ“– [Improve documentation](https://github.com/wayofdev/php-cs-fixer-config/issues/new?assignees=&labels=type%3A+documentation%2Ctype%3A+maintenance&projects=&template=4-docs-bug-report.yml&title=%5BDocs%5D%3A+)
* ๐Ÿ‘จโ€๐Ÿ’ป Contribute to the code

You are more than welcome. Before contributing, kindly check our [contribution guidelines](.github/CONTRIBUTING.md).

[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=for-the-badge)](https://conventionalcommits.org)


## ๐Ÿซก Contributors



Contributors Badge





## ๐ŸŒ Social Links

* **Twitter:** Follow our organization [@wayofdev](https://twitter.com/intent/follow?screen_name=wayofdev) and the author [@wlotyp](https://twitter.com/intent/follow?screen_name=wlotyp).
* **Discord:** Join our community on [Discord](https://discord.gg/CE3TcCC5vr).


## ๐Ÿงฑ Resources

* Full documentation about all fixers is available here - [PHP-CS-Fixer configuration UI](https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0)

* The official [PHP-CS-Fixer documentation](https://github.com/FriendsOfPHP/PHP-CS-Fixer)


## โš–๏ธ License

[![Licence](https://img.shields.io/github/license/wayofdev/php-cs-fixer-config?style=for-the-badge&color=blue)](./LICENSE.md)