Skip to content

Commit 415332d

Browse files
authored
bug #1448 [make:entity] only show supported types in cli wizard (#1448)
1 parent d848873 commit 415332d

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

src/Maker/MakeEntity.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -430,27 +430,14 @@ private function printAvailableTypes(ConsoleStyle $io): void
430430
{
431431
$allTypes = $this->getTypesMap();
432432

433-
if ('Hyper' === getenv('TERM_PROGRAM')) {
434-
$wizard = 'wizard 🧙';
435-
} else {
436-
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
437-
}
438-
439433
$typesTable = [
440434
'main' => [
441-
'string' => [],
435+
'string' => ['ascii_string'],
442436
'text' => [],
443437
'boolean' => [],
444438
'integer' => ['smallint', 'bigint'],
445439
'float' => [],
446440
],
447-
'relation' => [
448-
'relation' => 'a '.$wizard.' will help you build the relation',
449-
EntityRelation::MANY_TO_ONE => [],
450-
EntityRelation::ONE_TO_MANY => [],
451-
EntityRelation::MANY_TO_MANY => [],
452-
EntityRelation::ONE_TO_ONE => [],
453-
],
454441
'array_object' => [
455442
'array' => ['simple_array'],
456443
'json' => [],
@@ -469,19 +456,30 @@ private function printAvailableTypes(ConsoleStyle $io): void
469456

470457
$printSection = static function (array $sectionTypes) use ($io, &$allTypes) {
471458
foreach ($sectionTypes as $mainType => $subTypes) {
459+
if (!\array_key_exists($mainType, $allTypes)) {
460+
// The type is not a valid DBAL Type - don't show it as an option
461+
continue;
462+
}
463+
464+
foreach ($subTypes as $key => $potentialType) {
465+
if (!\array_key_exists($potentialType, $allTypes)) {
466+
// The type is not a valid DBAL Type - don't show it as an "or" option
467+
unset($subTypes[$key]);
468+
}
469+
470+
// Remove type as not to show it again in "Other Types"
471+
unset($allTypes[$potentialType]);
472+
}
473+
474+
// Remove type as not to show it again in "Other Types"
472475
unset($allTypes[$mainType]);
476+
473477
$line = sprintf(' * <comment>%s</comment>', $mainType);
474478

475-
if (\is_string($subTypes) && $subTypes) {
476-
$line .= sprintf(' or %s', $subTypes);
477-
} elseif (\is_array($subTypes) && !empty($subTypes)) {
479+
if (!empty($subTypes)) {
478480
$line .= sprintf(' or %s', implode(' or ', array_map(
479481
static fn ($subType) => sprintf('<comment>%s</comment>', $subType), $subTypes))
480482
);
481-
482-
foreach ($subTypes as $subType) {
483-
unset($allTypes[$subType]);
484-
}
485483
}
486484

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

491+
$printRelationsSection = static function () use ($io) {
492+
if ('Hyper' === getenv('TERM_PROGRAM')) {
493+
$wizard = 'wizard 🧙';
494+
} else {
495+
$wizard = '\\' === \DIRECTORY_SEPARATOR ? 'wizard' : 'wizard 🧙';
496+
}
497+
498+
$io->writeln(sprintf(' * <comment>relation</comment> a %s will help you build the relation', $wizard));
499+
500+
$relations = [EntityRelation::MANY_TO_ONE, EntityRelation::ONE_TO_MANY, EntityRelation::MANY_TO_MANY, EntityRelation::ONE_TO_ONE];
501+
foreach ($relations as $relation) {
502+
$line = sprintf(' * <comment>%s</comment>', $relation);
503+
504+
$io->writeln($line);
505+
}
506+
507+
$io->writeln('');
508+
};
509+
493510
$io->writeln('<info>Main Types</info>');
494511
$printSection($typesTable['main']);
495512

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

499516
$io->writeln('<info>Array/Object Types</info>');
500517
$printSection($typesTable['array_object']);

tests/Maker/MakeEntityTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,40 @@ public function getTestDetails(): \Generator
7474
}),
7575
];
7676

77+
yield 'it_only_shows_supported_types' => [$this->createMakeEntityTest()
78+
->run(function (MakerTestRunner $runner) {
79+
$output = $runner->runMaker([
80+
// entity class name
81+
'Developer',
82+
// property name
83+
'keyboards',
84+
// field type
85+
'?',
86+
// use default type
87+
'',
88+
// default length
89+
'',
90+
// nullable
91+
'',
92+
// no more properties
93+
'',
94+
]);
95+
96+
self::assertStringContainsString('Main Types', $output);
97+
self::assertStringContainsString('* string or ascii_string', $output);
98+
self::assertStringContainsString('* ManyToOne', $output);
99+
100+
// get the dependencies installed in the test project (tmp/cache/TEST)
101+
$installedVersions = require $runner->getPath('vendor/composer/installed.php');
102+
103+
if (!str_starts_with($installedVersions['versions']['doctrine/dbal']['version'], '3.')) {
104+
self::assertStringNotContainsString('* object', $output);
105+
} else {
106+
self::assertStringContainsString('* object', $output);
107+
}
108+
}),
109+
];
110+
77111
yield 'it_creates_a_new_class_and_api_resource' => [$this->createMakeEntityTest()
78112
->addExtraDependencies('api')
79113
->run(function (MakerTestRunner $runner) {

0 commit comments

Comments
 (0)