Skip to content

Commit 6936deb

Browse files
committed
When passing content via STDIN, you can now specify the file path to use on the command line (request #934)
1 parent 7fe54a2 commit 6936deb

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CodeSniffer/CLI.php

+12-2
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['stdinPath'] = '';
289290

290291
$reportFormat = PHP_CodeSniffer::getConfigData('report_format');
291292
if ($reportFormat !== null) {
@@ -640,6 +641,13 @@ public function processLongArgument($arg, $pos)
640641

641642
$this->values['bootstrap'][] = $path;
642643
}
644+
} else if (substr($arg, 0, 11) === 'stdin-path=') {
645+
$this->values['stdinPath'] = PHP_CodeSniffer::realpath(substr($arg, 11));
646+
647+
// It may not exist and return false instead, so just use whatever they gave us.
648+
if ($this->values['stdinPath'] === false) {
649+
$this->values['stdinPath'] = trim(substr($arg, 11));
650+
}
643651
} else if (substr($arg, 0, 12) === 'report-file=') {
644652
$this->values['reportFile'] = PHP_CodeSniffer::realpath(substr($arg, 12));
645653

@@ -1205,7 +1213,7 @@ public function printUsage()
12051213
*/
12061214
public function printPHPCSUsage()
12071215
{
1208-
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors]'.PHP_EOL;
1216+
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors] [--stdin-path=<stdinPath>]'.PHP_EOL;
12091217
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL;
12101218
echo ' [--report-width=<reportWidth>] [--generator=<generator>] [--tab-width=<tabWidth>]'.PHP_EOL;
12111219
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
@@ -1228,6 +1236,7 @@ public function printPHPCSUsage()
12281236
echo ' --colors Use colors in output'.PHP_EOL;
12291237
echo ' --no-colors Do not use colors in output (this is the default)'.PHP_EOL;
12301238
echo ' <file> One or more files and/or directories to check'.PHP_EOL;
1239+
echo ' <stdinPath> If processing STDIN, the file path that STDIN will be processed as '.PHP_EOL;
12311240
echo ' <bootstrap> A comma separated list of files to run before processing starts'.PHP_EOL;
12321241
echo ' <encoding> The encoding of the files being checked (default is iso-8859-1)'.PHP_EOL;
12331242
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL;
@@ -1260,7 +1269,7 @@ public function printPHPCSUsage()
12601269
*/
12611270
public function printPHPCBFUsage()
12621271
{
1263-
echo 'Usage: phpcbf [-nwli] [-d key[=value]]'.PHP_EOL;
1272+
echo 'Usage: phpcbf [-nwli] [-d key[=value]] [--stdin-path=<stdinPath>]'.PHP_EOL;
12641273
echo ' [--standard=<standard>] [--sniffs=<sniffs>] [--suffix=<suffix>]'.PHP_EOL;
12651274
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
12661275
echo ' [--tab-width=<tabWidth>] [--encoding=<encoding>]'.PHP_EOL;
@@ -1274,6 +1283,7 @@ public function printPHPCBFUsage()
12741283
echo ' --version Print version information'.PHP_EOL;
12751284
echo ' --no-patch Do not make use of the "diff" or "patch" programs'.PHP_EOL;
12761285
echo ' <file> One or more files and/or directories to fix'.PHP_EOL;
1286+
echo ' <stdinPath> If processing STDIN, the file path that STDIN will be processed as '.PHP_EOL;
12771287
echo ' <bootstrap> A comma separated list of files to run before processing starts'.PHP_EOL;
12781288
echo ' <encoding> The encoding of the files being fixed (default is iso-8859-1)'.PHP_EOL;
12791289
echo ' <extensions> A comma separated list of file extensions to fix'.PHP_EOL;

CodeSniffer/File.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,11 @@ public function start($contents=null)
459459
// If this is standard input, see if a filename was passed in as well.
460460
// This is done by including: phpcs_input_file: [file path]
461461
// as the first line of content.
462-
if ($this->_file === 'STDIN' && $contents !== null) {
463-
if (substr($contents, 0, 17) === 'phpcs_input_file:') {
462+
if ($this->_file === 'STDIN') {
463+
$cliValues = $this->phpcs->cli->getCommandLineValues();
464+
if ($cliValues['stdinPath'] !== '') {
465+
$this->_file = $cliValues['stdinPath'];
466+
} else if ($contents !== null && substr($contents, 0, 17) === 'phpcs_input_file:') {
464467
$eolPos = strpos($contents, $this->eolChar);
465468
$filename = trim(substr($contents, 17, ($eolPos - 17)));
466469
$contents = substr($contents, ($eolPos + strlen($this->eolChar)));

package.xml

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
3232
-- Absolute paths are still allowed and work the same way they always have
3333
-- This change allows ruleset.xml files to be more portable
3434
- Content passed via STDIN will now be processed even if files are specified on the command line or in a ruleset
35+
- When passing content via STDIN, you can now specify the file path to use on the command line (request #934)
36+
-- This allows sniffs that check file paths to work correctly
37+
-- This is the same functionality provided by the phpcs_input_file line, except it is available on the command line
3538
- Files processed with custom tokenizers will no longer be skipped if they appear minified (request #877)
3639
-- If the custom tokenizer wants minified files skipped, it can set a $skipMinified member var to TRUE
3740
-- See the included JS and CSS tokenizers for an example

0 commit comments

Comments
 (0)