https://github.com/isaaceindhoven/php-code-sniffer-baseliner
Baseline tool for PHP_CodeSniffer
https://github.com/isaaceindhoven/php-code-sniffer-baseliner
php phpcodesniffer phpcs
Last synced: 27 days ago
JSON representation
Baseline tool for PHP_CodeSniffer
- Host: GitHub
- URL: https://github.com/isaaceindhoven/php-code-sniffer-baseliner
- Owner: isaaceindhoven
- License: mit
- Archived: true
- Created: 2021-04-30T07:20:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-24T08:42:00.000Z (over 2 years ago)
- Last Synced: 2025-11-27T15:13:53.784Z (2 months ago)
- Topics: php, phpcodesniffer, phpcs
- Language: PHP
- Homepage: https://isaac.nl
- Size: 86.9 KB
- Stars: 16
- Watchers: 6
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Renamed and moved to iO CodeSniffer Standard
===========
This repository has been archived and renamed, moved to [iO PHP_CodeSniffer Baseliner](https://github.com/iodigital-com/php-code-sniffer-baseliner). Feature sniffs and changes will be processed in the iO repository.
# PHP_CodeSniffer Baseliner
This tool enables you to integrate [PHP_CodeSniffer][php-code-sniffer] into an existing
project by automatically adding `phpcs:ignore` and `phpcs:disable`/`phpcs:enable` instructions throughout the codebase
as a baseline. This allows you to make PHP_CodeSniffer pass without changing any source code, making it
possible to use PHP_CodeSniffer in e.g. continuous integration pipelines or git hooks. This way, you can enforce that
all new code adheres to your coding standard without touching the existing code.
## Installation
Require the package with composer:
```sh
composer require --dev isaac/php-code-sniffer-baseliner
```
It is also possible to install this package as a global composer dependency.
## Usage
In order to add `phpcs:ignore` and `phpcs:disable`/`phpcs:enable` instructions throughout your project, run:
```sh
vendor/bin/phpcs-baseliner create-baseline
```
## How does it work?
First, the tool runs `vendor/bin/phpcs` and captures the report. Based on the report output, it will add
`// phpcs:ignore` instructions to the source code for each violation. It will only ignore the sniffs that actually are
violated. In rare cases, adding these instructions could introduce new violations. Therefore, this process is repeated
until no violations are reported by `phpcs`.
## Example
Let's say we want to enforce `declare(strict_types = 1);` statements and native property type hints using
PHP_CodeSniffer. The [Slevomat Coding Standard][slevomat-coding-standard] has sniffs for this:
[`SlevomatCodingStandard.TypeHints.DeclareStrictTypes`][declare-strict-types-sniff]
and [`SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint`][property-type-hint-sniff]. We install
Slevomat Coding Standard and add the sniffs to our ruleset in `phpcs.xml`.
If we now run `vendor/bin/phpcs-baseliner create-baseline` in our project, it will add ignore instructions in all files
not containing `declare(strict_types = 1);` statements or native property type declarations:
```diff
- `.
- Support ignoring violations on the first line of a file that end with a multi-line string, example:
```php
```
- Support detection of and merging with older types of ignore instructions, such as `@phpcsSuppress`.
[php-code-sniffer]: (https://github.com/squizlabs/PHP_CodeSniffer)
[slevomat-coding-standard]: (https://github.com/slevomat/coding-standard)
[declare-strict-types-sniff]: (https://github.com/slevomat/coding-standard#slevomatcodingstandardtypehintsdeclarestricttypes-)
[property-type-hint-sniff]: (https://github.com/slevomat/coding-standard#slevomatcodingstandardtypehintspropertytypehint-)
[unit-test-data-set]: (https://github.com/isaaceindhoven/php-code-sniffer-baseliner/blob/master/tests/File/AddBaselineProcessorTestDataProvider.php)