Skip to content

Commit 169a4f9

Browse files
author
Piotr Makowski
committed
[PMakowski] squizlabs#2.5.1.x-dev: Adding option to define editor path and open files in that during interactive session
1 parent 6731851 commit 169a4f9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

CodeSniffer.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ public function setInteractive($interactive)
379379
}//end setInteractive()
380380

381381

382+
/**
383+
* Sets the interactive flag.
384+
*
385+
* @param string $path During interactive session will use this to open invalid files.
386+
*
387+
* @return void
388+
*/
389+
public function setEditorPath($path)
390+
{
391+
if (defined('PHP_CODESNIFFER_EDITOR_PATH') === false) {
392+
define('PHP_CODESNIFFER_EDITOR_PATH', $path);
393+
}
394+
395+
}//end setEditorPath()
396+
397+
382398
/**
383399
* Sets an array of file extensions that we will allow checking of.
384400
*
@@ -1773,7 +1789,11 @@ public function processFile($file, $contents=null)
17731789
$reportData = $this->reporting->prepareFileReport($phpcsFile);
17741790
$reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']);
17751791

1776-
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
1792+
if (empty(PHP_CODESNIFFER_EDITOR_PATH)) {
1793+
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
1794+
} else {
1795+
echo '<ENTER> to recheck, [s] to skip, [o] to open in editor or [q] to quit : ';
1796+
}
17771797
$input = fgets(STDIN);
17781798
$input = trim($input);
17791799

@@ -1783,6 +1803,11 @@ public function processFile($file, $contents=null)
17831803
case 'q':
17841804
exit(0);
17851805
break;
1806+
case 'o':
1807+
if (false == empty(PHP_CODESNIFFER_EDITOR_PATH)) {
1808+
exec(PHP_CODESNIFFER_EDITOR_PATH.' '.$file);
1809+
}
1810+
break;
17861811
default:
17871812
// Repopulate the sniffs because some of them save their state
17881813
// and only clear it when the file changes, but we are rechecking

CodeSniffer/CLI.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ public function getDefaults()
285285
$defaults['bootstrap'] = array();
286286
$defaults['errorSeverity'] = null;
287287
$defaults['warningSeverity'] = null;
288+
$defaults['stdin'] = null;
289+
$defaults['editorPath'] = null;
288290

289291
$reportFormat = PHP_CodeSniffer::getConfigData('report_format');
290292
if ($reportFormat !== null) {
@@ -704,6 +706,17 @@ public function processLongArgument($arg, $pos)
704706
$this->values['errorSeverity'] = (int) substr($arg, 15);
705707
} else if (substr($arg, 0, 17) === 'warning-severity=') {
706708
$this->values['warningSeverity'] = (int) substr($arg, 17);
709+
} else if (substr($arg, 0, 12) === 'editor-path=') {
710+
$value = substr($arg, 12);
711+
if (preg_match('/^[\'\"]/', $value, $matches)) {
712+
while (0 === preg_match("/[{$matches[0]}]$/", $value)) {
713+
$value .= ' '.$this->_cliArgs[++$pos];
714+
$this->_cliArgs[$pos] = '';
715+
}
716+
$value = substr($value, 1, -1);
717+
}
718+
719+
$this->values['editorPath'] = $value;
707720
} else if (substr($arg, 0, 7) === 'ignore=') {
708721
// Split the ignore string on commas, unless the comma is escaped
709722
// using 1 or 3 slashes (\, or \\\,).
@@ -847,6 +860,7 @@ public function process($values=array())
847860
$phpcs->setTabWidth($values['tabWidth']);
848861
$phpcs->setEncoding($values['encoding']);
849862
$phpcs->setInteractive($values['interactive']);
863+
$phpcs->setEditorPath($values['editorPath']);
850864

851865
// Set file extensions if they were specified. Otherwise,
852866
// let PHP_CodeSniffer decide on the defaults.
@@ -1185,7 +1199,7 @@ public function printUsage()
11851199
*/
11861200
public function printPHPCSUsage()
11871201
{
1188-
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors]'.PHP_EOL;
1202+
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors] [--editor-path=<path>]'.PHP_EOL;
11891203
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL;
11901204
echo ' [--report-width=<reportWidth>] [--generator=<generator>] [--tab-width=<tabWidth>]'.PHP_EOL;
11911205
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;

0 commit comments

Comments
 (0)