Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Jan 1, 2024
1 parent 1a1b5b8 commit d9808c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
56 changes: 29 additions & 27 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
use Cake\Database\Schema\CachedCollection;
use Cake\Database\Schema\TableSchema;
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Database\Type\EnumType;
use Cake\Database\TypeFactory;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use ReflectionEnum;
use function Cake\Core\pluginSplit;

/**
Expand Down Expand Up @@ -1466,7 +1469,10 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array

foreach ($schema->columns() as $column) {
$columnSchema = $schema->getColumn($column);
if (!in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true)) {
if (
!in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true)
&& !str_starts_with($columnSchema['type'], 'enum-')
) {
continue;
}

Expand All @@ -1476,39 +1482,33 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array

$enumsDefinitionString = trim(mb_substr($columnSchema['comment'], strpos($columnSchema['comment'], '[enum]') + 6));
$isInt = in_array($columnSchema['type'], ['integer', 'tinyinteger', 'smallinteger'], true);
if (str_starts_with($columnSchema['type'], 'enum-')) {
$dbType = TypeFactory::build($columnSchema['type']);
if ($dbType instanceof EnumType) {
$class = $dbType->getEnumClassName();

Check warning on line 1488 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1483-L1488

Added lines #L1483 - L1488 were not covered by tests
/** @var \BackedEnum $enum */
$rEnum = new ReflectionEnum($class);

Check failure on line 1490 in src/Command/ModelCommand.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Variable $enum in PHPDoc tag @var does not match assigned variable $rEnum.
$rBackingType = $rEnum->getBackingType();
$type = (string)$rBackingType;
if ($type === 'int') {
$isInt = true;

Check warning on line 1494 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1490-L1494

Added lines #L1490 - L1494 were not covered by tests
}
}
}
$enumsDefinition = EnumParser::parseCases($enumsDefinitionString, $isInt);
if (!$enumsDefinition) {
continue;

Check warning on line 1500 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1498-L1500

Added lines #L1498 - L1500 were not covered by tests
}

$enums[$column] = $enumsDefinition;
$enums[$column] = [
'type' => $isInt ? 'int' : 'string',
'cases' => $enumsDefinition,
];

Check warning on line 1506 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1503-L1506

Added lines #L1503 - L1506 were not covered by tests
}

return $enums;
}

/**
* @param string $enumsDefinitionString
* @return array<int|string, string>
*/
protected function parseEnumsDefinition(string $enumsDefinitionString): array
{
$enumCases = explode(',', $enumsDefinitionString);

$definition = [];
foreach ($enumCases as $enumCase) {
$key = $value = trim($enumCase);
if (str_contains($key, ':')) {
$value = trim(mb_substr($key, strpos($key, ':') + 1));
$key = mb_substr($key, 0, strpos($key, ':'));
}

$definition[$key] = mb_strtolower($value);
}

return $definition;
}

/**
* @param \Cake\ORM\Table $model
* @param array<string, mixed> $data
Expand All @@ -1525,22 +1525,24 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console

$entity = $this->_entityName($model->getAlias());

Check warning on line 1526 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1526

Added line #L1526 was not covered by tests

foreach ($enums as $column => $enum) {
foreach ($enums as $column => $data) {
$enumCommand = new EnumCommand();

Check warning on line 1529 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1528-L1529

Added lines #L1528 - L1529 were not covered by tests

$name = $entity . Inflector::camelize($column);
if ($this->plugin) {
$name = $this->plugin . '.' . $name;

Check warning on line 1533 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1531-L1533

Added lines #L1531 - L1533 were not covered by tests
}

$enumCases = $data['cases'];

Check warning on line 1536 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1536

Added line #L1536 was not covered by tests

$cases = [];
foreach ($enum as $k => $v) {
foreach ($enumCases as $k => $v) {
$cases[] = $k . ':' . $v;

Check warning on line 1540 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1538-L1540

Added lines #L1538 - L1540 were not covered by tests
}

$args = new Arguments(
[$name, implode(',', $cases)],
['int' => false] + $args->getOptions(),
['int' => $data['type'] === 'int'] + $args->getOptions(),
['name', 'cases']
);
$enumCommand->execute($args, $io);

Check warning on line 1548 in src/Command/ModelCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ModelCommand.php#L1543-L1548

Added lines #L1543 - L1548 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion templates/bake/Template/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
{% if isKey is not same as(true) %}
{% set columnData = Bake.columnData(field, schema) %}
{% if columnData.type starts with 'enum-' %}
<td><?= h(${{ singularVar }}->{{ field }}->label()) ?></td>
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->label()) ?></td>
{% elseif columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %}
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
{% elseif columnData.null %}
Expand Down

0 comments on commit d9808c1

Please sign in to comment.