diff --git a/src/Console/Command/AddPrefixCommand.php b/src/Console/Command/AddPrefixCommand.php index e644d78f..65bb92c2 100644 --- a/src/Console/Command/AddPrefixCommand.php +++ b/src/Console/Command/AddPrefixCommand.php @@ -27,9 +27,9 @@ use Humbug\PhpScoper\Configuration\Throwable\UnknownConfigurationKey; use Humbug\PhpScoper\Console\ConfigLoader; use Humbug\PhpScoper\Console\ConsoleScoper; +use Humbug\PhpScoper\Console\InputOption\PhpVersionInputOption; use Humbug\PhpScoper\Scoper\Factory\ScoperFactory; use InvalidArgumentException; -use PhpParser\PhpVersion; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -57,7 +57,6 @@ final class AddPrefixCommand implements Command, CommandAware private const CONTINUE_ON_FAILURE_OPT = 'continue-on-failure'; private const CONFIG_FILE_OPT = 'config'; private const NO_CONFIG_OPT = 'no-config'; - private const PHP_VERSION_OPT = 'php-version'; private const DEFAULT_OUTPUT_DIR = 'build'; @@ -133,12 +132,7 @@ public function getConfiguration(): CommandConfiguration InputOption::VALUE_NONE, 'Do not look for a configuration file.', ), - new InputOption( - self::PHP_VERSION_OPT, - null, - InputOption::VALUE_REQUIRED, - 'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".', - ), + PhpVersionInputOption::createInputOption(), ], ); } @@ -155,7 +149,7 @@ public function execute(IO $io): int $paths = $this->getPathArguments($io, $cwd); $config = $this->retrieveConfig($io, $paths, $cwd); - $phpVersion = self::getPhpVersion($io); + $phpVersion = PhpVersionInputOption::getPhpVersion($io); $outputDir = $this->canonicalizePath( $this->getOutputDir($io, $config), @@ -175,17 +169,6 @@ public function execute(IO $io): int return ExitCode::SUCCESS; } - private static function getPhpVersion(IO $io): ?PhpVersion - { - $version = $io - ->getTypedOption(self::PHP_VERSION_OPT) - ->asNullableString(); - - return null === $version - ? $version - : PhpVersion::fromString($version); - } - private static function getStopOnFailure(IO $io): bool { $stopOnFailure = $io->getTypedOption(self::STOP_ON_FAILURE_OPT)->asBoolean(); diff --git a/src/Console/Command/InspectCommand.php b/src/Console/Command/InspectCommand.php index 0e401b50..0d2c8ef9 100644 --- a/src/Console/Command/InspectCommand.php +++ b/src/Console/Command/InspectCommand.php @@ -23,6 +23,7 @@ use Humbug\PhpScoper\Configuration\Configuration; use Humbug\PhpScoper\Configuration\ConfigurationFactory; use Humbug\PhpScoper\Console\ConfigLoader; +use Humbug\PhpScoper\Console\InputOption\PhpVersionInputOption; use Humbug\PhpScoper\Scoper\Factory\ScoperFactory; use Humbug\PhpScoper\Symbol\SymbolsRegistry; use InvalidArgumentException; @@ -50,7 +51,6 @@ final class InspectCommand implements Command, CommandAware private const PREFIX_OPT = 'prefix'; private const CONFIG_FILE_OPT = 'config'; private const NO_CONFIG_OPT = 'no-config'; - private const PHP_VERSION_OPT = 'php-version'; public function __construct( private readonly Filesystem $fileSystem, @@ -96,12 +96,7 @@ public function getConfiguration(): CommandConfiguration InputOption::VALUE_NONE, 'Do not look for a configuration file.', ), - new InputOption( - self::PHP_VERSION_OPT, - null, - InputOption::VALUE_REQUIRED, - 'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".', - ), + PhpVersionInputOption::createInputOption(), ], ); } @@ -116,7 +111,7 @@ public function execute(IO $io): int // working directory $cwd = getcwd(); - $phpversion = self::getPhpVersion($io); + $phpversion = PhpVersionInputOption::getPhpVersion($io); $filePath = $this->getFilePath($io, $cwd); $config = $this->retrieveConfig($io, [$filePath], $cwd); @@ -266,15 +261,4 @@ private static function exportSymbolsRegistry(SymbolsRegistry $symbolsRegistry, true, ); } - - private static function getPhpVersion(IO $io): ?PhpVersion - { - $version = $io - ->getTypedOption(self::PHP_VERSION_OPT) - ->asNullableString(); - - return null === $version - ? $version - : PhpVersion::fromString($version); - } } diff --git a/src/Console/InputOption/PhpVersionInputOption.php b/src/Console/InputOption/PhpVersionInputOption.php new file mode 100644 index 00000000..9e50389d --- /dev/null +++ b/src/Console/InputOption/PhpVersionInputOption.php @@ -0,0 +1,51 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\Console\InputOption; + +use Fidry\Console\IO; +use Humbug\PhpScoper\NotInstantiable; +use PhpParser\PhpVersion; +use Symfony\Component\Console\Input\InputOption; + +/** + * @private + */ +final class PhpVersionInputOption +{ + use NotInstantiable; + + private const PHP_VERSION_OPT = 'php-version'; + + public static function createInputOption(): InputOption + { + return new InputOption( + self::PHP_VERSION_OPT, + null, + InputOption::VALUE_REQUIRED, + 'PHP version in which the PHP parser and printer will be configured, e.g. "7.2".', + ); + } + + public static function getPhpVersion(IO $io): ?PhpVersion + { + $version = $io + ->getTypedOption(self::PHP_VERSION_OPT) + ->asNullableString(); + + return null === $version + ? $version + : PhpVersion::fromString($version); + } +} diff --git a/tests/Console/InputOption/PhpVersionInputOptionTest.php b/tests/Console/InputOption/PhpVersionInputOptionTest.php new file mode 100644 index 00000000..4f090d76 --- /dev/null +++ b/tests/Console/InputOption/PhpVersionInputOptionTest.php @@ -0,0 +1,65 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\Console\InputOption; + +use Fidry\Console\IO; +use PhpParser\PhpVersion; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputDefinition; + +/** + * @internal + */ +#[CoversClass(PhpVersionInputOption::class)] +final class PhpVersionInputOptionTest extends TestCase +{ + #[DataProvider('provideInput')] + public function test_it_can_parse_the_php_version(IO $io, ?PhpVersion $expected): void + { + $actual = PhpVersionInputOption::getPhpVersion($io); + + self::assertEquals($expected, $actual); + } + + public static function provideInput(): iterable + { + yield [ + self::createIO(null), + null, + ]; + + yield [ + self::createIO('8.2'), + PhpVersion::fromComponents(8, 2), + ]; + } + + private static function createIO(?string $value): IO + { + $input = new ArrayInput( + null === $value + ? [] + : [ + '--php-version' => $value, + ], + ); + $input->bind(new InputDefinition([PhpVersionInputOption::createInputOption()])); + + return IO::createNull()->withInput($input); + } +}