Skip to content

Commit

Permalink
Change all instances of options to use CliOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Feb 2, 2022
1 parent 951636c commit 43bae36
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 204 deletions.
45 changes: 21 additions & 24 deletions PhpcsChanged/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PhpcsChanged\Cli;

use PhpcsChanged\NoChangesException;
use PhpcsChanged\CliOptions;
use PhpcsChanged\Reporter;
use PhpcsChanged\JsonReporter;
use PhpcsChanged\FullReporter;
Expand Down Expand Up @@ -193,11 +194,13 @@ function runManualWorkflow(string $diffFile, string $phpcsOldFile, string $phpcs
return $messages;
}

function runSvnWorkflow(array $svnFiles, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
function runSvnWorkflow(CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
$svn = getenv('SVN') ?: 'svn';
$phpcs = getenv('PHPCS') ?: 'phpcs';
$cat = getenv('CAT') ?: 'cat';

$svnFiles = $options->files;

try {
$debug('validating executables');
$shell->validateExecutableExists('svn', $svn);
Expand All @@ -221,12 +224,12 @@ function runSvnWorkflow(array $svnFiles, array $options, ShellOperator $shell, C
return PhpcsMessages::merge($phpcsMessages);
}

function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
function runSvnWorkflowForFile(string $svnFile, CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
$svn = getenv('SVN') ?: 'svn';
$phpcs = getenv('PHPCS') ?: 'phpcs';
$cat = getenv('CAT') ?: 'cat';

$phpcsStandard = $options['standard'] ?? null;
$phpcsStandard = $options->phpcsStandard;
$phpcsStandardOption = $phpcsStandard ? ' --standard=' . escapeshellarg($phpcsStandard) : '';

try {
Expand Down Expand Up @@ -285,19 +288,21 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s
);
}

function runGitWorkflow(array $gitFiles, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
function runGitWorkflow(CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
$git = getenv('GIT') ?: 'git';
$phpcs = getenv('PHPCS') ?: 'phpcs';
$cat = getenv('CAT') ?: 'cat';

$gitFiles = $options->files;

try {
$debug('validating executables');
$shell->validateExecutableExists('git', $git);
$shell->validateExecutableExists('phpcs', $phpcs);
$shell->validateExecutableExists('cat', $cat);
$debug('executables are valid');
if (isset($options['git-base']) && ! empty($options['git-base'])) {
$options['git-base'] = getGitMergeBase($git, [$shell, 'executeCommand'], $options, $debug);
if ($options->mode === 'git-base' && ! empty($options->gitBase)) {
$options->gitBase = getGitMergeBase($git, [$shell, 'executeCommand'], $options, $debug);
}
} catch(\Exception $err) {
$shell->printError($err->getMessage());
Expand All @@ -316,12 +321,12 @@ function runGitWorkflow(array $gitFiles, array $options, ShellOperator $shell, C
return PhpcsMessages::merge($phpcsMessages);
}

function runGitWorkflowForFile(string $gitFile, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
function runGitWorkflowForFile(string $gitFile, CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages {
$git = getenv('GIT') ?: 'git';
$phpcs = getenv('PHPCS') ?: 'phpcs';
$cat = getenv('CAT') ?: 'cat';

$phpcsStandard = $options['standard'] ?? null;
$phpcsStandard = $options->phpcsStandard;
$phpcsStandardOption = $phpcsStandard ? ' --standard=' . escapeshellarg($phpcsStandard) : '';

try {
Expand Down Expand Up @@ -382,15 +387,14 @@ function runGitWorkflowForFile(string $gitFile, array $options, ShellOperator $s
return getNewPhpcsMessages($unifiedDiff, PhpcsMessages::fromPhpcsJson($oldFilePhpcsOutput, $fileName), PhpcsMessages::fromPhpcsJson($newFilePhpcsOutput, $fileName));
}

function reportMessagesAndExit(PhpcsMessages $messages, string $reportType, array $options): void {
$reporter = getReporter($reportType);
function reportMessagesAndExit(PhpcsMessages $messages, CliOptions $options): void {
$reporter = getReporter($options->reporter);
echo $reporter->getFormattedMessages($messages, $options);
exit($reporter->getExitCode($messages));
}

function fileHasValidExtension(\SplFileInfo $file): 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
$AllowedExtensions = [
'php',
'inc',
Expand All @@ -410,7 +414,7 @@ function fileHasValidExtension(\SplFileInfo $file): bool {

$extensions = [];
array_shift($fileParts);
foreach ($fileParts as $part) {
foreach ($fileParts as $part) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$extensions[] = implode('.', $fileParts);
array_shift($fileParts);
}
Expand All @@ -420,7 +424,6 @@ function fileHasValidExtension(\SplFileInfo $file): bool {
}

return true;
// phpcs:enable
}

function shouldIgnorePath(string $path, string $patternOption = null): bool {
Expand Down Expand Up @@ -500,17 +503,11 @@ function shouldIgnorePath(string $path, string $patternOption = null): bool {
return false;
}

function isCachingEnabled(array $options): bool {
if (isset($options['no-cache'])) {
return false;
}
if (isset($options['cache'])) {
return true;
}
return false;
function isCachingEnabled(CliOptions $options): bool {
return $options->useCache;
}

function loadCache(CacheManager $cache, ShellOperator $shell, array $options): void {
function loadCache(CacheManager $cache, ShellOperator $shell, CliOptions $options): void {
if (isCachingEnabled($options)) {
try {
$cache->load();
Expand All @@ -525,7 +522,7 @@ function loadCache(CacheManager $cache, ShellOperator $shell, array $options): v
}
}

if (isset($options['clear-cache'])) {
if ($options->clearCache) {
$cache->clearCache();
try {
$cache->save();
Expand All @@ -537,7 +534,7 @@ function loadCache(CacheManager $cache, ShellOperator $shell, array $options): v
}
}

function saveCache(CacheManager $cache, ShellOperator $shell, array $options): void {
function saveCache(CacheManager $cache, ShellOperator $shell, CliOptions $options): void {
if (isCachingEnabled($options)) {
try {
$cache->save();
Expand Down
7 changes: 4 additions & 3 deletions PhpcsChanged/FullReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use PhpcsChanged\Reporter;
use PhpcsChanged\PhpcsMessages;
use PhpcsChanged\LintMessage;
use PhpcsChanged\CliOptions;
use function PhpcsChanged\Cli\getLongestString;

class FullReporter implements Reporter {
public function getFormattedMessages(PhpcsMessages $messages, array $options): string {
public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string {
$files = array_unique(array_map(function(LintMessage $message): string {
return $message->getFile() ?? 'STDIN';
}, $messages->getMessages()));
Expand All @@ -30,7 +31,7 @@ public function getFormattedMessages(PhpcsMessages $messages, array $options): s
}, $files)));
}

private function getFormattedMessagesForFile(array $messages, string $file, array $options): ?string {
private function getFormattedMessagesForFile(array $messages, string $file, ?CliOptions $options): ?string {
$lineCount = count($messages);
if ($lineCount < 1) {
return null;
Expand All @@ -52,7 +53,7 @@ private function getFormattedMessagesForFile(array $messages, string $file, arra

$formattedLines = implode("\n", array_map(function(LintMessage $message) use ($longestNumber, $options): string {
$source = $message->getSource() ?: 'Unknown';
$sourceString = isset($options['s']) ? " ({$source})" : '';
$sourceString = !empty($options->showMessageCodes) ? " ({$source})" : '';
return sprintf(" %{$longestNumber}d | %s | %s%s", $message->getLineNumber(), $message->getType(), $message->getMessage(), $sourceString);
}, $messages));

Expand Down
51 changes: 25 additions & 26 deletions PhpcsChanged/GitWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PhpcsChanged\NoChangesException;
use PhpcsChanged\ShellException;
use PhpcsChanged\CliOptions;

function validateGitFileExists(string $gitFile, string $git, callable $isReadable, callable $executeCommand, callable $debug, array $options): void {
if (isset($options['arc-lint'])) {
Expand All @@ -23,7 +24,7 @@ function validateGitFileExists(string $gitFile, string $git, callable $isReadabl
}
}

function isRunFromGitRoot( string $git, callable $executeCommand, callable $debug ): bool {
function isRunFromGitRoot(string $git, callable $executeCommand, callable $debug): bool {
static $isRunFromGitRoot;
if (null !== $isRunFromGitRoot) {
return $isRunFromGitRoot;
Expand All @@ -39,24 +40,24 @@ function isRunFromGitRoot( string $git, callable $executeCommand, callable $debu
return $isRunFromGitRoot;
}

function getGitMergeBase(string $git, callable $executeCommand, array $options, callable $debug): string {
if ( empty($options['git-base']) ) {
function getGitMergeBase(string $git, callable $executeCommand, CliOptions $options, callable $debug): string {
if ( empty($options->gitBase) ) {
return '';
}
$mergeBaseCommand = "{$git} merge-base " . escapeshellarg($options['git-base']) . ' HEAD';
$mergeBaseCommand = "{$git} merge-base " . escapeshellarg($options->gitBase) . ' HEAD';
$debug('running merge-base command:', $mergeBaseCommand);
$mergeBase = $executeCommand($mergeBaseCommand);
if (! $mergeBase) {
$debug('merge-base command produced no output');
return $options['git-base'];
return $options->gitBase;
}
$debug('merge-base command output:', $mergeBase);
return trim($mergeBase);
}

function getGitUnifiedDiff(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): string {
$objectOption = isset($options['git-base']) && ! empty($options['git-base']) ? ' ' . escapeshellarg($options['git-base']) . '...' : '';
$stagedOption = empty( $objectOption ) && ! isset($options['git-unstaged']) ? ' --staged' : '';
function getGitUnifiedDiff(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): string {
$objectOption = $options->mode === 'git-base' && ! empty($options->gitBase) ? ' ' . escapeshellarg($options->gitBase) . '...' : '';
$stagedOption = empty( $objectOption ) && $options->mode !== 'git-unstaged' ? ' --staged' : '';
$unifiedDiffCommand = "{$git} diff{$stagedOption}{$objectOption} --no-prefix " . escapeshellarg($gitFile);
$debug('running diff command:', $unifiedDiffCommand);
$unifiedDiff = $executeCommand($unifiedDiffCommand);
Expand All @@ -67,16 +68,16 @@ function getGitUnifiedDiff(string $gitFile, string $git, callable $executeComman
return $unifiedDiff;
}

function isNewGitFile(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool {
if ( isset($options['git-base']) && ! empty($options['git-base']) ) {
function isNewGitFile(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool {
if ( $options->mode === 'git-base' && ! empty($options->gitBase) ) {
return isNewGitFileRemote( $gitFile, $git, $executeCommand, $options, $debug );
} else {
return isNewGitFileLocal( $gitFile, $git, $executeCommand, $options, $debug );
}
}

function isNewGitFileRemote(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool {
$gitStatusCommand = "${git} cat-file -e " . escapeshellarg($options['git-base']) . ':' . escapeshellarg($gitFile) . ' 2>/dev/null';
function isNewGitFileRemote(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool {
$gitStatusCommand = "${git} cat-file -e " . escapeshellarg($options->gitBase) . ':' . escapeshellarg($gitFile) . ' 2>/dev/null';
$debug('checking status of file with command:', $gitStatusCommand);
$return_val = 1;
$gitStatusOutput = [];
Expand All @@ -86,7 +87,7 @@ function isNewGitFileRemote(string $gitFile, string $git, callable $executeComma
return 0 !== $return_val;
}

function isNewGitFileLocal(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool {
function isNewGitFileLocal(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool {
$gitStatusCommand = "${git} status --porcelain " . escapeshellarg($gitFile);
$debug('checking git status of file with command:', $gitStatusCommand);
$gitStatusOutput = $executeCommand($gitStatusCommand);
Expand All @@ -100,7 +101,7 @@ function isNewGitFileLocal(string $gitFile, string $git, callable $executeComman
return isset($gitStatusOutput[0]) && $gitStatusOutput[0] === 'A';
}

function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, string $phpcsStandardOption, callable $executeCommand, array $options, callable $debug): string {
function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, string $phpcsStandardOption, callable $executeCommand, CliOptions $options, callable $debug): string {
$oldFileContents = getOldGitRevisionContentsCommand($gitFile, $git, $options, $executeCommand, $debug);

$oldFilePhpcsOutputCommand = "{$oldFileContents} | {$phpcs} --report=json -q" . $phpcsStandardOption . ' --stdin-path=' . escapeshellarg($gitFile) . ' -';
Expand All @@ -113,7 +114,7 @@ function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, stri
return $oldFilePhpcsOutput;
}

function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, string $cat, string $phpcsStandardOption, callable $executeCommand, array $options, callable $debug): string {
function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, string $cat, string $phpcsStandardOption, callable $executeCommand, CliOptions $options, callable $debug): string {
$newFileContents = getNewGitRevisionContentsCommand($gitFile, $git, $cat, $options, $executeCommand, $debug);

$newFilePhpcsOutputCommand = "{$newFileContents} | {$phpcs} --report=json -q" . $phpcsStandardOption . ' --stdin-path=' . escapeshellarg($gitFile) .' -';
Expand All @@ -130,14 +131,14 @@ function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, strin
return $newFilePhpcsOutput;
}

function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $cat, array $options, callable $executeCommand, callable $debug): string {
if (isset($options['git-base']) && ! empty($options['git-base'])) {
function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $cat, CliOptions $options, callable $executeCommand, callable $debug): string {
if ($options->mode === 'git-base' && ! empty($options->gitBase)) {
// for git-base mode, we get the contents of the file from the HEAD version of the file in the current branch
if (isRunFromGitRoot($git, $executeCommand, $debug)) {
return "{$git} show HEAD:" . escapeshellarg($gitFile);
}
return "{$git} show HEAD:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ')';
} else if (isset($options['git-unstaged'])) {
} else if ($options->mode === 'git-unstaged') {
// for git-unstaged mode, we get the contents of the file from the current working copy
return "{$cat} " . escapeshellarg($gitFile);
}
Expand All @@ -148,11 +149,11 @@ function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $
return "{$git} show :0:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ')';
}

function getOldGitRevisionContentsCommand(string $gitFile, string $git, array $options, callable $executeCommand, callable $debug): string {
if (isset($options['git-base']) && ! empty($options['git-base'])) {
function getOldGitRevisionContentsCommand(string $gitFile, string $git, CliOptions $options, callable $executeCommand, callable $debug): string {
if ($options->mode === 'git-base' && ! empty($options->gitBase)) {
// git-base
$rev = escapeshellarg($options['git-base']);
} else if (isset($options['git-unstaged'])) {
$rev = escapeshellarg($options->gitBase);
} else if ($options->mode === 'git-unstaged') {
// git-unstaged
$rev = ':0'; // :0 in this case means "staged version or HEAD if there is no staged version"
} else {
Expand All @@ -165,9 +166,7 @@ function getOldGitRevisionContentsCommand(string $gitFile, string $git, array $o
return "${git} show {$rev}:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ")";
}



function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, array $options, callable $debug): string {
function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, CliOptions $options, callable $debug): string {
$fileContents = getNewGitRevisionContentsCommand($gitFile, $git, $cat, $options, $executeCommand, $debug);
$command = "{$fileContents} | {$git} hash-object --stdin";
$debug('running new file git hash command:', $command);
Expand All @@ -179,7 +178,7 @@ function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $
return $hash;
}

function getOldGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, array $options, callable $debug): string {
function getOldGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, CliOptions $options, callable $debug): string {
$fileContents = getOldGitRevisionContentsCommand($gitFile, $git, $options, $executeCommand, $debug);
$command = "{$fileContents} | {$git} hash-object --stdin";
$debug('running old file git hash command:', $command);
Expand Down
3 changes: 2 additions & 1 deletion PhpcsChanged/JsonReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use PhpcsChanged\PhpcsMessages;
use PhpcsChanged\PhpcsMessagesHelpers;
use PhpcsChanged\LintMessage;
use PhpcsChanged\CliOptions;

class JsonReporter implements Reporter {
public function getFormattedMessages(PhpcsMessages $messages, array $options): string { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$files = array_unique(array_map(function(LintMessage $message): string {
return $message->getFile() ?? 'STDIN';
}, $messages->getMessages()));
Expand Down
2 changes: 1 addition & 1 deletion PhpcsChanged/PhpcsMessagesHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function fromPhpcsJson(string $messages, string $forcedFileName =

public static function toPhpcsJson(PhpcsMessages $messages): string {
$reporter = new JsonReporter();
return $reporter->getFormattedMessages($messages, []);
return $reporter->getFormattedMessages($messages, null);
}

public static function fromArrays(array $messages, string $fileName = null): PhpcsMessages {
Expand Down
3 changes: 2 additions & 1 deletion PhpcsChanged/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
namespace PhpcsChanged;

use PhpcsChanged\PhpcsMessages;
use PhpcsChanged\CliOptions;

interface Reporter {
public function getFormattedMessages(PhpcsMessages $messages, array $options): string;
public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string;
public function getExitCode(PhpcsMessages $messages): int;
}
3 changes: 2 additions & 1 deletion PhpcsChanged/XmlReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use PhpcsChanged\LintMessage;
use PhpcsChanged\UnixShell;
use PhpcsChanged\ShellException;
use PhpcsChanged\CliOptions;

class XmlReporter implements Reporter {
public function getFormattedMessages(PhpcsMessages $messages, array $options): string { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$files = array_unique(array_map(function(LintMessage $message): string {
return $message->getFile() ?? 'STDIN';
}, $messages->getMessages()));
Expand Down
Loading

0 comments on commit 43bae36

Please sign in to comment.