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

drago-ex / generator / 12947105156

24 Jan 2025 09:42AM UTC coverage: 84.018% (-3.1%) from 87.111%
12947105156

push

github

web-flow
Update DataClassGenerator.phpt

184 of 219 relevant lines covered (84.02%)

0.84 hits per line

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

92.31
/src/Drago/Generator/Base.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Drago\Generator;
6

7
use Doctrine\Inflector\Inflector;
8
use Nette\Utils\Strings;
9
use Throwable;
10

11

12
/**
13
 * Base class for PHP code generation related to database entities and data classes.
14
 * Provides utility methods for generating file names, validating columns, detecting types,
15
 * and handling table references, used by the generators for creating entity and data class files.
16
 */
17
class Base
18
{
19
        public function __construct(
1✔
20
                public Repository $repository,
21
                public Options $options,
22
                public Inflector $inflector,
23
        ) {
24
        }
1✔
25

26

27
        /**
28
         * Create filename by adding the suffix.
29
         */
30
        public function filename(string $name, string $suffix): string
1✔
31
        {
32
                $filename = $this->inflector->classify(Strings::lower($name));
1✔
33
                return $filename . $suffix;
1✔
34
        }
35

36

37
        /**
38
         * Validate column name, check for parentheses.
39
         * @throws ValidateColumnException
40
         */
41
        public function validateColumn(string $table, string $column): void
1✔
42
        {
43
                if (str_contains($column, '(')) {
1✔
44
                        throw new ValidateColumnException("Invalid column name '$column' in table '$table'. Use 'AS' or change the name.");
1✔
45
                }
46
        }
1✔
47

48

49
        /**
50
         * Detect the column's native type.
51
         */
52
        public function detectType(string $type): string
1✔
53
        {
54
                static $patterns = [
55
                        'BYTEA|BLOB|BIN' => Type::Binary,
1✔
56
                        'TEXT|CHAR|STRING' => Type::Text,
1✔
57
                        'YEAR|INT|LONG' => Type::Integer,
1✔
58
                        'CURRENCY|MONEY' => Type::Float,
1✔
59
                        'DATE|TIME' => Type::Date,
1✔
60
                        'BOOL' => Type::Bool,
1✔
61
                ];
62

63
                foreach ($patterns as $pattern => $typeConstant) {
1✔
64
                        if (preg_match("#$pattern#i", $type)) {
1✔
65
                                return $typeConstant;
1✔
66
                        }
67
                }
68

69
                return Type::Text;
×
70
        }
71

72

73
        /**
74
         * Get foreign key references for the table.
75
         */
76
        public function getReferencesTable(string $table): array
1✔
77
        {
78
                $ref = [];
1✔
79
                try {
80
                        foreach ($this->repository->getForeignKeys($table) as $foreign) {
1✔
81
                                $ref[$foreign['local'][0]] = $foreign['table'];
×
82
                        }
83
                } catch (Throwable $e) {
1✔
84
                        // Silent fail: no need to report errors
85
                }
86
                return $ref;
1✔
87
        }
88
}
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