Skip to content

Commit

Permalink
Make SymbolType an enum (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 12, 2022
1 parent 98184cd commit 7d1664e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
46 changes: 22 additions & 24 deletions src/Console/Command/InspectSymbolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public function getConfiguration(): CommandConfiguration
InputArgument::OPTIONAL,
sprintf(
'The symbol type inspect ("%s").',
implode('", "', SymbolType::ALL),
implode('", "', SymbolType::values()),
),
SymbolType::ANY_TYPE,
SymbolType::ANY_TYPE->value,
),
],
[
Expand Down Expand Up @@ -109,8 +109,7 @@ public function execute(IO $io): int
$cwd = getcwd();

$symbol = $io->getArgument(self::SYMBOL_ARG)->asString();
/** @var SymbolType::*_TYPE $symbolType */
$symbolType = $io->getArgument(self::SYMBOL_TYPE_ARG)->asStringChoice(SymbolType::ALL);
$symbolType = self::getSymbolType($io);
$config = $this->retrieveConfig($io, $cwd);

$enrichedReflector = $this->enrichedReflectorFactory->create(
Expand All @@ -128,6 +127,14 @@ public function execute(IO $io): int
return ExitCode::SUCCESS;
}

// TODO: https://github.com/theofidry/console/issues/99
private static function getSymbolType(IO $io): SymbolType
{
$symbolType = $io->getArgument(self::SYMBOL_TYPE_ARG)->asString();

return SymbolType::from($symbolType);
}

private function retrieveConfig(IO $io, string $cwd): Configuration
{
$configLoader = new ConfigLoader(
Expand Down Expand Up @@ -179,13 +186,10 @@ private function getConfigFilePath(IO $io, string $cwd): ?string
return file_exists($configPath) ? $configPath : null;
}

/**
* @param SymbolType::*_TYPE $type
*/
private static function printSymbol(
IO $io,
string $symbol,
string $type,
SymbolType $type,
?string $configPath,
EnrichedReflector $reflector
): void {
Expand Down Expand Up @@ -230,21 +234,18 @@ private static function printConfigLoaded(IO $io, ?string $configPath): void
$io->newLine();
}

/**
* @param SymbolType::*_TYPE $type
*/
private static function printInspectionHeadline(
IO $io,
string $symbol,
string $type
SymbolType $type
): void {
$io->writeln(
sprintf(
'Inspecting the symbol <comment>%s</comment> %s',
$symbol,
SymbolType::ANY_TYPE === $type
? 'for all types.'
: sprintf('for type <comment>%s</comment>:', $type),
: sprintf('for type <comment>%s</comment>:', $type->value),
),
);
}
Expand All @@ -258,21 +259,18 @@ private static function printAnyTypeSymbol(
$io->writeln(
sprintf(
'As a <comment>%s</comment>:',
$specificType,
$specificType->value,
),
);

self::printTypedSymbol($io, $symbol, $specificType, $reflector);
}
}

/**
* @param SymbolType::*_TYPE $type
*/
private static function printTypedSymbol(
IO $io,
string $symbol,
string $type,
SymbolType $type,
EnrichedReflector $reflector
): void {
[$internal, $exposed] = self::determineSymbolStatus(
Expand All @@ -293,11 +291,11 @@ private static function printTypedSymbol(
]);
}

/**
* @param SymbolType::*_TYPE $type
*/
private static function determineSymbolStatus(string $symbol, string $type, EnrichedReflector $reflector): array
{
private static function determineSymbolStatus(
string $symbol,
SymbolType $type,
EnrichedReflector $reflector
): array {
return match ($type) {
SymbolType::CLASS_TYPE => [
$reflector->isClassInternal($symbol),
Expand All @@ -314,7 +312,7 @@ private static function determineSymbolStatus(string $symbol, string $type, Enri
default => throw new InvalidArgumentException(
sprintf(
'Invalid type "%s"',
$type,
$type->value,
),
),
};
Expand Down
26 changes: 19 additions & 7 deletions src/Console/Command/SymbolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

namespace Humbug\PhpScoper\Console\Command;

// TODO: make this an enum in PHP 8.1
final class SymbolType
use function array_column;

enum SymbolType: string
{
public const CLASS_TYPE = 'class';
public const FUNCTION_TYPE = 'function';
public const CONSTANT_TYPE = 'constant';
public const ANY_TYPE = 'any';
case CLASS_TYPE = 'class';
case FUNCTION_TYPE = 'function';
case CONSTANT_TYPE = 'constant';
case ANY_TYPE = 'any';

public const ALL = [
self::CLASS_TYPE,
Expand All @@ -30,7 +31,7 @@ final class SymbolType
];

/**
* @return list<self::*_TYPE>
* @return list<self>
*/
public static function getAllSpecificTypes(): array
{
Expand All @@ -40,4 +41,15 @@ public static function getAllSpecificTypes(): array
self::CONSTANT_TYPE,
];
}

/**
* @return list<string>
*/
public static function values(): array
{
return array_column(
self::cases(),
'value',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function test_inspects_the_given_symbol(): void
$input = [
'inspect-symbol',
'symbol' => 'Acme\Foo',
'type' => SymbolType::CLASS_TYPE,
'type' => SymbolType::CLASS_TYPE->value,
'--no-interaction' => null,
'--no-config' => null,
];
Expand Down

0 comments on commit 7d1664e

Please sign in to comment.