-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make (in)compatible OS families non-empty-list or a nullable to express intent better #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Php\Pie\ExtensionType; | ||
use Php\Pie\Platform\OperatingSystemFamily; | ||
|
||
use Webmozart\Assert\Assert; | ||
use function array_key_exists; | ||
use function array_map; | ||
use function array_slice; | ||
|
@@ -31,9 +32,9 @@ | |
final class Package | ||
{ | ||
/** | ||
* @param list<ConfigureOption> $configureOptions | ||
* @param list<OperatingSystemFamily> $compatibleOsFamilies | ||
* @param list<OperatingSystemFamily> $incompatibleOsFamilies | ||
* @param list<ConfigureOption> $configureOptions | ||
* @param non-empty-list<OperatingSystemFamily>|null $compatibleOsFamilies | ||
* @param non-empty-list<OperatingSystemFamily>|null $incompatibleOsFamilies | ||
*/ | ||
public function __construct( | ||
public readonly CompletePackageInterface $composerPackage, | ||
|
@@ -46,8 +47,8 @@ public function __construct( | |
public readonly bool $supportZts, | ||
public readonly bool $supportNts, | ||
public readonly string|null $buildPath, | ||
public readonly array $compatibleOsFamilies, | ||
public readonly array $incompatibleOsFamilies, | ||
public readonly array|null $compatibleOsFamilies, | ||
public readonly array|null $incompatibleOsFamilies, | ||
) { | ||
} | ||
|
||
|
@@ -74,17 +75,10 @@ public static function fromComposerCompletePackage(CompletePackageInterface $com | |
? $phpExtOptions['build-path'] | ||
: null; | ||
|
||
/** @var list<string> $compatibleOsFamilies */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are not necessary; we need upstream Composer support first |
||
$compatibleOsFamilies = $phpExtOptions !== null && array_key_exists('os-families', $phpExtOptions) | ||
? $phpExtOptions['os-families'] | ||
: []; | ||
|
||
/** @var list<string> $incompatibleOsFamilies */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are not necessary; we need upstream Composer support first |
||
$incompatibleOsFamilies = $phpExtOptions !== null && array_key_exists('os-families-exclude', $phpExtOptions) | ||
? $phpExtOptions['os-families-exclude'] | ||
: []; | ||
$compatibleOsFamilies = $phpExtOptions['os-families'] ?? null; | ||
$incompatibleOsFamilies = $phpExtOptions['os-families-exclude'] ?? null; | ||
|
||
if ($compatibleOsFamilies && $incompatibleOsFamilies) { | ||
if ($compatibleOsFamilies !== null && $incompatibleOsFamilies !== null) { | ||
throw new InvalidArgumentException('Cannot specify both "os-families" and "os-families-exclude" in composer.json'); | ||
} | ||
|
||
|
@@ -129,25 +123,28 @@ public function githubOrgAndRepository(): string | |
} | ||
|
||
/** | ||
* @param list<string> $input | ||
* @param list<string>|null $input | ||
* | ||
* @return list<OperatingSystemFamily> | ||
* @return non-empty-list<OperatingSystemFamily>|null | ||
*/ | ||
private static function convertInputStringsToOperatingSystemFamilies(array $input): array | ||
private static function convertInputStringsToOperatingSystemFamilies(array|null $input): array|null | ||
{ | ||
if ($input === null) { | ||
return null; | ||
} | ||
|
||
$osFamilies = []; | ||
foreach ($input as $value) { | ||
// try to normalize a bit the input | ||
$valueToTry = ucfirst(strtolower($value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One improvement I didn't do, but I think it could make sense to always lowercase (including the |
||
|
||
$family = OperatingSystemFamily::tryFrom($valueToTry); | ||
if ($family === null) { | ||
throw new InvalidArgumentException(sprintf('Expected operating system family to be one of "%s", got "%s".', implode('", "', OperatingSystemFamily::asValuesList()), $value)); | ||
} | ||
Assert::inArray($valueToTry, OperatingSystemFamily::asValuesList(), 'Expected operating system family to be one of: %2$s. Got: %s'); | ||
|
||
$osFamilies[] = $family; | ||
$osFamilies[] = OperatingSystemFamily::from($valueToTry); | ||
} | ||
|
||
Assert::isNonEmptyList($osFamilies, 'Expected operating systems families to be a non-empty list.'); | ||
|
||
return $osFamilies; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,8 +53,8 @@ public function setUp(): void | |
true, | ||
true, | ||
null, | ||
[], | ||
[], | ||
null, | ||
null, | ||
); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key change here is that the OS families can either be
null
(explicitly not specified inphp-ext
), or a non-empty-list; I think this helps express intent better, otherwise, maybe someone could try:which does not make sense