Giter VIP home page Giter VIP logo

tlint's Introduction


Build Status

Install (Requires php7.2+)

composer global require tightenco/tlint

What is it?

This is an opinionated code linter 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)

return view('view', ['value' => 'Hello, World!']);

B)

$value = 'Hello, World!';

return view('view', compact('value'));

C)

return view('view')
    ->with('value', 'Hello, World!');

In this case TLint will warn if you are not using the C) 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)

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 to only run a single linter?

tlint --only=UseConfigOverEnv

Example Output

Linting TestLaravelApp/routes/web.php
============
Lints: 
============
! Prefer `view(...)->with(...)` over `view(..., [...])`.
5 : `    return view('test', ['test' => 'test']);``

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 agree 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:

Note: You may further customize the linters used by adding specific lint names to the "disabled" list (As shown below).

{
  "preset": "laravel",
  "disabled": ["NoInlineVarDocs"]
}

Editor Integrations

Available Linters

General PHP

  • No leading slashes in namespaces or static calls or instantiations. RemoveLeadingSlashNamespaces
  • Fully qualified class name only when it's being used a string (class name). QualifiedNamesOnlyForClassName
  • Class "things" should be ordered traits, static constants, statics, constants, public properties, protected properties, private properties, constructor, public methods, protected methods, private methods, other magic methods. ClassThingsOrder
  • Sort imports alphabetically AlphabeticalImports
  • Trailing commas on arrays TrailingCommasOnArrays
  • No parenthesis on empty instantiations NoParensEmptyInstantiations
  • Space after sole not operator SpaceAfterSoleNotOperator
  • One blank line between class constants / properties of different visibility OneLineBetweenClassVisibilityChanges
  • Never use string interpolation without braces NoStringInterpolationWithoutBraces
  • Spaces around concat operators, and start additional lines with concat ConcatenationSpacing
  • File should end with a new line NewLineAtEndOfFile
  • No /** @var ClassName $var */ inline docs NoInlineVarDocs (tighten#108)
  • There should be no unused imports NoUnusedImports

PhpUnit

  • There should be no method visibility in test methods NoMethodVisibilityInTestsTest (tighten#106 (comment))

Laravel

  • Use with over array parameters in view(). ViewWithOverArrayParamaters
  • Prefer view(..., [...]) over view(...)->with(...). ArrayParametersOverViewWith
  • Don’t use environment variables directly in code; instead, use them in config files and call config vars from code. UseConfigOverEnv
  • There should only be rest methods in an otherwise purely restful controller. PureRestControllers
  • Controller method order (rest methods follow docs). RestControllersMethodOrder
  • Use the simplest request(...) wherever possible. RequestHelperFunctionWherePossible
  • Use auth() helper over the Auth facade. UseAuthHelperOverFacade
  • Remove method docblocks in migrations. NoDocBlocksForMigrationUpDown
  • Import facades (don't use aliases). ImportFacades
  • Mailable values (from and subject etc) should be set in build(). MailableMethodsInBuild
  • No leading slashes on route paths. NoLeadingSlashesOnRoutePaths
  • Apply middleware in routes (not controllers). ApplyMiddlewareInRoutes
  • Model method order (relationships > scopes > accessors > mutators > boot). ModelMethodOrder
  • There should be no calls to dd() NoDd
  • Use request()->validate(...) helper function or extract a FormRequest instead of using $this->validate(...) in controllers RequestValidation
  • Blade directive spacing conventions. NoSpaceAfterBladeDirectives, SpaceAfterBladeDirectives
  • Spaces around blade rendered content SpacesAroundBladeRenderContent
  • 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) }}"> NoJsonDirective

tlint's People

Contributors

andrewmile avatar d9705996 avatar darkboywonder avatar jakebathman avatar josecanhelp avatar loganhenson avatar mattstauffer avatar mazedlx avatar nickknissen avatar sbine avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.