-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move the PHP version option into a dedicated class (#1050)
- Loading branch information
Showing
4 changed files
with
122 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |