Skip to content

Commit

Permalink
Merge pull request #297 from tighten/7.x
Browse files Browse the repository at this point in the history
7.0
  • Loading branch information
driftingly authored Nov 2, 2022
2 parents 001743e + ae0f895 commit 0e98678
Show file tree
Hide file tree
Showing 121 changed files with 526 additions and 4,206 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ on:
branches: [main, ci]
pull_request:
schedule:
- cron: '0 0 * * *'
- cron: "0 0 * * *"
jobs:
test:
name: PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony[0] }}, ${{ matrix.dependencies }}, ${{ matrix.os }}
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
os: [ubuntu, macos, windows]
php: ['7.3', '7.4', '8.0', '8.1']
symfony: [[4, '^4.4.30', '^4.4.20'], [5, '^5.3.7', '^5.0.9'], [6, '^6.0', '^6.0']]
php: ["8.0", "8.1"]
symfony:
[
[4, "^4.4.30", "^4.4.20"],
[5, "^5.3.7", "^5.0.9"],
[6, "^6.0", "^6.0"],
]
dependencies: [lowest, stable]
exclude:
- php: '7.3'
symfony: [6, '^6.0', '^6.0']
- php: '7.4'
symfony: [6, '^6.0', '^6.0']
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
Expand Down
40 changes: 37 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,50 @@ Breaking changes are marked with ⚠️.

## [Unreleased]

## [v7.0.0] - 2022-11-02

**Changed**

- Updated presets to include FullyQualifiedFacades by @bakerkretzmar ([#265](https://github.com/tighten/tlint/pull/265)).
- Move shared visitor setup into base Linter by @bakerkretzmar ([#281](https://github.com/tighten/tlint/pull/281)).
- Update readme by @driftingly ([#295](https://github.com/tighten/tlint/pull/295)).
- Update Laravel testing fixture by @driftingly ([#296](https://github.com/tighten/tlint/pull/296))

**Removed**

- Remove redundant linters and formatters by @driftingly ([#284](https://github.com/tighten/tlint/pull/284)).
- Linters
- AlphabeticalImports
- ClassThingsOrder
- ConcatenationNoSpacing
- ConcatenationSpacing
- ImportFacades
- NewLineAtEndOfFile
- NoCompact
- NoDump
- NoInlineVarDocs
- NoStringInterpolationWithoutBraces
- NoUnusedImports
- SpaceAfterSoleNotOperator
- TrailingCommasOnArrays
- UseConfigOverEnv
- Formatters
- AlphabeticalImports
- NewLineAtEndOfFile
- UnusedImports
- Drop support for PHP 7.3 and 7.4 by @driftingly ([#289](https://github.com/tighten/tlint/pull/289)).

## [v6.3.0] - 2022-04-25

**Added**

- Add paths config option by @driftingly in ([#288](https://github.com/tighten/tlint/pull/288)).
- Add paths config option by @driftingly ([#288](https://github.com/tighten/tlint/pull/288)).

## [v6.2.2] - 2022-04-03

**Fixed**

- Fix `NoRequestAll` linter to allow other request methods by @driftingly in ([#286](https://github.com/tighten/tlint/pull/286)).
- Fix `NoRequestAll` linter to allow other request methods by @driftingly ([#286](https://github.com/tighten/tlint/pull/286)).

## [v6.2.1] - 2021-11-12

Expand Down Expand Up @@ -88,7 +121,8 @@ Breaking changes are marked with ⚠️.

For previous changes see the [Releases](https://github.com/tighten/tlint/releases) page.

[Unreleased]: https://github.com/tighten/tlint/compare/v6.3.0...HEAD
[Unreleased]: https://github.com/tighten/tlint/compare/v7.0.0...HEAD
[v7.0.0]: https://github.com/tighten/tlint/compare/v6.3.0...v7.0.0
[v6.3.0]: https://github.com/tighten/tlint/compare/v6.2.2...v6.3.0
[v6.2.2]: https://github.com/tighten/tlint/compare/v6.2.1...v6.2.2
[v6.2.1]: https://github.com/tighten/tlint/compare/v6.2.0...v6.2.1
Expand Down
2 changes: 1 addition & 1 deletion bin/tlint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

const TLINT_VERSION = 'v6.3.0';
const TLINT_VERSION = 'v7.0.0';

foreach (
[
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
}
],
"require": {
"php": ">=7.3",
"php": ">=8.0",
"illuminate/view": "*",
"nikic/php-parser": "^4.12",
"nikic/php-parser": "^4.15",
"symfony/console": "^4.4.30 || ^5.3.7 || ^6.0",
"symfony/process": "^4.4.20 || ^5.0.9 || ^6.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5.16 || ^9.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^5.0",
"friendsofphp/php-cs-fixer": "^3.1"
},
Expand Down
64 changes: 18 additions & 46 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/tightenco/tlint.svg?style=flat-square)](https://packagist.org/packages/tightenco/tlint)

## Install (Requires PHP 7.3+)
## Install (Requires PHP 8.0+)

>**Note**
>TLint is intended to work with the tools included in [Duster](https://github.com/tighten/duster). To receive the best coverage we recommend using Duster to install and configure TLint.
```
composer global require tightenco/tlint
Expand All @@ -16,6 +19,11 @@ composer global require tightenco/tlint
composer global update tightenco/tlint
```

### Upgrading from 6.x to 7.x

TLint focuses on linting and formatting issues other tools are not able to catch.
The `7.x` release removes lints and formatters covered by tools in [Duster](https://github.com/tighten/duster). If you need to add these back you can grab them from an earlier version of TLint and follow the [Custom Configuration](#custom-configuration--presets) documentation.

## What Is It?

This is an opinionated code linter (with growing support for auto-formatting!) for Tighten flavored code conventions for Laravel and PHP.
Expand Down Expand Up @@ -43,14 +51,8 @@ return view('view')
->with('value', 'Hello, World!');
```

> In this case [TLint](https://github.com/tighten/tlint) will warn if you are not using the **B)** method.
> This example is a sort of "meta layer" of code linting, allowing teams to avoid higher level sticking points of code review / discussions.
> TLint also has more immediately useful lints that can supplement your editor/IDE such as:
- `NoUnusedImports`
- `TrailingCommasOnArrays`
- And many more! (See below for full listing)
In this case TLint will warn if you are not using the **B)** method.
This example is a sort of "meta layer" of code linting, allowing teams to avoid higher level sticking points of code review / discussions.

## Usage

Expand Down Expand Up @@ -83,7 +85,7 @@ tlint lint test.php --json
Want to only run a single linter?

```
tlint lint --only=UseConfigOverEnv
tlint lint --only=ArrayParametersOverViewWith
```

## Example Output
Expand Down Expand Up @@ -119,7 +121,7 @@ The default configuration is "tighten" flavored, but you may change this by addi
```json
{
"preset": "laravel",
"disabled": ["NoInlineVarDocs"],
"disabled": ["ArrayParametersOverViewWith"],
"excluded": ["tests/"],
"paths": [
{
Expand All @@ -136,21 +138,23 @@ You can also add your own custom preset and linters by providing a fully-qualifi
```php
namespace App\Support\Linting;

/** use ... */
use Tighten\TLint\Presets\PresetInterface;

class Preset implements PresetInterface
{
public function getLinters() : array
{
return [
PrefixTestsWithTest::class,
CustomLinter::class,
ModelMethodOrder::class,
];
}

public function getFormatters() : array
{
return [];
return [
CustomFormatter::class,
];
}
}
```
Expand Down Expand Up @@ -196,30 +200,19 @@ The default configuration is "tighten", but you may change this by adding a `tfo
<!-- linters -->
| Linter | Description |
| --- | --- |
| `AlphabeticalImports` | Imports should be ordered alphabetically. |
| `ApplyMiddlewareInRoutes` | Apply middleware in routes (not controllers). |
| `ArrayParametersOverViewWith` | Prefer `view(..., [...])` over `view(...)->with(...)`. |
| `ClassThingsOrder` | Class "things" should follow the ordering presented in the [handbook](https://gist.github.com/mattstauffer/1178946cb585b17a3941dd0edcbce0c4) |
| `ConcatenationNoSpacing` | There should be no space around `.` concatenations, and additional lines should always start with a `.` |
| `ConcatenationSpacing` | There should be 1 space around `.` concatenations, and additional lines should always start with a `.` |
| `FullyQualifiedFacades` | Import facades using their full namespace. |
| `ImportFacades` | Import facades (don't use aliases). |
| `MailableMethodsInBuild` | Mailable values (from and subject etc) should be set in build(). |
| `ModelMethodOrder` | Model method order should be: booting > boot > booted > custom_static > relationships > scopes > accessors > mutators > custom |
| `NewLineAtEndOfFile` | File should end with a new line |
| `NoCompact` | There should be no calls to `compact()` in controllers |
| `NoDatesPropertyOnModels` | The `$dates` property was deprecated in Laravel 8. Use `$casts` instead. |
| `NoDocBlocksForMigrationUpDown` | Remove doc blocks from the up and down method in migrations. |
| `NoDump` | There should be no calls to `dd()`, `dump()`, `ray()`, or `var_dump()` |
| `NoInlineVarDocs` | No /** @var ClassName $var */ inline docs. [ref](https://github.com/tighten/tlint/issues/108) |
| `NoJsonDirective` | Use blade `{{ $model }}` auto escaping for models, and double quotes via json_encode over @json blade directive: `<vue-comp :values='@json($var)'>` -> `<vue-comp :values="{{ $model }}">` OR `<vue-comp :values="{!! json_encode($var) !!}">` |
| `NoLeadingSlashesOnRoutePaths` | No leading slashes on route paths. |
| `NoMethodVisibilityInTests` | There should be no method visibility in test methods. [ref](https://github.com/tighten/tlint/issues/106#issuecomment-537952774) |
| `NoParensEmptyInstantiations` | No parenthesis on empty instantiations |
| `NoRequestAll` | No `request()->all()`. Use `request()->only(...)` to retrieve specific input values. |
| `NoSpaceAfterBladeDirectives` | No space between blade template directive names and the opening paren:`@section (` -> `@section(` |
| `NoStringInterpolationWithoutBraces` | Never use string interpolation without braces |
| `NoUnusedImports` | There should be no unused imports. |
| `OneLineBetweenClassVisibilityChanges` | Class members of differing visibility must be separated by a blank line |
| `PureRestControllers` | You should not mix restful and non-restful public methods in a controller |
| `QualifiedNamesOnlyForClassName` | Fully Qualified Class Names should only be used for accessing class names |
Expand All @@ -228,30 +221,18 @@ The default configuration is "tighten", but you may change this by adding a `tfo
| `RequestValidation` | Use `request()->validate(...)` helper function or extract a FormRequest instead of using `$this->validate(...)` in controllers |
| `RestControllersMethodOrder` | REST methods in controllers should match the ordering here: https://laravel.com/docs/controllers#restful-partial-resource-routes |
| `SpaceAfterBladeDirectives` | Put a space between blade control structure names and the opening paren:`@if(` -> `@if (` |
| `SpaceAfterSoleNotOperator` | There should be a space after sole `!` operators |
| `SpacesAroundBladeRenderContent` | Spaces around blade rendered content:`{{1 + 1}}` -> `{{ 1 + 1 }}` |
| `TrailingCommasOnArrays` | Multiline arrays should have trailing commas |
| `UseAnonymousMigrations` | Prefer anonymous class migrations. |
| `UseAuthHelperOverFacade` | Prefer the `auth()` helper function over the `Auth` Facade. |
| `UseConfigOverEnv` | Don’t use environment variables directly; instead, use them in config files and call config vars from code |
| `ViewWithOverArrayParameters` | Prefer `view(...)->with(...)` over `view(..., [...])`. |
<!-- /linters -->

### General PHP

- `AlphabeticalImports`
- `ClassThingsOrder`
- `ConcatenationSpacing`
- `NewLineAtEndOfFile`
- `NoInlineVarDocs`
- `NoParensEmptyInstantiations`
- `NoStringInterpolationWithoutBraces`
- `NoUnusedImports`
- `OneLineBetweenClassVisibilityChanges`
- `QualifiedNamesOnlyForClassName`
- `RemoveLeadingSlashNamespaces`
- `SpaceAfterSoleNotOperator`
- `TrailingCommasOnArrays`

### PHPUnit

Expand All @@ -262,12 +243,10 @@ The default configuration is "tighten", but you may change this by adding a `tfo
- `ApplyMiddlewareInRoutes`
- `ArrayParametersOverViewWith`
- `FullyQualifiedFacades`
- `ImportFacades`
- `MailableMethodsInBuild`
- `NoLeadingSlashesOnRoutePaths`
- `ModelMethodOrder`
- `NoDocBlocksForMigrationUpDown`
- `NoDump`
- `NoJsonDirective`
- `NoSpaceAfterBladeDirectives`, `SpaceAfterBladeDirectives`
- `PureRestControllers`
Expand All @@ -277,7 +256,6 @@ The default configuration is "tighten", but you may change this by adding a `tfo
- `SpacesAroundBladeRenderContent`
- `UseAnonymousMigrations`
- `UseAuthHelperOverFacade`
- `UseConfigOverEnv`
- `ViewWithOverArrayParameters`

## Available Formatters (Beta)
Expand All @@ -290,22 +268,16 @@ The default configuration is "tighten", but you may change this by adding a `tfo
<!-- formatters -->
| Formatter | Description |
| --- | --- |
| `AlphabeticalImports` | Alphabetizes import statements. |
| `ExcessSpaceBetweenAndAfterImports` | Removes excess newlines around use statements. |
| `FullyQualifiedFacades` | Import facades using their full namespace. |
| `NewLineAtEndOfFile` | Applies a newline at the end of files. |
| `NoDatesPropertyOnModels` | Use `$casts` instead of `$dates` on Eloquent models. |
| `NoDocBlocksForMigrationUpDown` | Removes doc blocks from the up and down method in migrations. |
| `UnusedImports` | Removes unused import statements. |
| `UseAnonymousMigrations` | Prefer anonymous class migrations. |
<!-- /formatters -->

### General PHP

- `AlphabeticalImports`
- `ExcessSpaceBetweenAndAfterImports`
- `NewLineAtEndOfFile`
- `UnusedImports`

### Laravel

Expand Down
25 changes: 24 additions & 1 deletion src/BaseLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Tighten\TLint;

use Closure;
use LogicException;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\FindingVisitor;
use PhpParser\Parser;

class BaseLinter
Expand All @@ -29,7 +33,7 @@ public static function appliesToPath(string $path, array $configPaths): bool

public function lint(Parser $parser)
{
return [];
return $this->traverseAutomatically($parser);
}

public function getLintDescription()
Expand Down Expand Up @@ -72,4 +76,23 @@ public function getFilename()
{
return $this->filename;
}

protected function visitor(): Closure
{
throw new LogicException('Custom linters must override either the `lint` or `visitor` method.');
}

protected function visitUsing(Parser $parser, Closure $callback): FindingVisitor
{
$traverser = new NodeTraverser;
$traverser->addVisitor($visitor = new FindingVisitor($callback));
$traverser->traverse($parser->parse($this->code));

return $visitor;
}

private function traverseAutomatically(Parser $parser): array
{
return $this->visitUsing($parser, $this->visitor())->getFoundNodes();
}
}
Loading

0 comments on commit 0e98678

Please sign in to comment.