Skip to content

Commit 631bf7a

Browse files
authored
refactor: Move the PHP version option into a dedicated class (#1050)
1 parent 41eff3d commit 631bf7a

File tree

4 files changed

+122
-39
lines changed

4 files changed

+122
-39
lines changed

src/Console/Command/AddPrefixCommand.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
use Humbug\PhpScoper\Configuration\Throwable\UnknownConfigurationKey;
2828
use Humbug\PhpScoper\Console\ConfigLoader;
2929
use Humbug\PhpScoper\Console\ConsoleScoper;
30+
use Humbug\PhpScoper\Console\InputOption\PhpVersionInputOption;
3031
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
3132
use InvalidArgumentException;
32-
use PhpParser\PhpVersion;
3333
use Symfony\Component\Console\Exception\RuntimeException;
3434
use Symfony\Component\Console\Input\InputArgument;
3535
use Symfony\Component\Console\Input\InputOption;
@@ -57,7 +57,6 @@ final class AddPrefixCommand implements Command, CommandAware
5757
private const CONTINUE_ON_FAILURE_OPT = 'continue-on-failure';
5858
private const CONFIG_FILE_OPT = 'config';
5959
private const NO_CONFIG_OPT = 'no-config';
60-
private const PHP_VERSION_OPT = 'php-version';
6160

6261
private const DEFAULT_OUTPUT_DIR = 'build';
6362

@@ -133,12 +132,7 @@ public function getConfiguration(): CommandConfiguration
133132
InputOption::VALUE_NONE,
134133
'Do not look for a configuration file.',
135134
),
136-
new InputOption(
137-
self::PHP_VERSION_OPT,
138-
null,
139-
InputOption::VALUE_REQUIRED,
140-
'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".',
141-
),
135+
PhpVersionInputOption::createInputOption(),
142136
],
143137
);
144138
}
@@ -155,7 +149,7 @@ public function execute(IO $io): int
155149

156150
$paths = $this->getPathArguments($io, $cwd);
157151
$config = $this->retrieveConfig($io, $paths, $cwd);
158-
$phpVersion = self::getPhpVersion($io);
152+
$phpVersion = PhpVersionInputOption::getPhpVersion($io);
159153

160154
$outputDir = $this->canonicalizePath(
161155
$this->getOutputDir($io, $config),
@@ -175,17 +169,6 @@ public function execute(IO $io): int
175169
return ExitCode::SUCCESS;
176170
}
177171

178-
private static function getPhpVersion(IO $io): ?PhpVersion
179-
{
180-
$version = $io
181-
->getTypedOption(self::PHP_VERSION_OPT)
182-
->asNullableString();
183-
184-
return null === $version
185-
? $version
186-
: PhpVersion::fromString($version);
187-
}
188-
189172
private static function getStopOnFailure(IO $io): bool
190173
{
191174
$stopOnFailure = $io->getTypedOption(self::STOP_ON_FAILURE_OPT)->asBoolean();

src/Console/Command/InspectCommand.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Humbug\PhpScoper\Configuration\Configuration;
2424
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
2525
use Humbug\PhpScoper\Console\ConfigLoader;
26+
use Humbug\PhpScoper\Console\InputOption\PhpVersionInputOption;
2627
use Humbug\PhpScoper\Scoper\Factory\ScoperFactory;
2728
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
2829
use InvalidArgumentException;
@@ -50,7 +51,6 @@ final class InspectCommand implements Command, CommandAware
5051
private const PREFIX_OPT = 'prefix';
5152
private const CONFIG_FILE_OPT = 'config';
5253
private const NO_CONFIG_OPT = 'no-config';
53-
private const PHP_VERSION_OPT = 'php-version';
5454

5555
public function __construct(
5656
private readonly Filesystem $fileSystem,
@@ -96,12 +96,7 @@ public function getConfiguration(): CommandConfiguration
9696
InputOption::VALUE_NONE,
9797
'Do not look for a configuration file.',
9898
),
99-
new InputOption(
100-
self::PHP_VERSION_OPT,
101-
null,
102-
InputOption::VALUE_REQUIRED,
103-
'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".',
104-
),
99+
PhpVersionInputOption::createInputOption(),
105100
],
106101
);
107102
}
@@ -116,7 +111,7 @@ public function execute(IO $io): int
116111
// working directory
117112
$cwd = getcwd();
118113

119-
$phpversion = self::getPhpVersion($io);
114+
$phpversion = PhpVersionInputOption::getPhpVersion($io);
120115
$filePath = $this->getFilePath($io, $cwd);
121116
$config = $this->retrieveConfig($io, [$filePath], $cwd);
122117

@@ -266,15 +261,4 @@ private static function exportSymbolsRegistry(SymbolsRegistry $symbolsRegistry,
266261
true,
267262
);
268263
}
269-
270-
private static function getPhpVersion(IO $io): ?PhpVersion
271-
{
272-
$version = $io
273-
->getTypedOption(self::PHP_VERSION_OPT)
274-
->asNullableString();
275-
276-
return null === $version
277-
? $version
278-
: PhpVersion::fromString($version);
279-
}
280264
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Console\InputOption;
16+
17+
use Fidry\Console\IO;
18+
use Humbug\PhpScoper\NotInstantiable;
19+
use PhpParser\PhpVersion;
20+
use Symfony\Component\Console\Input\InputOption;
21+
22+
/**
23+
* @private
24+
*/
25+
final class PhpVersionInputOption
26+
{
27+
use NotInstantiable;
28+
29+
private const PHP_VERSION_OPT = 'php-version';
30+
31+
public static function createInputOption(): InputOption
32+
{
33+
return new InputOption(
34+
self::PHP_VERSION_OPT,
35+
null,
36+
InputOption::VALUE_REQUIRED,
37+
'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".',
38+
);
39+
}
40+
41+
public static function getPhpVersion(IO $io): ?PhpVersion
42+
{
43+
$version = $io
44+
->getTypedOption(self::PHP_VERSION_OPT)
45+
->asNullableString();
46+
47+
return null === $version
48+
? $version
49+
: PhpVersion::fromString($version);
50+
}
51+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Console\InputOption;
16+
17+
use Fidry\Console\IO;
18+
use PhpParser\PhpVersion;
19+
use PHPUnit\Framework\Attributes\CoversClass;
20+
use PHPUnit\Framework\Attributes\DataProvider;
21+
use PHPUnit\Framework\TestCase;
22+
use Symfony\Component\Console\Input\ArrayInput;
23+
use Symfony\Component\Console\Input\InputDefinition;
24+
25+
/**
26+
* @internal
27+
*/
28+
#[CoversClass(PhpVersionInputOption::class)]
29+
final class PhpVersionInputOptionTest extends TestCase
30+
{
31+
#[DataProvider('provideInput')]
32+
public function test_it_can_parse_the_php_version(IO $io, ?PhpVersion $expected): void
33+
{
34+
$actual = PhpVersionInputOption::getPhpVersion($io);
35+
36+
self::assertEquals($expected, $actual);
37+
}
38+
39+
public static function provideInput(): iterable
40+
{
41+
yield [
42+
self::createIO(null),
43+
null,
44+
];
45+
46+
yield [
47+
self::createIO('8.2'),
48+
PhpVersion::fromComponents(8, 2),
49+
];
50+
}
51+
52+
private static function createIO(?string $value): IO
53+
{
54+
$input = new ArrayInput(
55+
null === $value
56+
? []
57+
: [
58+
'--php-version' => $value,
59+
],
60+
);
61+
$input->bind(new InputDefinition([PhpVersionInputOption::createInputOption()]));
62+
63+
return IO::createNull()->withInput($input);
64+
}
65+
}

0 commit comments

Comments
 (0)