Giter Club home page Giter Club logo

laravelcs's Introduction

Laravel PHP_CodeSniffer

This is a custom Sniff to detect violations and reformat PHP source code based on Laravel Coding Standard.

Your source code will be checked for PSR-1 & the Laravel "flavor" of PSR-2.

Usage wih git

Clone this repository

git clone http://github.com/antonioribeiro/laravelcs LaravelCodeSniffer

Execute CodeSniffer

phpcs --standard=LaravelCodeSniffer/Standards/Laravel/  /path/to/your/project/files

Usage with Composer

The recommended way is to install it globally with the following command:

composer global require pragmarx/laravelcs

Make sure you have ~/.composer/vendor/bin/ in your PATH.

You will then be able to run PHP Code Sniffer with the Laravel Standard:

phpcs --standard=$HOME/.composer/vendor/pragmarx/laravelcs/Standards/Laravel/ /path/to/your/project/files

Testing the Sniff

This Sniff was tested using the Laravel Framework source code and some changes, to comply with PSR-2, were required, click here to see them. Those were the violations found and fixed in Filesystem.php:

 --------------------------------------------------------------------------------
 FOUND 14 ERRORS AFFECTING 11 LINES
 --------------------------------------------------------------------------------
   29 | ERROR | [x] Inline control structures are not allowed
   44 | ERROR | [x] Inline control structures are not allowed
  113 | ERROR | [x] Expected 1 new line after closing parenthesis; found " "
  113 | ERROR | [x] Expected 1 newline after opening brace; 0 found
  113 | ERROR | [x] Inline control structures are not allowed
  113 | ERROR | [x] Closing brace must be on a line by itself
  241 | ERROR | [x] Inline control structures are not allowed
  247 | ERROR | [ ] Opening brace should be on the same line as the declaration
  249 | ERROR | [x] Closing brace indented incorrectly; expected 2 spaces, found 3
  310 | ERROR | [x] Inline control structures are not allowed
  335 | ERROR | [x] Inline control structures are not allowed
  343 | ERROR | [x] Inline control structures are not allowed
  361 | ERROR | [x] Inline control structures are not allowed
  384 | ERROR | [x] Inline control structures are not allowed
 --------------------------------------------------------------------------------
 PHPCBF CAN FIX THE 13 MARKED SNIFF VIOLATIONS AUTOMATICALLY
 --------------------------------------------------------------------------------

Contributing

There are probably still a lot to do here, so please, open issues and send pull requests.

laravelcs's People

Contributors

antonioribeiro avatar bryant1410 avatar franzyke avatar mvpasarel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravelcs's Issues

Documentation

Please improve documentation.
Already install the package but dont know how to use it, when not in global require.

Getting an error when trying to run

I installed globally via composer

Getting the error below when I try to run with Laravel Standard

ERROR: the "~/.composer/vendor/pragmarx/laravelcs/Standards/Laravel" coding standard is not installed. The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

Just running phpcs works fine.

Tabs incorrectly expected when using Standard

I am using the standard installed globally, but for some reason, it is expecting tabs to indent code instead of the 4 spaces defined in the https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md that is followed by Laravel.

This is the command I am using
phpcs --standard=$HOME/.composer/vendor/pragmarx/laravelcs/Standards/Laravel/ app/Http/Controllers/ParentController.php

See output

FILE: ...grant/code/mykidsmoola/app/Http/Controllers/ParentController.php

FOUND 10 ERRORS AFFECTING 9 LINES

23 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
23 | ERROR | [x] Multi-line function call not indented correctly;
| | expected 6 spaces but found 8
24 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
25 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
26 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
27 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
39 | ERROR | [x] Tabs must be used to indent lines; spaces are not
| | allowed
40 | ERROR | [x] Multi-line function call not indented correctly;
| | expected 6 spaces but found 3
41 | ERROR | [x] Multi-line function call not indented correctly;
| | expected 6 spaces but found 3
42 | ERROR | [x] Multi-line function call not indented correctly;
| | expected 6 spaces but found 3

PHPCBF CAN FIX THE 10 MARKED SNIFF VIOLATIONS AUTOMATICALLY

Time: 39ms; Memory: 4Mb`

Conditional if error

if (true) {
    // some code
}

throws an error

53 | ERROR | [x] Expected 1 new line after closing parenthesis; | | found " "

Interface "PHP_CodeSniffer_Sniff" not found

When trying to use this standard with PHPCS 3.5.4 and PHP 8.1.5 installed globally on my system, the following error occurs - this error doesn't get triggered when using any other standards:

phpcs: Uncaught Error: Interface "PHP_CodeSniffer_Sniff" not found in .../Standards/Laravel/Sniffs/Tabs/DisallowWhitespaceIndentSniff.php:12

How to specify your own rulesets?

Just wondering how you can tweak the set of rules to custom them as you want? I found the rules used, but would like to avoid to modify this file, and maybe use a simpler format for your rules (javascript linting or pep8 style)

PHPCS needs updated support.

The most recent PHPCS is not supported in this repo, also there are PSR2 clashes with the repo. The current repo does not support PHP 7 return type Hinting, which was fixed in the last year in PHPCS >=3.0.1.
Code example of type hinting.
protected static function buildSeleniumProcess() : Process { //some code here }
Also when using routes file Squiz does not like function closures.
`Route::get('/', function () {
return view('welcome');

<- error here disallow whitespace indent

[tab]});`

<- squiz.whitespace and laravel.tabs disallow whitespace clash.

PHP Notice: Use of undefined constant T_DOC_COMMENT_STRING - assumed 'T_DOC_COMMENT_STRING'

Use of undefined constant T_DOC_XXXXXXXXXX - assumed 'T_DOC_XXXXXX'

I am getting a lot of these errors when running the command. They come from different files located under /vendor/pragmarx/laravelcs/Standards/Laravel/Sniffs/

I am unable to install this globally using composer, because I need to deploy it at build time on a continuous integration server. So, I have added it to the composer.json file for the laravel project.

I call it like this

phpcs --standard=./vendor/pragmarx/laravelcs/Standards/Laravel /path/to/laravel

I am running
PHP 5.5.22-1+deb.sury.org~precise+1 (cli) (built: Feb 20 2015 11:25:27)

Thanks!

Your requirements could not be resolved to an installable set of packages.

Hi, when I tried to install I got this error:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for pragmarx/laravelcs ^0.1.0 -> satisfiable by pragmarx/laravelcs[v0.1.0].
    - pragmarx/laravelcs v0.1.0 requires squizlabs/php_codesniffer ~2.0 -> satisfiable by squizlabs/php_codesniffer[2.0.0, 2.0.0RC1, 2.0.0RC2, 2.0.0RC3, 2.0.0RC4, 2.0.0a1, 2.0.0a2, 2.1.0, 2.2.0, 2.3.0, 2.3.1, 2.3.2, 2.3.3, 2.3.4, 2.4.0, 2.5.0, 2.5.1, 2.6.0, 2.6.1, 2.6.2, 2.7.0, 2.7.1, 2.8.0, 2.8.1, 2.9.0, 2.9.1, 2.9.2, 2.9.x-dev] but these conflict with your requirements or minimum-stability.

Installation failed, reverting ./composer.json to its original content.

Now, what do I do?

Deprecation notice

Hello,

Looks like this package is not being maintained. I've found documentation pointing to Laravael 4.2 which uses and old coding standard in comparison con current version 6.0.

It'd be nice to reference this repository for an up to date version of this coding standard ruleset.

https://github.com/emielmolenaar/phpcs-laravel

Regards

PHP Fatal error: Interface 'PHP_CodeSniffer_Sniff' not found

Hi there:

I am getting a fatal error when attempting to use your standard while running phpcs from the command line.

Here is the command I'm using:

$  phpcs --standard=Laravel /Users/govinda/Sites/mysite/app/Models/Blog.php
PHP Fatal error:  Interface 'PHP_CodeSniffer_Sniff' not found in /Users/govinda/.composer/vendor/pragmarx/laravelcs/Standards/Laravel/Sniffs/ControlStructures/ControlSignatureSniff.php on line 12

PHP Stack trace:
PHP   1. {main}() /Users/govinda/pear/bin/phpcs:0
PHP   2. PHP_CodeSniffer\Runner->runPHPCS() /Users/govinda/pear/bin/phpcs:18
PHP   3. PHP_CodeSniffer\Runner->init() /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Runner.php:70
PHP   4. PHP_CodeSniffer\Ruleset->__construct($config = *uninitialized*) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Runner.php:294
PHP   5. PHP_CodeSniffer\Ruleset->registerSniffs($files = *uninitialized*, $restrictions = *uninitialized*, $exclusions = *uninitialized*) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Ruleset.php:215
PHP   6. PHP_CodeSniffer\Autoload::loadFile($path = *uninitialized*) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Ruleset.php:1103
PHP   7. include() /Users/govinda/pear/share/pear/PHP/CodeSniffer/autoload.php:167

Fatal error: Interface 'PHP_CodeSniffer_Sniff' not found in /Users/govinda/.composer/vendor/pragmarx/laravelcs/Standards/Laravel/Sniffs/ControlStructures/ControlSignatureSniff.php on line 12

Call Stack:
    0.0328     364632   1. {main}() /Users/govinda/pear/bin/phpcs:0
    0.0669     519088   2. PHP_CodeSniffer\Runner->runPHPCS() /Users/govinda/pear/bin/phpcs:18
    0.1921     872736   3. PHP_CodeSniffer\Runner->init() /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Runner.php:70
    0.2551    1094504   4. PHP_CodeSniffer\Ruleset->__construct(???) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Runner.php:294
    0.2847    1111192   5. PHP_CodeSniffer\Ruleset->registerSniffs(???, ???, ???) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Ruleset.php:215
    0.2847    1111192   6. PHP_CodeSniffer\Autoload::loadFile(???) /Users/govinda/pear/share/pear/PHP/CodeSniffer/src/Ruleset.php:1103
    0.3235    1150880   7. include('/Users/govinda/.composer/vendor/pragmarx/laravelcs/Standards/Laravel/Sniffs/ControlStructures/ControlSignatureSniff.php') /Users/govinda/pear/share/pear/PHP/CodeSniffer/autoload.php:167

In contrast, phpcs seems to work fine with PSR2. For example, the following command outputs normally:

$  phpcs --standard=PSR2 /Users/govinda/Sites/mysite/app/Models/Blog.php
FILE: /Users/govinda/Sites/mysite/app/Models/Blog.php
--------------------------------------------------------------------------------------------------------------------------
FOUND 209 ERRORS AND 6 WARNINGS AFFECTING 175 LINES
--------------------------------------------------------------------------------------------------------------------------
  14 | ERROR   | [x] Opening brace of a class must be on the line after the definition
  16 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
...

Some of my system info:

PHP_CodeSniffer version 3.1.1 (stable) by Squiz (http://www.squiz.net)
PEAR Version: 1.10.5
PHP Version: 7.1.8
Zend Engine Version: 3.1.0
Running on: Darwin Govindas-iMac.local 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

Is this is a namespace issue?

document Laravel as default

Nice! Please add the following to README.md to make Laravel the default standard.
Link (or copy) Laravel into the global CodeSniffer directory.
Mac OSX Homebrew example:
ln -s LaravelCodeSniffer/Standards/Laravel/ /usr/local/etc/php-code-sniffer/Standards/
Set Laravel as default:
phpcs --config-set default_standard Laravel
Details at https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options
AFAIK, this also applies, and simplifies https://github.com/FriendsOfPHP/PHP-CS-Fixer

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.