Skip to content

Commit

Permalink
Do not try to validate runtime options when loading bin script (#95)
Browse files Browse the repository at this point in the history
* Change fileHasValidExtension to accept extensions instead of options

* Create info mode so that -i option works
  • Loading branch information
sirbrillig authored Aug 24, 2023
1 parent 03492be commit e31c0ad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions PhpcsChanged/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,19 @@ function reportMessagesAndExit(PhpcsMessages $messages, CliOptions $options, She
exit($reporter->getExitCode($messages));
}

function fileHasValidExtension(\SplFileInfo $file, CliOptions $options = null): bool {
function fileHasValidExtension(\SplFileInfo $file, string $phpcsExtensions = ''): bool {
// The following logic is copied from PHPCS itself. See https://github.com/squizlabs/PHP_CodeSniffer/blob/2ecd8dc15364cdd6e5089e82ffef2b205c98c412/src/Filters/Filter.php#L161
// phpcs:disable

if (empty($options->phpcsExtensions)) {
if (empty($phpcsExtensions)) {
$AllowedExtensions = [
'php',
'inc',
'js',
'css',
];
} else {
$AllowedExtensions = explode(',', $options->phpcsExtensions);
$AllowedExtensions = explode(',', $phpcsExtensions);
}

// Extensions can only be checked for files.
Expand Down
7 changes: 6 additions & 1 deletion PhpcsChanged/CliOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class CliOptions {
*
* Use the `Modes` constants for this purpose rather than the strings.
*
* @var 'svn'|'manual'|'git-staged'|'git-unstaged'|'git-base'|null
* If this is null, validation will fail.
*
* @var 'svn'|'manual'|'git-staged'|'git-unstaged'|'git-base'|'info'|null
*/
public $mode;

Expand Down Expand Up @@ -254,6 +256,9 @@ public static function fromArray(array $options): self {
if (isset($options['error-severity'])) {
$cliOptions->errorSeverity = $options['error-severity'];
}
if (isset($options['i'])) {
$cliOptions->mode = Modes::INFO_ONLY;
}
$cliOptions->validate();
return $cliOptions;
}
Expand Down
1 change: 1 addition & 0 deletions PhpcsChanged/Modes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PhpcsChanged;

class Modes {
const INFO_ONLY = 'info';
const SVN = 'svn';
const MANUAL = 'manual';
const GIT_STAGED = 'git-staged';
Expand Down
2 changes: 1 addition & 1 deletion bin/phpcs-changed
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ foreach( $fileNames as $file ) {
continue;
}
$iterator = new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator(new \RecursiveDirectoryIterator($file, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)), function($file, $key, $iterator) use($options) {
if ($file->isFile() && !fileHasValidExtension($file, CliOptions::fromArray($options))) {
if ($file->isFile() && !fileHasValidExtension($file, isset($options['extensions']) && is_string($options['extensions']) ? $options['extensions'] : '')) {
return false;
}
return $iterator->hasChildren() || $file->isFile() ? true : false;
Expand Down

0 comments on commit e31c0ad

Please sign in to comment.