Skip to content

Commit 8ab5e6f

Browse files
committed
Preparation for v3
* Support for Doctrine ORM v3 * Use of PHP type hints
1 parent cfcd687 commit 8ab5e6f

File tree

16 files changed

+134
-234
lines changed

16 files changed

+134
-234
lines changed

.php-cs-fixer.dist.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
'phpdoc_order' => true,
3737
'protected_to_private' => false,
3838
'phpdoc_to_comment' => ['ignored_tags' => ['psalm-suppress']],
39+
'phpdoc_to_param_type' => true,
40+
'phpdoc_to_return_type' => true,
41+
'phpdoc_to_property_type' => true,
3942
])
4043
->setFinder($finder)
4144
;

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ methods for Doctrine ORM entities.
77
![Tests](https://github.com/e-commit/doctrine-entities-generator-bundle/workflows/Tests/badge.svg)
88

99

10+
| Bundle version | Compatible with Doctrine ORM |
11+
|----------------|------------------------------|
12+
| 3.* | ≥ 3.2 ; < 4.0 |
13+
| 2.* | ≥ 2.7 ; < 3.0 |
14+
15+
16+
1017
## Installation ##
1118

1219
Install the bundle with Composer : In your project directory, execute the following command :

UPGRADE-2.0.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

UPGRADE-3.0.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# UPGRADE FROM 2.x to 3.0
2+
3+
## Doctrine ORM
4+
5+
The bundle is no longer compatible with Doctrine ORM v2. Version 3 (≥ 3.2) is required.
6+
7+
8+
## `Ecommit\DoctrineEntitiesGeneratorBundle\EntityGenerator\EntityGenerator` class
9+
10+
The signatures of the following methods have been modified :
11+
12+
Before :
13+
14+
```php
15+
protected function addField(GenerateEntityRequest $request, array $fieldMapping): void
16+
protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, array $embeddedMapping): void
17+
protected function addAssociation(GenerateEntityRequest $request, array $associationMapping): void
18+
protected function addAssociationToOne(GenerateEntityRequest $request, array $associationMapping, string $block, ?string $foreignMethodNameSet): void
19+
protected function addAssociationToMany(GenerateEntityRequest $request, array $associationMapping, string $block, ?string $foreignMethodNameAdd, ?string $foreignMethodNameRemove): void
20+
```
21+
22+
After :
23+
24+
```php
25+
protected function addField(GenerateEntityRequest $request, FieldMapping $fieldMapping): void
26+
protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, EmbeddedClassMapping $embeddedMapping): void
27+
protected function addAssociation(GenerateEntityRequest $request, AssociationMapping $associationMapping): void
28+
protected function addAssociationToOne(GenerateEntityRequest $request, AssociationMapping $associationMapping, string $block, ?string $foreignMethodNameSet): void
29+
protected function addAssociationToMany(GenerateEntityRequest $request, AssociationMapping $associationMapping, string $block, ?string $foreignMethodNameAdd, ?string $foreignMethodNameRemove): void
30+
```
31+
32+
With the following PHP classes:
33+
34+
* `Doctrine\ORM\Mapping\AssociationMapping`
35+
* `Doctrine\ORM\Mapping\EmbeddedClassMapping`
36+
* `Doctrine\ORM\Mapping\FieldMapping`
37+
38+
39+
## Template
40+
41+
The following variables available in the template are no longer arrays :
42+
43+
* `fieldMapping` : This variable is now a object of `Doctrine\ORM\Mapping\AssociationMapping`.
44+
* `embeddedMapping` : This variable is now a object of `Doctrine\ORM\Mapping\EmbeddedClassMapping`.
45+
* `associationMapping` : This variable is now a object of `Doctrine\ORM\Mapping\FieldMapping`.
46+
47+
## PHP type hint
48+
49+
Classes now use PHP type hints.

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
},
1919
"extra": {
2020
"branch-alias": {
21-
"dev-master": "2.x-dev"
21+
"dev-master": "3.x-dev"
2222
}
2323
},
2424
"require": {
2525
"php": "^8.1",
2626
"ext-mbstring": "*",
27-
"doctrine/collections": "^1.0|^2.0",
28-
"doctrine/doctrine-bundle": "^1.12.3|^2.4.5",
27+
"doctrine/collections": "^2.0",
28+
"doctrine/doctrine-bundle": "^2.11.1",
2929
"doctrine/inflector": "^1.4|^2.0",
30-
"doctrine/orm": "^2.7",
31-
"doctrine/persistence": "^1.1|^2.0|^3.0",
30+
"doctrine/orm": "^3.2",
31+
"doctrine/persistence": "^3.1",
3232
"nikic/php-parser": "^4.10",
3333
"symfony/config": "^6.4|^7.0",
3434
"symfony/console": "^6.4|^7.0",
@@ -41,7 +41,7 @@
4141
"twig/twig": "^2.12.0|^3.0"
4242
},
4343
"require-dev": {
44-
"doctrine/dbal": "^2.13.4|^3.1.4",
44+
"doctrine/dbal": "^3.6|^4.0",
4545
"friendsofphp/php-cs-fixer": "^3.0",
4646
"phpstan/extension-installer": "^1.2",
4747
"phpstan/phpstan": "^1.9",

phpstan-baseline.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,3 @@ parameters:
1212
paths:
1313
- tests/App/Entity/
1414
- tests/App/GeneratedEntity/
15-
- # arrays in Doctrine\ORM\Mapping\ClassMetadataInfo -> AssociationMapping not defined
16-
message: '#Method Ecommit\\DoctrineEntitiesGeneratorBundle\\EntityGenerator\\EntityGenerator::addAssociation.*\(\) has parameter \$associationMapping with no value type specified in iterable type array#'
17-
path: src/EntityGenerator/EntityGenerator.php

src/Command/GenerateEntitiesCommand.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,8 @@
2424
#[AsCommand(name: 'ecommit:doctrine:generate-entities', description: 'Generate Doctrine ORM entities')]
2525
class GenerateEntitiesCommand extends Command
2626
{
27-
/**
28-
* @var EntitySearcherInterface
29-
*/
30-
protected $searcher;
31-
32-
/**
33-
* @var EntityGeneratorInterface
34-
*/
35-
protected $generator;
36-
37-
public function __construct(EntitySearcherInterface $searcher, EntityGeneratorInterface $generator)
27+
public function __construct(protected EntitySearcherInterface $searcher, protected EntityGeneratorInterface $generator)
3828
{
39-
$this->searcher = $searcher;
40-
$this->generator = $generator;
41-
4229
parent::__construct();
4330
}
4431

src/EntityGenerator/EntityGenerator.php

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
use Doctrine\Inflector\Inflector;
1919
use Doctrine\Inflector\InflectorFactory;
2020
use Doctrine\ORM\EntityManagerInterface;
21-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
21+
use Doctrine\ORM\Mapping\AssociationMapping;
22+
use Doctrine\ORM\Mapping\ClassMetadata;
23+
use Doctrine\ORM\Mapping\EmbeddedClassMapping;
24+
use Doctrine\ORM\Mapping\FieldMapping;
25+
use Doctrine\ORM\Mapping\InverseSideMapping;
26+
use Doctrine\ORM\Mapping\OwningSideMapping;
2227
use Doctrine\Persistence\ManagerRegistry;
2328
use Ecommit\DoctrineEntitiesGeneratorBundle\Attribute\GenerateEntityTemplate;
2429
use Ecommit\DoctrineEntitiesGeneratorBundle\Entity\EntityInitializerInterface;
@@ -31,9 +36,6 @@
3136
use Twig\Environment;
3237

3338
/**
34-
* @phpstan-import-type FieldMapping from ClassMetadataInfo
35-
* @phpstan-import-type EmbeddedClassMapping from ClassMetadataInfo
36-
* @phpstan-import-type AssociationMapping from ClassMetadataInfo
3739
* @phpstan-import-type FileParts from GenerateEntityRequest
3840
*/
3941
class EntityGenerator implements EntityGeneratorInterface
@@ -43,37 +45,10 @@ class EntityGenerator implements EntityGeneratorInterface
4345
public const TYPE_ADD = 'add';
4446
public const TYPE_REMOVE = 'remove';
4547

46-
/**
47-
* @var EntitySearcherInterface
48-
*/
49-
protected $searcher;
48+
protected Inflector $inflector;
5049

51-
/**
52-
* @var ManagerRegistry
53-
*/
54-
protected $registry;
55-
56-
/**
57-
* @var Environment
58-
*/
59-
protected $twig;
60-
61-
/**
62-
* @var Inflector
63-
*/
64-
protected $inflector;
65-
66-
/**
67-
* @var string
68-
*/
69-
protected $template;
70-
71-
public function __construct(EntitySearcherInterface $searcher, ManagerRegistry $registry, Environment $twig, string $template)
50+
public function __construct(protected EntitySearcherInterface $searcher, protected ManagerRegistry $registry, protected Environment $twig, protected string $template)
7251
{
73-
$this->searcher = $searcher;
74-
$this->registry = $registry;
75-
$this->twig = $twig;
76-
$this->template = $template;
7752
$this->inflector = InflectorFactory::create()->build();
7853
}
7954

@@ -85,9 +60,6 @@ public function generate(string $className): void
8560
throw new ClassNotManagedException(\sprintf('Class "%s" not managed', $className));
8661
}
8762
$metadata = $entityManager->getClassMetadata($reflectionClass->getName());
88-
if (!$metadata instanceof ClassMetadataInfo) {
89-
throw new ClassNotManagedException(\sprintf('Class "%s" cannot be generated (Metatada not found)', $className));
90-
}
9163

9264
if (!$this->searcher->classCanBeGenerated($metadata)) {
9365
throw new ClassNotManagedException(\sprintf('Class "%s" cannot be generated (Is IgnoreGenerateEntity attribute used ?)', $className));
@@ -268,12 +240,9 @@ protected function methodIsDefinedOutsideBlock(GenerateEntityRequest $request, s
268240
return false;
269241
}
270242

271-
/**
272-
* @param FieldMapping $fieldMapping
273-
*/
274-
protected function addField(GenerateEntityRequest $request, array $fieldMapping): void
243+
protected function addField(GenerateEntityRequest $request, FieldMapping $fieldMapping): void
275244
{
276-
$fieldName = $fieldMapping['fieldName'];
245+
$fieldName = $fieldMapping->fieldName;
277246
$types = $request->doctrineExtractor->getTypes($request->reflectionClass->getName(), $fieldName);
278247
$phpType = (new \ReflectionProperty($request->reflectionClass->getName(), $fieldName))->getType();
279248

@@ -305,12 +274,9 @@ protected function addField(GenerateEntityRequest $request, array $fieldMapping)
305274
}
306275
}
307276

308-
/**
309-
* @param EmbeddedClassMapping $embeddedMapping
310-
*/
311-
protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, array $embeddedMapping): void
277+
protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, EmbeddedClassMapping $embeddedMapping): void
312278
{
313-
$targetClass = $embeddedMapping['class'];
279+
$targetClass = $embeddedMapping->class;
314280
$phpType = (new \ReflectionProperty($request->reflectionClass->getName(), $fieldName))->getType();
315281

316282
$targetClassAlias = $request->useStatementManipulator->addUseStatementIfNecessary($targetClass);
@@ -346,58 +312,55 @@ protected function addEmbedded(GenerateEntityRequest $request, string $fieldName
346312
}
347313
}
348314

349-
/**
350-
* @param AssociationMapping $associationMapping
351-
*/
352-
protected function addAssociation(GenerateEntityRequest $request, array $associationMapping): void
315+
protected function addAssociation(GenerateEntityRequest $request, AssociationMapping $associationMapping): void
353316
{
354-
if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE && $associationMapping['mappedBy']) {
317+
if ($associationMapping->type() & ClassMetadata::TO_ONE && $associationMapping instanceof InverseSideMapping) {
355318
$this->addAssociationToOne(
356319
$request,
357320
$associationMapping,
358321
'assocation_one_to_one_reverse',
359-
$this->buildMethodName(self::TYPE_SET, $associationMapping['mappedBy'])
322+
$this->buildMethodName(self::TYPE_SET, $associationMapping->mappedBy)
360323
);
361-
} elseif ($associationMapping['type'] & ClassMetadataInfo::TO_ONE && $associationMapping['inversedBy']) {
324+
} elseif ($associationMapping->type() & ClassMetadata::TO_ONE && $associationMapping instanceof OwningSideMapping) {
362325
$this->addAssociationToOne(
363326
$request,
364327
$associationMapping,
365328
'assocation_one_to_one_owning',
366329
null
367330
);
368-
} elseif ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
331+
} elseif ($associationMapping->type() & ClassMetadata::TO_ONE) {
369332
$this->addAssociationToOne(
370333
$request,
371334
$associationMapping,
372335
'assocation_one_to_one_unidirectional',
373336
null
374337
);
375-
} elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
376-
if ($associationMapping['type'] & ClassMetadataInfo::ONE_TO_MANY && $associationMapping['mappedBy']) {
338+
} elseif ($associationMapping->type() & ClassMetadata::TO_MANY) {
339+
if ($associationMapping->type() & ClassMetadata::ONE_TO_MANY && $associationMapping instanceof InverseSideMapping) {
377340
$this->addAssociationToMany(
378341
$request,
379342
$associationMapping,
380343
'assocation_one_to_many_reverse',
381-
$this->buildMethodName(self::TYPE_SET, $associationMapping['mappedBy']),
382-
$this->buildMethodName(self::TYPE_SET, $associationMapping['mappedBy'])
344+
$this->buildMethodName(self::TYPE_SET, $associationMapping->mappedBy),
345+
$this->buildMethodName(self::TYPE_SET, $associationMapping->mappedBy)
383346
);
384-
} elseif ($associationMapping['type'] & ClassMetadataInfo::MANY_TO_MANY && $associationMapping['mappedBy']) {
347+
} elseif ($associationMapping->type() & ClassMetadata::MANY_TO_MANY && $associationMapping instanceof InverseSideMapping) {
385348
$this->addAssociationToMany(
386349
$request,
387350
$associationMapping,
388351
'assocation_many_to_many_reverse',
389-
$this->buildMethodName(self::TYPE_ADD, $associationMapping['mappedBy']),
390-
$this->buildMethodName(self::TYPE_REMOVE, $associationMapping['mappedBy'])
352+
$this->buildMethodName(self::TYPE_ADD, $associationMapping->mappedBy),
353+
$this->buildMethodName(self::TYPE_REMOVE, $associationMapping->mappedBy)
391354
);
392-
} elseif ($associationMapping['type'] & ClassMetadataInfo::MANY_TO_MANY && $associationMapping['inversedBy']) {
355+
} elseif ($associationMapping->type() & ClassMetadata::MANY_TO_MANY && $associationMapping instanceof OwningSideMapping) {
393356
$this->addAssociationToMany(
394357
$request,
395358
$associationMapping,
396359
'assocation_many_to_many_owning',
397360
null,
398361
null
399362
);
400-
} elseif ($associationMapping['type'] & ClassMetadataInfo::MANY_TO_MANY) {
363+
} elseif ($associationMapping->type() & ClassMetadata::MANY_TO_MANY) {
401364
$this->addAssociationToMany(
402365
$request,
403366
$associationMapping,
@@ -409,13 +372,10 @@ protected function addAssociation(GenerateEntityRequest $request, array $associa
409372
}
410373
}
411374

412-
/**
413-
* @param AssociationMapping $associationMapping
414-
*/
415-
protected function addAssociationToOne(GenerateEntityRequest $request, array $associationMapping, string $block, ?string $foreignMethodNameSet): void
375+
protected function addAssociationToOne(GenerateEntityRequest $request, AssociationMapping $associationMapping, string $block, ?string $foreignMethodNameSet): void
416376
{
417-
$fieldName = $associationMapping['fieldName'];
418-
$targetEntity = $associationMapping['targetEntity'];
377+
$fieldName = $associationMapping->fieldName;
378+
$targetEntity = $associationMapping->targetEntity;
419379

420380
$targetEntityAlias = $request->useStatementManipulator->addUseStatementIfNecessary($targetEntity);
421381
if ($request->reflectionClass->getName() === $targetEntity) {
@@ -449,13 +409,10 @@ protected function addAssociationToOne(GenerateEntityRequest $request, array $as
449409
}
450410
}
451411

452-
/**
453-
* @param AssociationMapping $associationMapping
454-
*/
455-
protected function addAssociationToMany(GenerateEntityRequest $request, array $associationMapping, string $block, ?string $foreignMethodNameAdd, ?string $foreignMethodNameRemove): void
412+
protected function addAssociationToMany(GenerateEntityRequest $request, AssociationMapping $associationMapping, string $block, ?string $foreignMethodNameAdd, ?string $foreignMethodNameRemove): void
456413
{
457-
$fieldName = $associationMapping['fieldName'];
458-
$targetEntity = $associationMapping['targetEntity'];
414+
$fieldName = $associationMapping->fieldName;
415+
$targetEntity = $associationMapping->targetEntity;
459416

460417
$targetEntityAlias = $request->useStatementManipulator->addUseStatementIfNecessary($targetEntity);
461418
if ($request->reflectionClass->getName() === $targetEntity) {

0 commit comments

Comments
 (0)