• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-entity-generator / 21166733718

20 Jan 2026 09:43AM UTC coverage: 99.432% (-0.6%) from 100.0%
21166733718

Pull #241

github

web-flow
Merge 2b61df918 into 962b971b7
Pull Request #241: [239]: append field modifiers instead of uppercase options

165 of 167 new or added lines in 17 files covered. (98.8%)

4 existing lines in 2 files now uncovered.

1051 of 1057 relevant lines covered (99.43%)

11.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.14
/src/Generators/MigrationGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Carbon\Carbon;
6
use RonasIT\Support\Collections\FieldsCollection;
7
use RonasIT\Support\Enums\FieldModifierEnum;
8
use RonasIT\Support\Enums\FieldTypeEnum;
9
use RonasIT\Support\Events\SuccessCreateMessage;
10
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
11
use RonasIT\Support\ValueObjects\Field;
12

13
class MigrationGenerator extends EntityGenerator
14
{
15
    public function generate(): void
16
    {
17
        if (!$this->isStubExists('migration')) {
6✔
18
            return;
1✔
19
        }
20

21
        $entities = $this->getTableName($this->model);
5✔
22

23
        $content = $this->getStub('migration', [
5✔
24
            'class' => $this->getPluralName($this->model),
5✔
25
            'entity' => $this->model,
5✔
26
            'entities' => $entities,
5✔
27
            'relations' => $this->prepareRelations(),
5✔
28
            'fields' => $this->fields,
5✔
29
            'table' => $this->generateTable($this->fields),
5✔
30
        ]);
5✔
31

32
        $now = Carbon::now()->format('Y_m_d_His');
5✔
33

34
        $this->saveClass('migrations', "{$now}_{$entities}_create_table", $content);
5✔
35

36
        event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
5✔
37
    }
38

39
    protected function isJson(FieldTypeEnum $type): bool
40
    {
41
        return $type === FieldTypeEnum::Json;
2✔
42
    }
43

44
    protected function isRequired(array $modifiers): bool
45
    {
46
        return in_array(FieldModifierEnum::Required, $modifiers);
2✔
47
    }
48

49
    protected function isNullable(array $modifiers): bool
50
    {
51
        return empty($modifiers);
2✔
52
    }
53

54
    protected function getJsonLine(string $fieldName): string
55
    {
56
        if (env('DB_CONNECTION') == 'mysql') {
2✔
57
            return "\$table->json('{$fieldName}')->nullable();";
1✔
58
        }
59

60
        return "\$table->jsonb('{$fieldName}')->default(\"{}\");";
1✔
61
    }
62

63
    protected function getRequiredLine(string $fieldName, string $typeName): string
64
    {
65
        if ($typeName === 'timestamp' && env('DB_CONNECTION') === 'mysql') {
2✔
66
            return "\$table->{$typeName}('{$fieldName}')->nullable();";
1✔
67
        }
68

69
        return "\$table->{$typeName}('{$fieldName}');";
2✔
70
    }
71

72
    protected function getNonRequiredLine(string $fieldName, string $typeName): string
73
    {
74
        return "\$table->{$typeName}('{$fieldName}')->nullable();";
2✔
75
    }
76

77
    protected function generateTable(FieldsCollection $fields): array
78
    {
79
        $resultTable = [];
5✔
80

81
        foreach ($fields as $field) {
5✔
82
            $resultTable[] = $this->getTableRow($field->type, $field);
2✔
83
        }
84

85
        return $resultTable;
5✔
86
    }
87

88
    protected function getTableRow(FieldTypeEnum $fieldType, Field $field): string
89
    {
90
        if ($this->isJson($fieldType)) {
2✔
91
            return $this->getJsonLine($field->name);
2✔
92
        }
93

94
        if ($this->isRequired($field->modifiers)) {
2✔
95
            return $this->getRequiredLine($field->name, $fieldType->value);
2✔
96
        }
97

98
        if ($this->isNullable($field->modifiers)) {
2✔
99
            return $this->getNonRequiredLine($field->name, $fieldType->value);
2✔
100
        }
101

NEW
102
        throw new UnknownFieldTypeException($fieldType->value, 'MigrationGenerator');
×
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc