Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,27 +430,14 @@ private function printAvailableTypes(ConsoleStyle $io): void
{
$allTypes = $this->getTypesMap();

if ('Hyper' === getenv('TERM_PROGRAM')) {
$wizard = 'wizard 🧙';
} else {
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
}

$typesTable = [
'main' => [
'string' => [],
'string' => ['ascii_string'],
'text' => [],
'boolean' => [],
'integer' => ['smallint', 'bigint'],
'float' => [],
],
'relation' => [
'relation' => 'a '.$wizard.' will help you build the relation',
EntityRelation::MANY_TO_ONE => [],
EntityRelation::ONE_TO_MANY => [],
EntityRelation::MANY_TO_MANY => [],
EntityRelation::ONE_TO_ONE => [],
],
'array_object' => [
'array' => ['simple_array'],
'json' => [],
Expand All @@ -469,19 +456,30 @@ private function printAvailableTypes(ConsoleStyle $io): void

$printSection = static function (array $sectionTypes) use ($io, &$allTypes) {
foreach ($sectionTypes as $mainType => $subTypes) {
if (!\array_key_exists($mainType, $allTypes)) {
// The type is not a valid DBAL Type - don't show it as an option
continue;
}

foreach ($subTypes as $key => $potentialType) {
if (!\array_key_exists($potentialType, $allTypes)) {
// The type is not a valid DBAL Type - don't show it as an "or" option
unset($subTypes[$key]);
}

// Remove type as not to show it again in "Other Types"
unset($allTypes[$potentialType]);
}

// Remove type as not to show it again in "Other Types"
unset($allTypes[$mainType]);

$line = sprintf(' * <comment>%s</comment>', $mainType);

if (\is_string($subTypes) && $subTypes) {
$line .= sprintf(' or %s', $subTypes);
} elseif (\is_array($subTypes) && !empty($subTypes)) {
if (!empty($subTypes)) {
$line .= sprintf(' or %s', implode(' or ', array_map(
static fn ($subType) => sprintf('<comment>%s</comment>', $subType), $subTypes))
);

foreach ($subTypes as $subType) {
unset($allTypes[$subType]);
}
}

$io->writeln($line);
Expand All @@ -490,11 +488,30 @@ private function printAvailableTypes(ConsoleStyle $io): void
$io->writeln('');
};

$printRelationsSection = static function () use ($io) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broke out handling relations to reduce the complexity above. otherwise we're nesting if's 3+ layers deep.

if ('Hyper' === getenv('TERM_PROGRAM')) {
$wizard = 'wizard 🧙';
} else {
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
}

$io->writeln(sprintf(' * <comment>relation</comment> a %s will help you build the relation', $wizard));

$relations = [EntityRelation::MANY_TO_ONE, EntityRelation::ONE_TO_MANY, EntityRelation::MANY_TO_MANY, EntityRelation::ONE_TO_ONE];
foreach ($relations as $relation) {
$line = sprintf(' * <comment>%s</comment>', $relation);

$io->writeln($line);
}

$io->writeln('');
};

$io->writeln('<info>Main Types</info>');
$printSection($typesTable['main']);

$io->writeln('<info>Relationships/Associations</info>');
$printSection($typesTable['relation']);
$printRelationsSection();

$io->writeln('<info>Array/Object Types</info>');
$printSection($typesTable['array_object']);
Expand Down
34 changes: 34 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,40 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_only_shows_supported_types' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
// entity class name
'Developer',
// property name
'keyboards',
// field type
'?',
// use default type
'',
// default length
'',
// nullable
'',
// no more properties
'',
]);

self::assertStringContainsString('Main Types', $output);
self::assertStringContainsString('* string or ascii_string', $output);
self::assertStringContainsString('* ManyToOne', $output);

// get the dependencies installed in the test project (tmp/cache/TEST)
$installedVersions = require $runner->getPath('vendor/composer/installed.php');

if (!str_starts_with($installedVersions['versions']['doctrine/dbal']['version'], '3.')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hack way to determine if dbal 3 or 4 is installed. MakerTestRunner::doesClassExist() doesnt provide all available classes for a cached project under test. To be dealt with under a different PR.

self::assertStringNotContainsString('* object', $output);
} else {
self::assertStringContainsString('* object', $output);
}
}),
];

yield 'it_creates_a_new_class_and_api_resource' => [$this->createMakeEntityTest()
->addExtraDependencies('api')
->run(function (MakerTestRunner $runner) {
Expand Down