Skip to content

Commit a47248b

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 4e15784 commit a47248b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-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
*
@@ -1780,7 +1796,11 @@ public function processFile($file, $contents=null)
17801796
$reportData = $this->reporting->prepareFileReport($phpcsFile);
17811797
$reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']);
17821798

1783-
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
1799+
if (empty(PHP_CODESNIFFER_EDITOR_PATH)) {
1800+
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
1801+
} else {
1802+
echo '<ENTER> to recheck, [s] to skip, [o] to open in editor or [q] to quit : ';
1803+
}
17841804
$input = fgets(STDIN);
17851805
$input = trim($input);
17861806

@@ -1790,6 +1810,11 @@ public function processFile($file, $contents=null)
17901810
case 'q':
17911811
exit(0);
17921812
break;
1813+
case 'o':
1814+
if (false == empty(PHP_CODESNIFFER_EDITOR_PATH)) {
1815+
exec(PHP_CODESNIFFER_EDITOR_PATH.' '.$file);
1816+
}
1817+
break;
17931818
default:
17941819
// Repopulate the sniffs because some of them save their state
17951820
// and only clear it when the file changes, but we are rechecking

CodeSniffer/CLI.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public function getDefaults()
286286
$defaults['errorSeverity'] = null;
287287
$defaults['warningSeverity'] = null;
288288
$defaults['stdin'] = null;
289+
$defaults['editorPath'] = null;
289290

290291
$reportFormat = PHP_CodeSniffer::getConfigData('report_format');
291292
if ($reportFormat !== null) {
@@ -722,6 +723,17 @@ public function processLongArgument($arg, $pos)
722723
$this->values['errorSeverity'] = (int) substr($arg, 15);
723724
} else if (substr($arg, 0, 17) === 'warning-severity=') {
724725
$this->values['warningSeverity'] = (int) substr($arg, 17);
726+
} else if (substr($arg, 0, 12) === 'editor-path=') {
727+
$value = substr($arg, 12);
728+
if (preg_match('/^[\'\"]/', $value, $matches)) {
729+
while (0 === preg_match("/[{$matches[0]}]$/", $value)) {
730+
$value .= ' '.$this->_cliArgs[++$pos];
731+
$this->_cliArgs[$pos] = '';
732+
}
733+
$value = substr($value, 1, -1);
734+
}
735+
736+
$this->values['editorPath'] = $value;
725737
} else if (substr($arg, 0, 7) === 'ignore=') {
726738
// Split the ignore string on commas, unless the comma is escaped
727739
// using 1 or 3 slashes (\, or \\\,).
@@ -865,6 +877,7 @@ public function process($values=array())
865877
$phpcs->setTabWidth($values['tabWidth']);
866878
$phpcs->setEncoding($values['encoding']);
867879
$phpcs->setInteractive($values['interactive']);
880+
$phpcs->setEditorPath($values['editorPath']);
868881

869882
// Set file extensions if they were specified. Otherwise,
870883
// let PHP_CodeSniffer decide on the defaults.
@@ -1205,7 +1218,7 @@ public function printUsage()
12051218
*/
12061219
public function printPHPCSUsage()
12071220
{
1208-
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors]'.PHP_EOL;
1221+
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors] [--editor-path=<path>]'.PHP_EOL;
12091222
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL;
12101223
echo ' [--report-width=<reportWidth>] [--generator=<generator>] [--tab-width=<tabWidth>]'.PHP_EOL;
12111224
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;

0 commit comments

Comments
 (0)