Skip to content

Commit

Permalink
refactor: Move the PHP version option into a dedicated class (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Jun 16, 2024
1 parent 41eff3d commit 631bf7a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 39 deletions.
23 changes: 3 additions & 20 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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(),
],
);
}
Expand All @@ -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),
Expand All @@ -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();
Expand Down
22 changes: 3 additions & 19 deletions src/Console/Command/InspectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
],
);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
}
51 changes: 51 additions & 0 deletions src/Console/InputOption/PhpVersionInputOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* 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);
}
}
65 changes: 65 additions & 0 deletions tests/Console/InputOption/PhpVersionInputOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* 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);
}
}

0 comments on commit 631bf7a

Please sign in to comment.