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

RonasIT / laravel-entity-generator / 12026977149

26 Nov 2024 08:49AM UTC coverage: 39.655% (+0.1%) from 39.537%
12026977149

Pull #108

github

web-flow
Merge ddd257d48 into 8497bb4ef
Pull Request #108: feat: skip generation for invalid stubs

33 of 75 new or added lines in 15 files covered. (44.0%)

23 existing lines in 2 files now uncovered.

391 of 986 relevant lines covered (39.66%)

1.31 hits per line

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

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

3
namespace RonasIT\Support\Generators;
4

5
use Carbon\Carbon;
6
use Exception;
7
use RonasIT\Support\Events\SuccessCreateMessage;
8

9
class MigrationGenerator extends EntityGenerator
10
{
11
    public function generate(): void
12
    {
13
        $entities = $this->getTableName($this->model);
×
14

NEW
15
        if (!$this->checkStubExists('migration')) {
×
NEW
16
            return;
×
17
        }
18

19
        $content = $this->getStub('migration', [
×
20
            'class' => $this->getPluralName($this->model),
×
21
            'entity' => $this->model,
×
22
            'entities' => $entities,
×
23
            'relations' => $this->relations,
×
24
            'fields' => $this->fields,
×
25
            'table' => $this->generateTable($this->fields)
×
26
        ]);
×
27
        $now = Carbon::now()->format('Y_m_d_His');
×
28

29
        $this->saveClass('migrations', "{$now}_{$entities}_create_table", $content);
×
30

31
        event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
×
32
    }
33

34
    protected function isJson($typeName): bool
35
    {
36
        return $typeName == 'json';
×
37
    }
38

39
    protected function isRequired($typeName): bool
40
    {
41
        return !empty(explode('-', $typeName)[1]);
×
42
    }
43

44
    protected function isNullable($typeName): bool
45
    {
46
        return empty(explode('-', $typeName)[1]);
×
47
    }
48

49
    protected function getJsonLine($fieldName): string
50
    {
51
        if (env("DB_CONNECTION") == "mysql") {
×
52
            return "\$table->json('{$fieldName}')->nullable();";
×
53
        }
54

55
        return "\$table->jsonb('{$fieldName}')->default(\"{}\");";
×
56
    }
57

58
    protected function getRequiredLine($fieldName, $typeName): string
59
    {
60
        $type = explode('-', $typeName)[0];
×
61

62
        if ($type === 'timestamp' && env('DB_CONNECTION') === 'mysql') {
×
63
            return "\$table->{$type}('{$fieldName}')->nullable();";
×
64
        }
65

66
        return "\$table->{$type}('{$fieldName}');";
×
67
    }
68

69
    protected function getNonRequiredLine($fieldName, $typeName): string
70
    {
71
        $type = explode('-', $typeName)[0];
×
72

73
        return "\$table->{$type}('{$fieldName}')->nullable();";
×
74
    }
75

76
    protected function generateTable($fields): array
77
    {
78
        $resultTable = [];
×
79

80
        foreach ($fields as $typeName => $fieldNames) {
×
81
            foreach ($fieldNames as $fieldName) {
×
82
                array_push($resultTable, $this->getTableRow($fieldName, $typeName));
×
83
            }
84
        }
85

86
        return $resultTable;
×
87
    }
88

89
    protected function getTableRow($fieldName, $typeName): string
90
    {
91
        if ($this->isJson($typeName)) {
×
92
            return $this->getJsonLine($fieldName);
×
93
        }
94

95
        if ($this->isRequired($typeName)) {
×
96
            return $this->getRequiredLine($fieldName, $typeName);
×
97
        }
98

99
        if ($this->isNullable($typeName)) {
×
100
            return $this->getNonRequiredLine($fieldName, $typeName);
×
101
        }
102

103
        throw new Exception('Unknown fieldType in MigrationGenerator');
×
104
    }
105
}
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

© 2025 Coveralls, Inc