Skip to content

Commit d1a39f9

Browse files
committed
Allow using default standards location in phpcs-diff, instead of the built-in ones.
1 parent 4cba5e4 commit d1a39f9

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed

bin/phpcs-diff

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace PHPCSDiff;
1313

14+
use GetOpt\GetOpt;
1415
use PHPCSDiff\Log\LoggerInterface;
1516

1617
define( 'PHPCS_DIFF_PLUGIN_DIR', str_replace( '\\', '/', dirname( dirname( __FILE__ ) ) ) );
@@ -32,24 +33,26 @@ echo "\n$logo\n" . OUTPUT_UNDERLINED . COLOUR_SECONDARY . 'https://github.com/za
3233

3334
require_once PHPCS_DIFF_PLUGIN_DIR . '/vendor/autoload.php';
3435

35-
$getopt = new \GetOpt\GetOpt(
36+
$getopt = new GetOpt(
3637
[
37-
[ 's', 'start_revision', \GetOpt\GetOpt::REQUIRED_ARGUMENT ],
38-
[ 'e', 'end_revision', \GetOpt\GetOpt::REQUIRED_ARGUMENT ],
38+
[ 's', 'start_revision', GetOpt::REQUIRED_ARGUMENT ],
39+
[ 'e', 'end_revision', GetOpt::REQUIRED_ARGUMENT ],
3940
[ 't', 'tolerance' ],
40-
[ 'standard', \GetOpt\GetOpt::REQUIRED_ARGUMENT ],
41+
[ 'standard', GetOpt::REQUIRED_ARGUMENT ],
4142
[ 'log_level' ],
42-
[ 'ignore_space_changes', \GetOpt\GetOpt::NO_ARGUMENT ],
43-
[ 'sniff_unstaged', \GetOpt\GetOpt::NO_ARGUMENT ],
44-
[ 'no_colours', \GetOpt\GetOpt::NO_ARGUMENT ],
45-
[ 'colour_primary', \GetOpt\GetOpt::REQUIRED_ARGUMENT ],
46-
[ 'colour_secondary', \GetOpt\GetOpt::REQUIRED_ARGUMENT ],
47-
[ 'excluded_exts', \GetOpt\GetOpt::OPTIONAL_ARGUMENT ],
43+
[ 'ignore_space_changes', GetOpt::NO_ARGUMENT ],
44+
[ 'sniff_unstaged', GetOpt::NO_ARGUMENT ],
45+
[ 'no_colours', GetOpt::NO_ARGUMENT ],
46+
[ 'colour_primary', GetOpt::REQUIRED_ARGUMENT ],
47+
[ 'colour_secondary', GetOpt::REQUIRED_ARGUMENT ],
48+
[ 'excluded_exts', GetOpt::OPTIONAL_ARGUMENT ],
49+
[ 'standards_location', GetOpt::OPTIONAL_ARGUMENT ],
50+
[ 'default_standards_location', GetOpt::NO_ARGUMENT ],
4851
]
4952
);
5053

5154
$getopt->getOption( 'tolerance', true )
52-
->setMode( \GetOpt\GetOpt::OPTIONAL_ARGUMENT )
55+
->setMode( GetOpt::OPTIONAL_ARGUMENT )
5356
->setValidation(
5457
function ( $value ) {
5558
return in_array( $value, [ 'blockers', 'warnings', 'notes', 'none' ] );
@@ -58,7 +61,7 @@ $getopt->getOption( 'tolerance', true )
5861
->setDefaultValue( 'notes' );
5962

6063
$getopt->getOption( 'log_level', true )
61-
->setMode( \GetOpt\GetOpt::OPTIONAL_ARGUMENT )
64+
->setMode( GetOpt::OPTIONAL_ARGUMENT )
6265
->setValidation( 'is_numeric' )
6366
->setDefaultValue( LoggerInterface::WARNING );
6467

@@ -78,6 +81,8 @@ $log_level = $getopt->getOption( 'log_level' );
7881
$ignore_space_changes = $getopt->offsetExists( 'ignore_space_changes' );
7982
$no_colours = $getopt->offsetExists( 'no_colours' );
8083
$excluded_exts = $getopt->getOption( 'excluded_exts' );
84+
$standards_location = $getopt->getOption( 'standards_location' );
85+
$default_standards_location = $getopt->getOption( 'default_standards_location' );
8186

8287
$sniff_unstaged = $getopt->offsetExists( 'sniff_unstaged' );
8388
if ( $sniff_unstaged ) {
@@ -125,8 +130,14 @@ if ( $no_colours ) {
125130
$colour_secondary = $getopt->offsetExists( 'colour_secondary' ) ? arg_to_colour( $getopt->getOption( 'colour_secondary' ), COLOUR_SECONDARY ) : COLOUR_SECONDARY;
126131
}
127132

133+
if( $default_standards_location ) {
134+
$standards_location = null;
135+
} elseif( null === $standards_location ) {
136+
$standards_location = PHPCS_DIFF_STANDARDS;
137+
}
138+
128139
$logger = new Log\ShellLogger( (int) $log_level );
129-
$options = [ 'ignore-space-change' => $ignore_space_changes ];
140+
$options = [ 'ignore-space-change' => $ignore_space_changes, 'standards_location' => $standards_location ];
130141
$version_control = new Backends\Git( '', $logger, $options );
131142
$controller = new Main( $version_control, $logger, $options );
132143
$controller->set_phpcs_standard( $phpcs_standard );

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"otgs/toolset-coding-standards": "dev-master",
2020
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
2121
"sebastian/diff": "^3.0",
22-
"ulrichsg/getopt-php": "^3.0",
22+
"ulrichsg/getopt-php": "3.2.1",
2323
"slowprog/composer-copy-file": "~0.2",
2424
"sirbrillig/phpcs-variable-analysis": "^2.1"
2525
},

composer.lock

Lines changed: 32 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/backends/Git.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,16 @@ public function run_phpcs_for_file_at_revision( $filename, $revision, $phpcs_com
195195
$phpcs_standard_arg = '';
196196
}
197197

198+
if( null !== $standards_location ) {
199+
$standards_location_arg = sprintf( '--runtime-set installed_paths %s', escapeshellarg( $standards_location ) );
200+
} else {
201+
$standards_location_arg = '';
202+
}
203+
198204
$phpcs_command = sprintf(
199-
'%s --report=json --runtime-set installed_paths %s %s --stdin-path=%s -',
205+
'%s --report=json %s %s --stdin-path=%s -',
200206
escapeshellcmd( $phpcs_command ),
201-
escapeshellarg( $standards_location ),
207+
$standards_location_arg,
202208
$phpcs_standard_arg,
203209
escapeshellarg( $filename )
204210
);
@@ -207,4 +213,4 @@ public function run_phpcs_for_file_at_revision( $filename, $revision, $phpcs_com
207213

208214
return shell_exec( $command_string );
209215
}
210-
}
216+
}

0 commit comments

Comments
 (0)