Stars: 423
Forks: 33
Pull Requests: 198
Issues: 127
Watchers: 16
Last Updated: 2022-12-10 04:17:34
Tighten linter for Laravel conventions.
License: MIT License
Languages: PHP
Upgrade
composer global update tightenco/tlintUpgrading from 6.x to 7.x
TLint focuses on linting and formatting issues other tools are not able to catch. The
7.xrelease 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)
tlintFor individual files and specific directories
tlint lint index.php tlint lint appYou can also lint only diff files by running the following with unstaged git changes
tlint lint --diff tlint lint src --diffWant the output from a file as JSON? (Primarily used for integration with editor plugins)
tlint lint test.php --jsonWant to only run a single linter?
tlint lint --only=ArrayParametersOverViewWithExample Output
Linting TestLaravelApp/routes/web.php ============ Lints: ============ ! Prefer `view(...)->with(...)` over `view(..., [...])`. 5 : ` return view('test', ['test' => 'test']);``Formatting (Beta)
Using the same conventions as above, but using the format command, you can auto-fix some lints:
tlint formatLinting 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.jsonfile 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, ModelMethodOrder::class, ]; } public function getFormatters() : array { return [ CustomFormatter::class, ]; } }Then your config could look like:
{ "preset": "App\\Support\\Linting\\Preset" }This lets you define whatever custom linting functionality, or modify the existing linters to your liking.
Formatting Configuration (Beta)
Similar to linting there are two "preset" styles for formatting: Laravel & Tighten.
The default configuration is "tighten", but you may change this by adding a
tformat.jsonfile to your project's root directory with the following schema:{ "preset": "laravel" }Editor Integrations
PHPStorm
Sublime
VSCode
Available Linters
Linter Description ApplyMiddlewareInRoutesApply middleware in routes (not controllers). ArrayParametersOverViewWithPrefer view(..., [...])overview(...)->with(...).FullyQualifiedFacadesImport facades using their full namespace. MailableMethodsInBuildMailable values (from and subject etc) should be set in build(). ModelMethodOrderModel method order should be: booting > boot > booted > custom_static > relationships > scopes > accessors > mutators > custom NoDatesPropertyOnModelsThe $datesproperty was deprecated in Laravel 8. Use$castsinstead.NoDocBlocksForMigrationUpDownRemove doc blocks from the up and down method in migrations. NoJsonDirectiveUse 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) !!}">NoLeadingSlashesOnRoutePathsNo leading slashes on route paths. NoMethodVisibilityInTestsThere should be no method visibility in test methods. ref NoParensEmptyInstantiationsNo parenthesis on empty instantiations NoRequestAllNo request()->all(). Userequest()->only(...)to retrieve specific input values.NoSpaceAfterBladeDirectivesNo space between blade template directive names and the opening paren: @section (->@section(OneLineBetweenClassVisibilityChangesClass members of differing visibility must be separated by a blank line PureRestControllersYou should not mix restful and non-restful public methods in a controller QualifiedNamesOnlyForClassNameFully Qualified Class Names should only be used for accessing class names RemoveLeadingSlashNamespacesPrefer Namespace\...over\Namespace\....RequestHelperFunctionWherePossibleUse the request(...) helper function directly to access request values wherever possible RequestValidationUse request()->validate(...)helper function or extract a FormRequest instead of using$this->validate(...)in controllersRestControllersMethodOrderREST methods in controllers should match the ordering here: https://laravel.com/docs/controllers#restful-partial-resource-routes SpaceAfterBladeDirectivesPut a space between blade control structure names and the opening paren: @if(->@if (SpacesAroundBladeRenderContentSpaces around blade rendered content: {{1 + 1}}->{{ 1 + 1 }}UseAnonymousMigrationsPrefer anonymous class migrations. UseAuthHelperOverFacadePrefer the auth()helper function over theAuthFacade.ViewWithOverArrayParametersPrefer view(...)->with(...)overview(..., [...]).General PHP
NoParensEmptyInstantiationsOneLineBetweenClassVisibilityChangesQualifiedNamesOnlyForClassNameRemoveLeadingSlashNamespacesPHPUnit
NoMethodVisibilityInTestsLaravel
ApplyMiddlewareInRoutesArrayParametersOverViewWithFullyQualifiedFacadesMailableMethodsInBuildNoLeadingSlashesOnRoutePathsModelMethodOrderNoDocBlocksForMigrationUpDownNoJsonDirectiveNoSpaceAfterBladeDirectives,SpaceAfterBladeDirectivesPureRestControllersRequestHelperFunctionWherePossibleRequestValidationRestControllersMethodOrderSpacesAroundBladeRenderContentUseAnonymousMigrationsUseAuthHelperOverFacadeViewWithOverArrayParametersAvailable Formatters (Beta)
Notes about formatting
- Formatting is designed to alter the least amount of code possible.
- Import related formatters are not designed to alter grouped imports.
Formatter Description ArrayParametersOverViewWithPrefer view(..., [...])overview(...)->with(...).FullyQualifiedFacadesImport facades using their full namespace. MailableMethodsInBuildMailable values (from and subject etc) should be set in build(). NoDatesPropertyOnModelsUse $castsinstead of$dateson Eloquent models.NoDocBlocksForMigrationUpDownRemoves doc blocks from the up and down method in migrations. NoSpaceAfterBladeDirectivesNo space between blade template directive names and the opening parenthesis. RemoveLeadingSlashNamespacesPrefer Namespace\...over\Namespace\....NoLeadingSlashesOnRoutePathsNo leading slashes on route paths. RequestHelperFunctionWherePossibleUse the request(...) helper function directly to access request values wherever possible. OneLineBetweenClassVisibilityChangesClass members of differing visibility must be separated by a blank line. RequestValidationUse request()->validate(...)helper function or extract a FormRequest instead of using$this->validate(...)in controllersSpaceAfterBladeDirectivesPuts a space between blade control structure names and the opening parenthesis SpacesAroundBladeRenderContentSpaces around blade rendered content. UseAnonymousMigrationsPrefer anonymous class migrations. UseAuthHelperOverFacadePrefer the auth()helper function over theAuthFacade.General PHP
OneLineBetweenClassVisibilityChangesRemoveLeadingSlashNamespacesLaravel
ArrayParametersOverViewWithFullyQualifiedFacadesMailableMethodsInBuildNoDatesPropertyOnModelsNoDocBlocksForMigrationUpDownNoSpaceAfterBladeDirectivesNoLeadingSlashesOnRoutePathsRequestHelperFunctionWherePossibleRequestValidationSpaceAfterBladeDirectivesSpacesAroundBladeRenderContentUseAnonymousMigrationsUseAuthHelperOverFacadeContributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.