Skip to content

Commit

Permalink
[PMakowski] squizlabs#2.5.1.x-dev: Adding option to define editor pat…
Browse files Browse the repository at this point in the history
…h and open files in that during interactive session
  • Loading branch information
Piotr Makowski committed Mar 16, 2016
1 parent 6731851 commit f97d5dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 26 additions & 1 deletion CodeSniffer.php
Expand Up @@ -379,6 +379,22 @@ public function setInteractive($interactive)
}//end setInteractive()


/**
* Sets the interactive flag.
*
* @param string $path During interactive session will use this to open invalid files.
*
* @return void
*/
public function setEditorPath($path)
{
if (defined('PHP_CODESNIFFER_EDITOR_PATH') === false) {
define('PHP_CODESNIFFER_EDITOR_PATH', $path);
}

}//end setEditorPath()


/**
* Sets an array of file extensions that we will allow checking of.
*
Expand Down Expand Up @@ -1773,7 +1789,11 @@ public function processFile($file, $contents=null)
$reportData = $this->reporting->prepareFileReport($phpcsFile);
$reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']);

echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
if (empty(PHP_CODESNIFFER_EDITOR_PATH)) {
echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
} else {
echo '<ENTER> to recheck, [s] to skip, [o] to open in editor or [q] to quit : ';
}
$input = fgets(STDIN);
$input = trim($input);

Expand All @@ -1783,6 +1803,11 @@ public function processFile($file, $contents=null)
case 'q':
exit(0);
break;
case 'o':
if (false == empty(PHP_CODESNIFFER_EDITOR_PATH)) {
exec(PHP_CODESNIFFER_EDITOR_PATH.' '.$file);
}
break;
default:
// Repopulate the sniffs because some of them save their state
// and only clear it when the file changes, but we are rechecking
Expand Down
15 changes: 14 additions & 1 deletion CodeSniffer/CLI.php
Expand Up @@ -285,6 +285,7 @@ public function getDefaults()
$defaults['bootstrap'] = array();
$defaults['errorSeverity'] = null;
$defaults['warningSeverity'] = null;
$defaults['editorPath'] = null;

$reportFormat = PHP_CodeSniffer::getConfigData('report_format');
if ($reportFormat !== null) {
Expand Down Expand Up @@ -704,6 +705,17 @@ public function processLongArgument($arg, $pos)
$this->values['errorSeverity'] = (int) substr($arg, 15);
} else if (substr($arg, 0, 17) === 'warning-severity=') {
$this->values['warningSeverity'] = (int) substr($arg, 17);
} else if (substr($arg, 0, 12) === 'editor-path=') {
$value = substr($arg, 12);
if (preg_match('/^[\'\"]/', $value, $matches)) {
while (0 === preg_match("/[{$matches[0]}]$/", $value)) {
$value .= ' '.$this->_cliArgs[++$pos];
$this->_cliArgs[$pos] = '';
}
$value = substr($value, 1, -1);
}

$this->values['editorPath'] = $value;
} else if (substr($arg, 0, 7) === 'ignore=') {
// Split the ignore string on commas, unless the comma is escaped
// using 1 or 3 slashes (\, or \\\,).
Expand Down Expand Up @@ -847,6 +859,7 @@ public function process($values=array())
$phpcs->setTabWidth($values['tabWidth']);
$phpcs->setEncoding($values['encoding']);
$phpcs->setInteractive($values['interactive']);
$phpcs->setEditorPath($values['editorPath']);

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

0 comments on commit f97d5dc

Please sign in to comment.