Skip to content

Commit a65295b

Browse files
authored
Add --version cli option (#172)
1 parent f61a643 commit a65295b

8 files changed

+61
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ This tool reads your `composer.json` and scans all paths listed in `autoload` &
7979
- `--composer-json path/to/composer.json` for custom path to composer.json
8080
- `--dump-usages symfony/console` to show usages of certain package(s), `*` placeholder is supported
8181
- `--config path/to/config.php` for custom path to config file
82+
- `--version` display version
8283
- `--help` display usage & cli options
8384
- `--verbose` to see more example classes & usages
8485
- `--show-all-usages` to see all usages

bin/composer-dependency-analyser

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<?php declare(strict_types=1);
33

44
use ShipMonk\ComposerDependencyAnalyser\Analyser;
5+
use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException;
56
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
67
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
78
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
@@ -52,6 +53,9 @@ try {
5253
) {
5354
$stdErrPrinter->printLine("\n<red>{$e->getMessage()}</red>" . PHP_EOL);
5455
exit(255);
56+
57+
} catch (AbortException $e) {
58+
exit(0);
5559
}
5660

5761
exit($exitCode);

src/Cli.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Cli
1313
{
1414

1515
private const OPTIONS = [
16+
'version' => false,
1617
'help' => false,
1718
'verbose' => false,
1819
'ignore-shadow-deps' => false,
@@ -126,6 +127,10 @@ public function getProvidedOptions(): CliOptions
126127
{
127128
$options = new CliOptions();
128129

130+
if (isset($this->providedOptions['version'])) {
131+
$options->version = true;
132+
}
133+
129134
if (isset($this->providedOptions['help'])) {
130135
$options->help = true;
131136
}

src/CliOptions.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
class CliOptions
66
{
77

8+
/**
9+
* @var true|null
10+
*/
11+
public $version = null;
12+
813
/**
914
* @var true|null
1015
*/

src/Exception/AbortException.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonk\ComposerDependencyAnalyser\Exception;
4+
5+
class AbortException extends RuntimeException
6+
{
7+
8+
public function __construct()
9+
{
10+
parent::__construct('');
11+
}
12+
13+
}

src/Initializer.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
namespace ShipMonk\ComposerDependencyAnalyser;
44

55
use Composer\Autoload\ClassLoader;
6+
use Composer\InstalledVersions;
7+
use OutOfBoundsException;
68
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
79
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
10+
use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException;
811
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
912
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
1013
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
1114
use ShipMonk\ComposerDependencyAnalyser\Result\ConsoleFormatter;
1215
use ShipMonk\ComposerDependencyAnalyser\Result\JunitFormatter;
1316
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
1417
use Throwable;
18+
use function class_exists;
1519
use function count;
1620
use function get_class;
1721
use function is_file;
@@ -28,6 +32,7 @@ class Initializer
2832
vendor/bin/composer-dependency-analyser
2933
3034
Options:
35+
--version Print analyser version.
3136
--help Print this help text and exit.
3237
--verbose Print more usage examples
3338
--show-all-usages Removes the limit of showing only few usages
@@ -220,6 +225,7 @@ public function initComposerClassLoaders(): array
220225

221226
/**
222227
* @param list<string> $argv
228+
* @throws AbortException
223229
* @throws InvalidCliException
224230
*/
225231
public function initCliOptions(string $cwd, array $argv): CliOptions
@@ -228,7 +234,12 @@ public function initCliOptions(string $cwd, array $argv): CliOptions
228234

229235
if ($cliOptions->help !== null) {
230236
$this->stdOutPrinter->printLine(self::$help);
231-
throw new InvalidCliException(''); // just exit
237+
throw new AbortException();
238+
}
239+
240+
if ($cliOptions->version !== null) {
241+
$this->stdOutPrinter->printLine($this->deduceVersion());
242+
throw new AbortException();
232243
}
233244

234245
return $cliOptions;
@@ -256,4 +267,19 @@ public function initFormatter(CliOptions $options): ResultFormatter
256267
throw new InvalidConfigException("Invalid format option provided, allowed are 'console' or 'junit'.");
257268
}
258269

270+
private function deduceVersion(): string
271+
{
272+
try {
273+
/** @throws OutOfBoundsException */
274+
$version = class_exists(InstalledVersions::class)
275+
? InstalledVersions::getPrettyVersion('shipmonk/composer-dependency-analyser')
276+
: 'unknown';
277+
278+
} catch (OutOfBoundsException $e) {
279+
$version = 'not found';
280+
}
281+
282+
return "Version: $version";
283+
}
284+
259285
}

tests/BinTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function test(): void
2424
$okOutput = 'No composer issues found';
2525
$dumpingOutput = 'Dumping sample usages of';
2626
$helpOutput = 'Usage:';
27+
$versionOutput = 'Version:';
2728

2829
$usingConfig = 'Using config';
2930

@@ -32,8 +33,9 @@ public function test(): void
3233
$this->runCommand('php bin/composer-dependency-analyser', $rootDir, 0, $okOutput, $usingConfig);
3334
$this->runCommand('php bin/composer-dependency-analyser --verbose', $rootDir, 0, $okOutput, $usingConfig);
3435
$this->runCommand('php ../bin/composer-dependency-analyser', $testsDir, 255, null, $noComposerJsonError);
35-
$this->runCommand('php bin/composer-dependency-analyser --help', $rootDir, 255, $helpOutput);
36-
$this->runCommand('php ../bin/composer-dependency-analyser --help', $testsDir, 255, $helpOutput);
36+
$this->runCommand('php bin/composer-dependency-analyser --help', $rootDir, 0, $helpOutput);
37+
$this->runCommand('php ../bin/composer-dependency-analyser --help', $testsDir, 0, $helpOutput);
38+
$this->runCommand('php ../bin/composer-dependency-analyser --version', $testsDir, 0, $versionOutput);
3739
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json', $rootDir, 0, $okOutput, $usingConfig);
3840
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.lock', $rootDir, 255, null, $noPackagesError);
3941
$this->runCommand('php bin/composer-dependency-analyser --composer-json=README.md', $rootDir, 255, null, $parseError);

tests/InitializerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
77
use ShipMonk\ComposerDependencyAnalyser\Config\PathToScan;
8-
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
8+
use ShipMonk\ComposerDependencyAnalyser\Exception\AbortException;
99
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
1010
use ShipMonk\ComposerDependencyAnalyser\Result\ConsoleFormatter;
1111
use ShipMonk\ComposerDependencyAnalyser\Result\JunitFormatter;
@@ -114,7 +114,7 @@ public function testInitCliOptionsHelp(): void
114114

115115
$initializer = new Initializer(__DIR__, $printer, $printer);
116116

117-
$this->expectException(InvalidCliException::class);
117+
$this->expectException(AbortException::class);
118118
$initializer->initCliOptions(__DIR__, ['script.php', '--help']);
119119
}
120120

0 commit comments

Comments
 (0)