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

Add Pint, PHP-CS-Fixer, and reconfigure #43

Merged
merged 12 commits into from
Oct 18, 2022
2 changes: 1 addition & 1 deletion .github/workflows/compatibility-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [Ubuntu, Windows, macOS]
php: [7.4, 8.0, 8.1]
php: [8.0, 8.1]

include:
- os: Ubuntu
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/composer.lock
/vendor
.phpunit.result.cache
50 changes: 50 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use Tighten\Duster\Fixer\ClassNotation\CustomOrderedClassElementsFixer;

$finder = Finder::create()
->in(array_filter(
[
'./app',
'./config',
'./database',
'./public',
'./resources',
'./routes',
'./tests',
],
fn($dir) => is_dir($dir)
))
->notName('*.blade.php');

return (new Config())
->setFinder($finder)
->setUsingCache(false)
->registerCustomFixers([new CustomOrderedClassElementsFixer()])
->setRules([
'Tighten/custom_ordered_class_elements' => [
'order' => [
'use_trait',
'property_public_static',
'property_protected_static',
'property_private_static',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct',
'invoke',
'method_public_static',
'method_protected_static',
'method_private_static',
'method_public',
'method_protected',
'method_private',
'magic',
],
],
]);
100 changes: 61 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,102 @@
![Project Banner](https://raw.githubusercontent.com/tighten/duster/main/banner.png)
# Duster

Automatically apply Tighten's default code style for Laravel apps:
Automatically apply Tighten's default code style for Laravel apps.

- PHPCS, with PSR-12 + some special preferences
- Tighten's Tlint
- Maybe JS and CSS?
To achieve this, Duster installs and automatically configures the following tools:

To achieve this, this package installs PHPCS (and PHPCBF with it) and Tlint, and automatically configures them. Tlint uses the default `Tighten` preset. PHPCS uses the [`Tighten` preset](https://github.com/tighten/tighten-coding-standard) which is `PSR-12` and a few Tighten-specific rules.
- TLint: Opinionated code linter for Laravel and PHP
- using the default `Tighten` preset
- PHP_CodeSniffer: catch issues that can't be fixed automatically
- using the `Tighten` preset which is mostly PSR1 with some Tighten-specific rules
- PHP CS Fixer: custom rules not supported by Laravel Pint
- `CustomOrderedClassElementsFixer` Tighten-specific order of class elements
- Pint: Laravel's code style rules (with a few Tighten specific customizations)
- using the default `Laravel` preset with some Tighten-specific rules

## Installation

You can install the package via composer:

```bash
composer require tightenco/duster --dev
./vendor/bin/duster init
```

When installing you may see the following message:
Optionally you can publish a GitHub Actions linting config:

>dealerdirect/phpcodesniffer-composer-installer contains a Composer plugin which is currently not in your allow-plugins config. See https://getcomposer.org/allow-plugins
>Do you trust "dealerdirect/phpcodesniffer-composer-installer" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]

You will need to accept the [phpcodesniffer-composer-installer](https://github.com/PHPCSStandards/composer-installer) prompt to have the PHPCS rulesets and so the GitHub actions will work.

This adds an `allowed-plugins` entry to your `composer.json` file:

```json
...
"config": {
...
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
```bash
duster github-actions
```


You must run `./vendor/bin/duster init` after installing, or you won't have a local copy of the PHPCS config file, and Duster won't work.

The `init` command will also optionally add a GitHub action to run Duster's linters.

## Usage

To lint everything at once:

```bash
./vendor/bin/duster lint
duster
```

To fix everything at once:

```bash
./vendor/bin/duster fix
duster fix
```

To run individual lints:
To view all available commands:

```bash
./vendor/bin/duster tlint
./vendor/bin/duster phpcs
duster help
```

To run individual fixes:
## Customizing

```bash
./vendor/bin/duster tlint fix
./vendor/bin/duster phpcs fix
### TLint

Create a `tlint.json` file in your project root. Learn more in the [TLint documentation](https://github.com/tighten/tlint#configuration).

### PHP_CodeSniffer

Create a `.phpcs.xml.dist` file in your project root with the following:

```xml
<?xml version="1.0"?>
<ruleset>
<file>app</file>
<file>config</file>
<file>database</file>
<file>public</file>
<file>resources</file>
<file>routes</file>
<file>tests</file>

<rule ref="Tighten"/>
</ruleset>
```

### Customizing the lints
Now you can add customizations below the `<rule ref="Tighten"/>` line or even disable the Tighten rule and use your own ruleset. Learn more in this [introductory article](https://ncona.com/2012/12/creating-your-own-phpcs-standard/).

### PHP CS Fixer

Create a `.php-cs-fixer.dist.php` file in your project root with the contents from [Duster's `.php-cs-fixer.dist.php`](.php-cs-fixer.dist.php). Learn more in the [PHP CS Fixer documentation](https://cs.symfony.com/doc/config.html).

To override the configuration for PHPCS, you can edit the `.phpcs.xml.dist` file and add customizations below the `<rule ref="Tighten"/>` line or even disable the Tighten rule and use your own ruleset. Learn more in this [introductory article](https://ncona.com/2012/12/creating-your-own-phpcs-standard/).
### Pint

Create a `pint.json` file in your project root with the following:

```json
{
"preset": "laravel",
"rules": {
"concat_space": {
"spacing": "one"
},
"class_attributes_separation": {
}
}
}
```

To override the configuration for Tlint, create a `tlint.json` file in your project root. Learn more in the [Tlint documentation](https://github.com/tighten/tlint#configuration).
Now you can add or remove customizations. Learn more in the [Pint documentation](https://laravel.com/docs/pint#configuring-pint).

## Contributing

Expand Down
4 changes: 0 additions & 4 deletions bin/actions/fix

This file was deleted.

9 changes: 0 additions & 9 deletions bin/actions/fix-phpcs

This file was deleted.

5 changes: 0 additions & 5 deletions bin/actions/fix-tlint

This file was deleted.

12 changes: 0 additions & 12 deletions bin/actions/help

This file was deleted.

42 changes: 0 additions & 42 deletions bin/actions/init

This file was deleted.

4 changes: 0 additions & 4 deletions bin/actions/lint

This file was deleted.

9 changes: 0 additions & 9 deletions bin/actions/lint-phpcs

This file was deleted.

5 changes: 0 additions & 5 deletions bin/actions/lint-tlint

This file was deleted.

Loading