tightenco / tlint
Tighten linter for Laravel conventions
Installs: 890 289
Dependents: 33
Suggesters: 1
Security: 0
Stars: 524
Watchers: 15
Forks: 32
Open Issues: 3
Requires
- php: >=8.1
- illuminate/view: *
- nikic/php-parser: ^5.0
- symfony/console: ^6.1||^7.0
- symfony/process: ^6.1||^7.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- spatie/ray: ^1.37
- symfony/var-dumper: ^6.1
- tightenco/duster: ^2.0
- dev-main
- v9.3.1
- v9.3.0
- v9.2.0
- v9.1.1
- v9.1.0
- v9.0.0
- v8.0.3
- v8.0.2
- v8.0.1
- v8.0.0
- v7.0.1
- v7.0.0
- v6.3.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.0
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.0.16
- v5.0.15
- v5.0.14
- v5.0.13
- v5.0.11
- v5.0.10
- v5.0.9
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.0
- 2.1.8
- 2.1.7
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.3
- 2.0.2
- 2.0.0
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-mes/fix-static-model-on-v8
This package is auto-updated.
Last update: 2024-11-09 20:01:55 UTC
README
Install (Requires PHP 8.1+)
Note TLint is intended to work with the tools included in Duster. To receive the best coverage we recommend using Duster to install and configure TLint.
# Include in project composer require tightenco/tlint --dev # Include globally composer global require tightenco/tlint
Upgrade
# Upgrade in project composer update tightenco/tlint #Upgrade globally composer global update tightenco/tlint
Upgrading from 8.x to 9.x
TLint 9 requires PHP >= 8.1.
tformat.json
has been dropped in favor of a single tlint.json
file.
Now linting the following files and directories:
public/
bootstrap/
server.php
app/Http/Middleware/RedirectIfAuthenticated.php
Exceptions/Handler.php
app/Http/Controllers/Auth/
app/Http/Kernel.php
To continue excluding these files and directories add them to your tlint.json
file under excluded
.
Upgrading from 7.x to 8.x
A significant number of formatters were added between the 7.x and 8.x releases.
If you want to roll these out gradually or disable them altogether, you can use the disabled
setting in your tlint.json
config.
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. If you need to add these back you can grab them from an earlier version of TLint and follow the Custom Configuration 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.
For example, Laravel has many available ways to pass variables from a controller to a view:
A)
$value = 'Hello, World!'; return view('view', compact('value'));
B)
return view('view', ['value' => 'Hello, World!']);
C)
return view('view') ->with('value', 'Hello, World!');
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
For entire project (you must pass the lint command to use other options)
tlint
For individual files and specific directories
tlint lint index.php
tlint lint app
You can also lint only diff files by running the following with unstaged git changes
tlint lint --diff
tlint lint src --diff
Want the output from a file as JSON? (Primarily used for integration with editor plugins)
tlint lint test.php --json
Want the output from a file as a checkstyle XML report? (Primarily used with CI tools like reviewdog and cs2pr)
tlint lint test.php --checkstyle
Want to only run a single linter?
tlint lint --only=ArrayParametersOverViewWith
Example Output
Linting TestLaravelApp/routes/web.php ============ Lints: ============ ! Prefer `view(...)->with(...)` over `view(..., [...])`. 5 : ` return view('test', ['test' => 'test']);``
Formatting
Using the same conventions as above, but using the format command, you can auto-fix some lints:
tlint format
Linting Configuration
TLint Ships with 2 "preset" styles: Laravel & Tighten. The Laravel preset is intended to match the conventions agreed upon by the Laravel framework contributors, while the Tighten preset is intended to match those agreed upon by Tighten team members.
The default configuration is "tighten" flavored, but you may change this by adding a tlint.json
file to your project's root directory with the following schema:
You may further customize the linters used by adding specific lint names to the
"disabled"
list. You may disable linting for specific directories by adding them to the"excluded"
list. You may provide custom paths by adding them to the"paths"
lists.
{ "preset": "laravel", "disabled": ["ArrayParametersOverViewWith"], "excluded": ["tests/"], "paths": [ { "controllers": ["app/Domain/Http/Controllers"] } ] }
Custom Configuration & Presets
You can also add your own custom preset and linters by providing a fully-qualified class name as the preset. For example, if you created a custom preset class:
namespace App\Support\Linting; use Tighten\TLint\Presets\PresetInterface; class Preset implements PresetInterface { public function getLinters() : array { return [ CustomLinter::class, ]; } public function getFormatters() : array { return [ CustomFormatter::class, ]; } }
Then your config could look like:
{ "preset": "App\\Support\\Linting\\Preset" }
This lets you define custom linting/formatting functionality, or modify the existing linters/formatters to your liking.
Editor Integrations
PHPStorm
Sublime
VSCode
Available Linters
General PHP
OneLineBetweenClassVisibilityChanges
QualifiedNamesOnlyForClassName
RemoveLeadingSlashNamespaces
Laravel
ApplyMiddlewareInRoutes
ArrayParametersOverViewWith
FullyQualifiedFacades
MailableMethodsInBuild
NoLeadingSlashesOnRoutePaths
NoDocBlocksForMigrationUpDown
NoJsonDirective
NoSpaceAfterBladeDirectives
,SpaceAfterBladeDirectives
PureRestControllers
RequestHelperFunctionWherePossible
RequestValidation
SpacesAroundBladeRenderContent
UseAnonymousMigrations
Available Formatters
Notes about formatting
- Formatting is designed to alter the least amount of code possible.
- Import related formatters are not designed to alter grouped imports.
General PHP
OneLineBetweenClassVisibilityChanges
RemoveLeadingSlashNamespaces
Laravel
ArrayParametersOverViewWith
FullyQualifiedFacades
MailableMethodsInBuild
NoDatesPropertyOnModels
NoDocBlocksForMigrationUpDown
NoLeadingSlashesOnRoutePaths
NoSpaceAfterBladeDirectives
RequestHelperFunctionWherePossible
RequestValidation
SpaceAfterBladeDirectives
SpacesAroundBladeRenderContent
UseAnonymousMigrations
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email hello@tighten.co instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.