Home Download Buy Blog Forum Support

PHP Code Sniffer

PHP Code Sniffer

Postby dzdrazil on Wed Sep 08, 2010 6:34 pm

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: Select all
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])

Also, I happen to prefer having this bound to F8 as such:
Code: Select all
<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.
dzdrazil
 
Posts: 3
Joined: Wed Sep 08, 2010 12:57 pm

Re: PHP Code Sniffer

Postby asermar on Tue Jan 24, 2012 5:29 pm

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
Code: Select all
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
Code: Select all
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
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm

Re: PHP Code Sniffer

Postby svenax on Tue Jan 24, 2012 10:50 pm

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)
Code: Select all
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.
svenax
 
Posts: 155
Joined: Tue Dec 20, 2011 12:47 pm

Re: PHP Code Sniffer

Postby asermar on Wed Jan 25, 2012 9:43 am

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:
Code: Select all
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
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm

Re: PHP Code Sniffer

Postby asermar on Thu Jan 26, 2012 11:15 pm

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

Code: Select all
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)
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm

Re: PHP Code Sniffer

Postby asermar on Fri Jan 27, 2012 12:47 pm

Hi,
The actual code for the plugin is
Code: Select all
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.
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm

Re: PHP Code Sniffer

Postby asermar on Fri Jan 27, 2012 1:24 pm

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.
Code: Select all
[
     {
        "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"
                    }
                }
            ]   
        }]
    }
]
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm

Re: PHP Code Sniffer

Postby lexusburn on Fri Mar 30, 2012 8:38 am

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
lexusburn
 
Posts: 1
Joined: Fri Mar 30, 2012 8:35 am

Re: PHP Code Sniffer

Postby asermar on Tue Apr 03, 2012 10:36 am

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: http://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
asermar
 
Posts: 8
Joined: Tue Jan 24, 2012 5:24 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 9 guests