Sublime Forum

PHP Code Sniffer

#1

This is a simple plugin to run phpcs by plugin. If there is any interest, perhaps this could be expanded to apply some fixes based on the text phpcs returns.

[code]import sublime, sublimeplugin

phpcs.py

This is a terribly simple plugin. Running it will call PHP Codesniffer

and validate your PHP code against the Codesniffer’s version of the

Zend Standards. Output will be shown in the console.

TODO: It should, in theory, be possible to automatically correct certain

errors by parsing the resulting text from phpcs and applying basic

string replacements on the correct lines- for example converting

56 | ERROR | Opening brace should be on a new line

to a regex on line 56 to turn …){… to …) {…

See http://pear.php.net/package/PHP_CodeSniffer/

class PhpcsCommand(sublimeplugin.TextCommand):
def run(self, view, args):
view.window().runCommand(“save”)
cmd = “phpcs --standard=Zend " + view.fileName()
view.window().runCommand(“exec”,”",cmd])[/code]
Also, I happen to prefer having this bound to F8 as such:

<binding key="f8" command="phpcs" />

Another point of improvement could be to add parameters/ multiple bindings to allow users to validate against multiple standards they may have installed.

0 Likes

#2

Hi,
sorry for reopen an old post.
This is the only one I’ve find about using code sniffer inside Sublime Text.

I’ve tried this plugin, but don’t woks.
I’ve created a Phpcs folder inside Packages and phpcs.py with the code

import sublime
import sublime_plugin

class PhpcsCommand(sublime_plugin.TextCommand):
    def run(self, view, args):
        view.window().runCommand("save")
        cmd = "phpcs --standard=Joomla " + view.fileName()
        view.window().runCommand("exec", "", cmd])

Each time I tried to use it I receive this by console

Traceback (most recent call last):
  File "./sublime_plugin.py", line 351, in run_
    return self.run(edit)
TypeError: run() takes exactly 3 arguments (2 given)

I’m not python programmer, so I haven’t find a solution by myself neither google.

I use the Joomla standard and have checked the command works in my console. I’m running kubuntu 11.10 with python 2.7

Some one can help me? thanks

0 Likes

#3

You are trying to use this plugin with Sublime Text 2, right? The API has changed a bit, so the code should look something like this instead (untested)

import sublime
import sublime_plugin

class PhpcsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().run_command("save")
        cmd = "phpcs --standard=Joomla " + self.view.file_name()
        self.view.window().run_command("exec", "", cmd])

I hope it works for you.

0 Likes

#4

Hi svenax, thanks for your reply.
Yes, this is Sublime Text 2.
I’ve change the plugin’s code in the way you quote, now, the fail at console is:

Traceback (most recent call last):
  File "./sublime_plugin.py", line 326, in run_
    return self.run(**args)
TypeError: run() argument after ** must be a mapping, not list
0 Likes

#5

Hi,
I’ve find an updated plugin in kirbymixedmedia.com/hacks-ma … ime-text-2
I’ve adapt the code, and works fine for me.

import sublime
import sublime_plugin


class PhpcsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().run_command('save')
        
        file_name = self.view.file_name()
        self.view.window().run_command("exec", {'cmd': 'phpcs',  '--standard=zend', '--tab-width=4', file_name]})
     	sublime.status_message("CodeSniffer executed on " + folder_name  + "/" + file_name)
0 Likes

#6

Hi,
The actual code for the plugin is

import sublime
import sublime_plugin


class PhpcsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().run_command('save')

        file_name = self.view.file_name()
        self.view.window().run_command("exec", {'cmd': 'phpcs', '--standard=Joomla', '--tab-width=4', file_name]})
        sublime.status_message("CodeSniffer executed on " + file_name)

It works, but looks like it works only once. Each new execution in the same file returns the same CodeSniffer output. I check with a console CodeSniffer run, and it’s correct.

Some one can tell me how to force the refresh?

thanks.

0 Likes

#7

I have find a new solution using SublimeREPL (viewtopic.php?f=5&t=2964)
I’ve make a new config for CodeSniffer in SubliemREPL that is working fine.


     {
        "id": "tools",
        "children":
        {
            "caption": "SublimeREPL",
            "mnemonic": "r",
            "id": "SublimeREPL",
            "children":
            
                {
                    "command": "repl_open", 
                    "caption": "CodeSniffer Joomla",
                    "mnemonic": "j",
                    "args": 
                    {
                        "type": "subprocess",
                        "encoding": "utf8",
                        "cmd": "phpcs", "--standard=Joomla", "--tab-width=4", "$file"],
                        "cwd": "$file_path",
                        "syntax": "Packages/PHP/PHP.tmLanguage",
                        "external_id": "phpcs"
                    }
                }
            ]   
        }]
    }
]
0 Likes

#8

Hello asermar,

i am new at sublime text and new at code sniffer.

can you pls. tell me in a few words what i have to do to add php code sniffer to sublime text?
i have installed php code sniffer and it is working fine on my machine.

had i only to install SublimeREPL and create a config-file like you pasted it anywhere?
Where had i to create this config file? How can i start CodeSniffer then?

best regards,
lexusburn

0 Likes

#9

Hi laxusburn,
Actually I’m using a diferent plugin that is a best solution.
You can find in package control an is call phpcs
You can see here: soulbroken.co.uk/code/sublimephpcs

The SublimeREPL solution is:

  • Install SublimeREPL
  • Create a new folder in packages/sublimerepl/config (my own is CodeSniffer_Joomla)
  • copy paste my previous config file in this folder, with name Main

Best Regards

0 Likes