Giter Site home page Giter Site logo

sublime-php_codesniffer's Introduction

PHP_CodeSniffer Sublime Text 2/3 Plugin

PHP_CodeSniffer Sublime Text Plugin allows running of PHP_CodeSniffer inside Sublime Text.

Running the PHPCS command displays the coding standard violations report and displays gutter markers for the lines that have code violations.

PHPCS screenshot

Running the PHPCBF command attempts to fix the coding standard violations and displays a diff of the changes that were made.

PHPCS Fixer screenshot

Installation

  • Install PHP_CodeSniffer.
  • Clone the PHP_CodeSniffer Sublime Text Plugin in to ST2/ST3 Packages directory.
git clone https://github.com/squizlabs/sublime-PHP_CodeSniffer PHP_CodeSniffer
  • Packages directory locations:
Mac: /Users/{user}/Library/Application Support/Sublime Text 2/Packages
Windows: C:\Users\{user}\AppData\Roaming\Sublime Text 2\Packages
Linux: ~/.config/sublime-text-2/Packages

Configuration

Configuration files can be opened via Preferences > Package Settings > PHP_CodeSniffer.

Make sure the php_path, phpcs_path and phpcbf_path paths are correct. E.g.

"phpcs_path": "/usr/local/bin/phpcs",
"phpcbf_path": "/usr/local/bin/phpcbf",

phpcs_standard

This settings can be the name of a single standard or a list of folder/project names and the standard to be used for each project. E.g.

"phpcs_standard": "Squiz"
"phpcs_standard": {
    "PHP_CodeSniffer": "PHPCS",
    "php-sikuli": "PSR1",
    "Sublime-PHP_CodeSniffer": "PEAR"
}

additional_args

Array containing additional arguments to pass to the PHPCS/PHPCBF scripts.

error_scope & warning_scope

These settings define the colors used for the error and warning gutter markers.

// Gutter error icon color.
"error_scope": "comment.block",

// Gutter warning icon color.
"warning_scope": "function"

run_on_save

If set to true then buffer will be checked on each save.

Usage

There are two shortcuts that can be used for Sublime PHP_CodeSniffer plugin:

  • ALT + S: Runs PHPCS command for the open buffer.
  • ALT + SHIFT + S: Runs PHPCBF command for the open buffer.

These commands are also available in Tools > PHP_CodeSniffer menu.

sublime-php_codesniffer's People

Contributors

gsherwood avatar sertand 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

Watchers

 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

sublime-php_codesniffer's Issues

Configure to use a projects ruleset

Our team is currently in the process of trying to get codesniffer working in sublime.

So far we have been able to set it up and get it running as it should with the default properties.

However, due to our different codebases we ideally need to use a different ruleset per project.

Having looked at the PHP_CodeSniffer documentation it says that If you run PHP_CodeSniffer without specifying a coding standard, PHP_CodeSniffer will look in the current directory, and all parent directories, for a file called either .phpcs.xml, phpcs.xml, .phpcs.xml.dist, or phpcs.xml.dist

I've removed the phpcs_standard parameter from the PHP_CodeSniffer.sublime-settings file but it doesn't seem to be picking up my projects phpcs.xml file at all.

Is there another setting I need to change somewhere to tell this plugin to use my projects phpcs.xml file?

We have installed PHP_CodeSniffer globally via composer.

This is the current PHP_CodeSniffer.sublime-settings file:

{
     // Run PHPCS when a buffer is saved.
    "run_on_save": false,

    // Path to PHP.
    "php_path": "C:/WAMP/PHP/7.1.33/php.exe",

    // Path to the PHPCS script.
    "phpcs_path": "C:/Users/rob/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/bin/phpcs",

    // Path to the PHPCBF script.
    "phpcbf_path": "C:/Users/rob/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/phpcbf",

    // PHPCS Standard to Use. Can be a string or a dict (folder => standard).
    // "phpcs_standard": "Squiz",

    // Additional arguments to pass to PHPCS/PHPCBF.
    "additional_args": [],

    // Gutter error icon colour.
    "error_scope": "comment.block",

    // Gutter warning icon colour.
    "warning_scope": "function"
}

This is what our custom ruleset looks like:

<?xml version="1.0"?>

<ruleset name="Custom-Ruleset">
    <description>Custom Ruleset</description>
    <arg name="tab-width" value="4"/>

     <exclude-pattern>*/vendor/*</exclude-pattern>

    <rule ref="PSR12">
        <exclude name="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose" />
        <exclude name="PSR12.Operators.OperatorSpacing.NoSpaceBefore" />
        <exclude name="PSR12.Operators.OperatorSpacing.NoSpaceAfter" />
        <exclude name="PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon" />
        <exclude name="PEAR.ControlStructures.MultiLineCondition.StartWithBoolean" />
        <exclude name="PEAR.ControlStructures.MultiLineCondition.SpacingAfterOpenBrace" />
    </rule>

    <rule ref="Squiz">
        <exclude name="Squiz.Commenting.ClosingDeclarationComment.Missing" />
        <exclude name="Squiz.Commenting.FileComment.Missing" />
        <exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop" />
        <exclude name="Squiz.Commenting.FunctionComment.SpacingAfterParamType" />
        <exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
        <exclude name="Squiz.Files.FileExtension.ClassFound" />
        <exclude name="Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue" />
        <exclude name="Squiz.Operators.ComparisonOperatorUsage.NotAllowed" />
        <exclude name="Squiz.PHP.DisallowComparisonAssignment.AssignedComparison" />
        <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen" />
        <exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose" />
        <exclude name="Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose" />
        <exclude name="Squiz.WhiteSpace.FunctionOpeningBraceSpace.SpacingAfter" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.After" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.AfterLast" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.Before" />
        <exclude name="Squiz.WhiteSpace.FunctionSpacing.BeforeFirst" />
    </rule>

    <rule ref="Generic.Formatting.SpaceAfterCast">
        <properties>
            <property name="spacing" value="1" />
        </properties>
    </rule>

    <rule ref="Generic.Formatting.SpaceAfterNot">
        <properties>
            <property name="spacing" value="0" />
        </properties>
    </rule>

    <rule ref="Generic.PHP.ForbiddenFunctions">
        <properties>
            <property name="forbiddenFunctions" type="array">
                <element key="count" value="sizeof"/>
                <element key="print" value="echo"/>
            </property>
         </properties>
    </rule>
</ruleset>

The ruleset seems to work fine in other editor plugins (i.e. linter-phpcs for Atom), so I'm fairly certain that I'm missing an important configuration setting in this plugin.

How to View Error Log?

In the documentation you show a screenshot of a pretty error log, in what appears to be the console. When I sniff a file, I do not get any error log. Nor do I get one if I use cs-fixer or phpcb.

Step by step guide for Windows users

Please guide me to use this package in windows 10. I have read the issues and solutions for more than 6hrs and I tried them but all fails. is there anyone who can explain every single step to bring this to work. Please explain how to set the path properly, etc.

The below code is the steps that I have done but it fails at the end:

Install phpcs (PHP Code Sniffer) || phpmd || php-cs-fixer by composer for Sublime Text 3 Windows 10

You must have installed PHP and PHP path in System Evironment to install phpcs,phpmd,php-cs-fixer

Install composer:
- Go to c:\ and create a folder
named bin
- Download composer.phar and put
it in this folder.
- Open cmd, go to c:\bin and run
this code: echo @php "%~dp0composer.phar" %*>composer.bat
- Add c:\bin to invironment
variable.

Install phpcs:
run this code:
composer require "squizlabs/php_codesniffer=*"
ok. phpcs installed.

Install phpmd:
run this code:
composer require "phpmd/phpmd"

Install php-cs-fixer:
open cmd, run this code
composer require friendsofphp/php-cs-fixer

Final, add this folder: C:\bin\vendor\bin to
System Environment.


Add php-cs-fixer & phpcbf for Sublime Text 3 phpcs:
Open Sublime Text 3 -> Preferences -> Package Settings -> PHP Code Sniffer -> Settings --User, add this code:
{
"phpcbf_executable_path": "C:/bin/vendor/bin/phpcbf.bat",
"phpcs_executable_path":"C:/bin/vendor/bin/phpcs.bat",
"phpmd_executable_path":"C:/bin/vendor/bin/phpmd.bat",
"php_cs_fixer_executable_path":"C:/bin/vendor/bin/php-cs-fixer.bat",
}

Windows problem

I am trying to get this to run on Win 10. I get the following error when I try to check a PHP file:

ERROR: Custom report "C:\Users\clf\AppData\Roaming\Sublime Text 3\Packages/PHP_CodeSniffer/STPluginReport.php" not found

run_on_save not working?

Hi there,

I've just installed this and have it working using ALT + S as well as the options to Check/Fix a file in Sublime > Tools. This works great!

However I have in my User config run_on_save set to true but nothing seems to happen after I hit save. Here is my User config:

{
     // Run PHPCS when a buffer is saved.
    "run_on_save": true,

    "phpcs_standard": "PSR2",

    // Path to the PHPCS script.
    "phpcs_path": "/Users/ralphmorris/phpspecs/PHP_CodeSniffer/bin/phpcs",

    // Path to the PHPCBF script.
    "phpcbf_path": "/Users/ralphmorris/phpspecs/PHP_CodeSniffer/bin/phpcbf",
}

If I move a bracket into the wrong position and then do Shift + Option + S it fixes it. But on save, nothing.

I'm unsure how to debug this. Any ideas?

Thanks
Ralph

Windows users

Hi, This is a looking good one plugin ! I'm a windows user and i wanted to know if it's compatible with windows users.
i ve download from the repositery and i ve rename the folder from "sublime-PHP_CodeSniffer-master" to "PHP_CodeSniffer" then put him in \Roaming\Sublim text 3\Packages
i have edited my settings files with this value :

edit after post : "we didn't see double\ in path on the post of this message but they really exist !

// Path to PHP.
"php_path": "C:\Program Files (x86)\EasyPHP\binaries\php\php_runningversion\php.exe",

// Path to the PHPCS script.
"phpcs_path": "C:\php\pear\phpcs.bat",

// Path to the PHPCBF script.
"phpcbf_path": "C:\php\pear\phpcbf.bat",

I've got this error in sublim text console :

@echo off
REM PHP_CodeSniffer tokenizes PHP code and detects violations of a
REM defined set of coding standards.
REM
REM PHP version 5
REM
REM @category PHP
REM @Package PHP_CodeSniffer
REM @author Greg Sherwood [email protected]
REM @author Marc McIntyre [email protected]
REM @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
REM @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
REM @link http://pear.php.net/package/PHP_CodeSniffer

if "%PHPBIN%" == "" set PHPBIN=C:\Program Files (x86)\EasyPHP\binaries\php\php_runningversion\php.exe
if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH
GOTO RUN
:USE_PEAR_PATH
set PHPBIN=%PHP_PEAR_PHP_BIN%
:RUN
"%PHPBIN%" "C:\php\pear\phpcs" %*

Then i have create windows environment variable
PHPBIN with value `C:\Program Files (x86)\EasyPHP\binaries\php\php_runningversion\php.exe
And change :

// Path to PHP.
"php_path": "%PHPBIN%",

the error message in sublime text disappeared
but ! no error is detect in my php file :( seem to be not working.
Do you have a working PHP_CodeSniffer.sublime-settings --User Example ? Thx for help.

Sorry for posting here. I know it's not the best place but it's difficult to contact you. I will delete this post if you ask me. And sry for my english :)
Cheers. Thx in advance.

PHPCBF can't change files outside of home directory

Firstly, thanks for this plugin!

So I'm working on a repository that's not in my home directory. I can sniff it with Sniff this file, but I can't use phpcbf to fix it.

I tried copying the file to my home folder and changing its permissions to be owned by me, and that worked fine. I copied it back to the other folder /home/different_user with my permissions preserved and I still couldn't changed it. On the chance that it was the folder permissions, I changed ownership of the other home folder to myself and it still wouldn't work.

So, I have two identical files, one in my home folder, one in a different home folder (both the other home folder and file owned by me, with identical permissions), and I can only change the one in my home folder.

I glanced at the source, but didn't see anything obvious that would lead to that.

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.