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

tito10047 / type-safe-id-bundle / 23162475169

16 Mar 2026 07:39PM UTC coverage: 82.443% (+12.5%) from 69.912%
23162475169

push

github

tito10047
add TypeIdGenerator for custom ID generation and related integration tests

19 of 20 new or added lines in 4 files covered. (95.0%)

8 existing lines in 1 file now uncovered.

108 of 131 relevant lines covered (82.44%)

25.79 hits per line

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

85.0
/src/IdEntityGenerator.php
1
<?php
2

3
namespace Tito10047\TypeSafeIdBundle;
4

5
use Doctrine\ORM\QueryBuilder;
6
use Symfony\Bridge\Doctrine\Types\AbstractUidType;
7
use Symfony\Bridge\Doctrine\Types\UlidType;
8
use Symfony\Bridge\Doctrine\Types\UuidType;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Bundle\MakerBundle\Generator;
11
use Symfony\Bundle\MakerBundle\Maker\Common\EntityIdTypeEnum;
12
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
13
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
14
use Symfony\Component\Uid\Ulid;
15
use Symfony\Component\Uid\Uuid;
16
use Symfony\Component\Uid\UuidV7;
17
use Tito10047\TypeSafeIdBundle\AbstractIntIdType;
18

19
class IdEntityGenerator extends Generator {
20

21

22
        /**
23
         * @noinspection PhpMissingParentConstructorInspection
24
         */
25
        private ?string $classNameId = null;
26
        private ?EntityIdTypeEnum   $idType = null;
27

28
        public function __construct(
29
                private readonly Generator $generator,
30
        ) {
31
        }
36✔
32

33
        public static function getControllerBaseClass(): ClassNameDetails {
UNCOV
34
                return new ClassNameDetails(AbstractController::class, '\\');
×
35
        }
36

37
        /**
38
         * @param array<string,mixed> $variables
39
         * @throws \Exception
40
         */
41
        public function generateClass(string $className, string $templateName, array $variables = []): string {
42
                /** @var UseStatementGenerator $useGenerator */
43
                $useGenerator = $variables['use_statements']??null;
36✔
44
                $this->idType ??= $variables['id_type'];
36✔
45
                if ($templateName === 'doctrine/Entity.tpl.php') {
36✔
46
                        $templateName = __DIR__ . '/../templates/extension/maker/Entity.tpl.php';
36✔
47

48
                        $this->classNameId = str_replace('\Entity\\', '\EntityId\\', $className)."Id";
36✔
49
                        $classNameIdType = $this->classNameId."Type";
36✔
50

51
                        // Generate the service ID for the custom ID generator
52
                        $generatorServiceId = 'doctrine.id_generator.' . str_replace('\\', '_', $this->classNameId);
36✔
53
                        $variables['id_generator_service'] = $generatorServiceId;
36✔
54
                        $useStatements = new  UseStatementGenerator([]);
36✔
55
                        if (($variables['id_type']??null) === EntityIdTypeEnum::UUID) {
36✔
56
                                $useStatements->addUseStatement(UuidV7::class);
12✔
57
                        } elseif (($variables['id_type']??null) === EntityIdTypeEnum::ULID) {
24✔
58
                                $useStatements->addUseStatement(Ulid::class);
12✔
59
                        }
60
                        $idVariables = $variables;
36✔
61
                        $idVariables['id_type'] ??= null;
36✔
62
                        $idVariables["use_statements"] = $useStatements;
36✔
63
                        $this->generator->generateClass(
36✔
64
                                $this->classNameId,
36✔
65
                                 __DIR__ . '/../templates/extension/maker/TypeId.tpl.php',
36✔
66
                                $idVariables
36✔
67
                        );
36✔
68

69
                        $typeVariables = $variables;
36✔
70
                        $typeVariables['id_type'] ??= null;
36✔
71
                        $useStatements = new  UseStatementGenerator([]);
36✔
72
                        $useStatements->addUseStatement($this->classNameId);
36✔
73
                        if (($variables['id_type']??null) === EntityIdTypeEnum::UUID || ($variables['id_type']??null) === EntityIdTypeEnum::ULID) {
36✔
74
                                $useStatements->addUseStatement(AbstractUidType::class);
24✔
75
                        } else {
76
                                $useStatements->addUseStatement(AbstractIntIdType::class);
12✔
77
                        }
78
                        $typeVariables["use_statements"] = $useStatements;
36✔
79
                        $parts = explode('\\', $this->classNameId);
36✔
80
                        $typeVariables["id_class"] = array_pop($parts);;
36✔
81
                        $this->generator->generateClass(
36✔
82
                                $classNameIdType,
36✔
83
                                __DIR__ . '/../templates/extension/maker/TypeIdType.tpl.php',
36✔
84
                                $typeVariables
36✔
85
                        );
36✔
86

87

88
                        $variables["use_statements"]->addUseStatement($this->classNameId);
36✔
89
                        $variables["use_statements"]->addUseStatement($classNameIdType);
36✔
90
                }
91
                if ($templateName === 'doctrine/Repository.tpl.php') {
36✔
92
                        $templateName = __DIR__ . '/../templates/extension/maker/Repository.tpl.php';
36✔
93
                        $variables['include_example_comments'] = false;
36✔
94
                        $variables['id_type'] = $this->idType;
36✔
95
                        $useGenerator->addUseStatement(QueryBuilder::class);
36✔
96
                        $useGenerator->addUseStatement($this->classNameId);
36✔
97

98
                }
99

100
                return $this->generator->generateClass($className, $templateName, $variables);
36✔
101
        }
102

103
        /**
104
         * @param array<string,mixed> $variables
105
         */
106
        public function generateFile(string $targetPath, string $templateName, array $variables = []): void {
UNCOV
107
                $this->generator->generateFile($targetPath, $templateName, $variables);
×
108
        }
109

110
        public function dumpFile(string $targetPath, string $contents): void {
UNCOV
111
                $this->generator->dumpFile($targetPath, $contents);
×
112
        }
113

114
        public function getFileContentsForPendingOperation(string $targetPath): string {
UNCOV
115
                return $this->generator->getFileContentsForPendingOperation($targetPath);
×
116
        }
117

118
        public function createClassNameDetails(
119
                string $name, string $namespacePrefix, string $suffix = '', string $validationErrorMessage = ''): ClassNameDetails {
120
                return $this->generator->createClassNameDetails($name, $namespacePrefix, $suffix,
36✔
121
                        $validationErrorMessage);
36✔
122
        }
123

124
        public function getRootDirectory(): string {
UNCOV
125
                return $this->generator->getRootDirectory();
×
126
        }
127

128
        public function hasPendingOperations(): bool {
129
                return $this->generator->hasPendingOperations();
36✔
130
        }
131

132
        public function writeChanges() {
133
                $this->generator->writeChanges();
36✔
134
        }
135

136
        public function getRootNamespace(): string {
UNCOV
137
                return $this->generator->getRootNamespace();
×
138
        }
139

140
        /**
141
         * @param string[] $parameters
142
         */
143
        public function generateController(string $controllerClassName, string $controllerTemplatePath, array $parameters = []): string {
144
                return $this->generator->generateController($controllerClassName, $controllerTemplatePath,
×
UNCOV
145
                        $parameters);
×
146
        }
147

148
        /**
149
         * @param array<string,mixed> $variables
150
         */
151
        public function generateTemplate(string $targetPath, string $templateName, array $variables = []): void {
UNCOV
152
                $this->generator->generateTemplate($targetPath, $templateName, $variables);
×
153
        }
154

155
        /**
156
         * @return string[]
157
         */
158
        public function getGeneratedFiles(): array {
159
                return $this->generator->getGeneratedFiles();
36✔
160
        }
161

162

163

164
}
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