NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Integrating CodeSniffer with PHPEd


Joined: 24 Oct 2011
Posts: 3
Reply with quote
http://pear.php.net/package/PHP_CodeSniffer

Script allows to jump to the error number line from the log window (read this)

Command line: "c:\Program Files\NuSphere\PhpED\php5\php.exe" "c:\Program Files\NuSphere\PhpED\scripts\codesnifferwrapper.php" "@FName@"

Code:

/**
* CodeSnifferWrapper
*
* Convert CodeSniffer output to PhpEd`s log window format
* @author Sergey Glagolev <sglagolev@gmail.com>
*/
class CodeSnifferWrapper
{
  const PATH_TO_PHP = "c:\\Program Files\\NuSphere\\PhpED\\php5\\";

  private $filename;
  private $params;
  private $output = array();

  public function __construct(array $arguments, array $params = array())
  {
    if( !empty($arguments[1]) )
    {
      $this->filename = "\"".$arguments[1]."\"";
      $this->params   = http_build_query($params, '', ' ');
    }
    else
      die('Wrong file name');
  }

  public function run()
  {
    $cmd = implode(" ", array('phpcs.bat', $this->filename, $this->params));
    $this->exec($cmd, self::PATH_TO_PHP);
    $this->parse();
    $this->send();
  }

  private function exec($cmd, $dir = '')
  {
    if( !empty($dir) )
      chdir($dir);

    exec($cmd, $this->output);
  }

  private function parse()
  {
    if( is_array($this->output) )
    {
      foreach($this->output as $key => $value)
      {
        $p = preg_replace("/^\s+(\d+)(.*)$/", $this->filename." ($1) $2", $value);
        $this->output[$key] = $p;
      }
    }
  }

  private function send()
  {
    echo implode("\n", $this->output);
  }
}
//------------------------------------------------------------------------------
$script_params = array(
  '--standard'     => 'Zend',
  '--report-width' => '200'
);

$wrapper = new CodeSnifferWrapper($argv, $script_params);
$wrapper->run();
//------------------------------------------------------------------------------
View user's profileFind all posts by seregaglSend private message


Joined: 03 Jul 2012
Posts: 1
Reply with quote
Thanks Smile

php-standard PSR-0, PSR-1 and PSR-2

Code:
https://github.com/klaussilveira/phpcs-psr


Edit : not able to get it to work ...

Code:

Script   PHP Catchable fatal error:  Argument 1 passed to CodeSnifferWrapper::__construct() must be an array, null given, called in C:\Program Files\NuSphere\PhpED\scripts\codesnifferwrapper.php on line 67 and defined in C:\Program Files\NuSphere\PhpED\scripts\codesnifferwrapper.php on line 16   11:56:31 AM   
View user's profileFind all posts by z2zSend private message


Joined: 08 Nov 2012
Posts: 1
Reply with quote
Everything works deduces
But in the graph log: Location
shows first error
and then in the other files it displays the same mistake (Location hang)

screenshot url: clip2net.com/s/2uLMo

use phped 8.1 (8108)

--------
also, this is the kind of code that causes hang log: Location

<?php
echo "D:\\WebServer\\domains\\project1.ru\\test1.php (2) \n";
echo "D:\\WebServer\\domains\\project1.ru\\test1.php (3) \n";
echo "D:\\WebServer\\domains\\project1.ru\\test1.php (4) \n";
?>

------------------------------
File location regexp: ^([^ ]+) (\([0-9]*\)) .*$
(or say Output location regexp)
by applying this regexp on each line in the output, IDE will get array of two elements (when matched, of course). And one of the elements will be file name and the other is line number.
-----------------------------------
who knows how to solve the problem? Sad
View user's profileFind all posts by CubSend private message
How I made PhpEd work with the CodeSniffer


Joined: 13 Dec 2012
Posts: 2
Reply with quote
This article describe installation for Windows (XP) system.

I wasn't able to install actual version of a Pear in a PhpEd/php directory (there was a lots of problems with Pear), so I use the a xampp/php directory.

Code:

#>cd xampp/php
#>download [url]http://pear.php.net/go-pear.phar[/url]
#>php go-pear.phar
#>pear upgrade pear
#>pear install PHP_CodeSniffer-1.4.3


And for example I use CakePHP framework
- download a file:


- uzip the file into a directory: xampp\php\PEAR\PHP\CodeSniffer\Standards

- rename a cakephp-codesniffer-master directory to CakePHP

- go to the Tools -> Settings -> Integration menu

- use "AddMenu" button and set the "Menu Name" to CodeSniff

- set "Command Line" to:
<full_path>\xampp\php\php.exe" -d include_path="'<full_path>\xampp\php\pear'" -f "<full_path>\xampp\php\phpcs" -- --standard=CakePHP "@FName@"

- set Options:
    - CHECK - Show this command in Workspace popup for files
    - CHECK - Show this command in File Bar popup
    - CHECK - Redirect Error stream to log window
    - SET - Save Output to file C:\Temp\CodeSniff.log
    - CHECK - Show this command for local files only
    - SET - Open file in editor/browser C:\Temp\CodeSniff.log



also You can use the CodeSnifferWrapper improvement
- save the script code above to <full_path>\xampp\php\codesnifferwrapper.php
- change the PATH_TO_PHP to the value const PATH_TO_PHP = "<full_path>\\xampp\\php\\";
- set "Command Line" to:
<full_path>\xampp\php\php.exe" -d include_path="'<full_path>\xampp\php\pear'" -f "<full_path>\xampp\php\codesnifferwrapper" "@FName@"

- set Options:
    - CHECK - Show this command in Workspace popup for files
    - CHECK - Show this command in Tools menu
    - CHECK - Show this command in File Bar popup
    - CHECK - Redirect Output stream to log window
    - CHECK - Redirect Error stream to log window



Good luck.
View user's profileFind all posts by grossmanSend private message
Another CodeSniffer integration approach


Joined: 27 Feb 2014
Posts: 4
Reply with quote
In my case CodeSniffer runs on dev server when code is about to get merged. It is generating a report file which must be processed before code ia accepted.

As you see I don't need to run CodeSniffer from PhpED IDE, but rather need a tool for CS log processing.

I wrote a small script to process an open in editor CS log and output errors to Log window with clickable error messages. Clicking on the error message jumps to the referring file/line.

Code:

<?php
//------------------------------------------------------------------------------
/**
* CodeSnifferWrapper
*
* Convert CodeSniffer output to PhpEd`s log window format
* @author Fast Eddie, 2014
*/
class CodeSnifferWrapper
{
  private $remote_path;

  public function __construct(array $arguments)
  {
   if( !empty($arguments[1]) )
   {
     $this->remote_path = $arguments[1];

   }
   else
     die('Wrong file name');
  }

  public function run()
  {
   $contents = file("php://stdin", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
   $curr_file = "";
   foreach ($contents as $key => $value) {
      $row = trim($value);
      if (substr($row,0,5) == 'FILE:') {
         $curr_file = str_replace($this->remote_path, '', substr($row,6));
         $curr_file = str_replace('/', '\\', $curr_file);
      }
      if (!empty($row)) {
         $error_message = explode('|',$row);
         if (isset($error_message[1])) {
            if (trim($error_message[1]) == 'ERROR') {
                $error_message[0] = "\"{$curr_file}\" (".trim($error_message[0]).")";
                echo implode(' | ',$error_message)."\n";
            }
         }
      }
   }
  }
}
//------------------------------------------------------------------------------
$wrapper = new CodeSnifferWrapper($argv);
$wrapper->run();
//------------------------------------------------------------------------------
?>


To setup:

Tools->Settings->Integration

Click "Add menu"
Name it as you wish. I named mine "CodeSniffer digest"

Execute with: Shell
Command line: @php53cli@ "/path/to/script/above/script.php" "/path/to/remove/from/log/"

Little comment on "/path/to/remove/from/log/":
CS outputs errors like this

Code:

FILE: /home/dev/code/vip/module/ConsoleVIP/src/ConsoleVIP/Controller/CliController.php
-------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
-------------------------------------------------------------------------------------------------------------------------
 10 | ERROR | Opening brace should be on a new line
-------------------------------------------------------------------------------------------------------------------------

Time: 105 ms, Memory: 3.75Mb


and "/home/dev/code/" must be removed to make PhpED correctly find local file. What's left must be a relative path to your local project/file

CHECK Show this command in File Bar popup
CHECK Work with editor
CHECK Take input from
CHECK Whole file

CHECK Redirect Output stream to log window
CHECK Redirect Error stream to log window


That's all. Now open CS log file in PhpED editor and right-click on the file name (tabs above editing area). Select "CodeSniffer digest". Enjoy Smile
View user's profileFind all posts by enaumchukSend private message
Integrating CodeSniffer with PHPEd
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic