Skip to content

Commit 7362329

Browse files
jrushlowOskarStarkweaverryan
authored
implement types and constructor property promotion for utilities (#1124)
Co-authored-by: Oskar Stark <[email protected]> Co-authored-by: Ryan Weaver <[email protected]>
1 parent ced11a7 commit 7362329

18 files changed

+124
-211
lines changed

src/Doctrine/DoctrineHelper.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ public function getEntitiesForAutocomplete(): array
166166
return $entities;
167167
}
168168

169-
/**
170-
* @return array|ClassMetadata
171-
*/
172-
public function getMetadata(string $classOrNamespace = null, bool $disconnected = false)
169+
public function getMetadata(string $classOrNamespace = null, bool $disconnected = false): array|ClassMetadata
173170
{
174171
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
175172
foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) {

src/Doctrine/EntityClassGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@
3030
*/
3131
final class EntityClassGenerator
3232
{
33-
private $generator;
34-
private $doctrineHelper;
35-
36-
public function __construct(Generator $generator, DoctrineHelper $doctrineHelper)
37-
{
38-
$this->generator = $generator;
39-
$this->doctrineHelper = $doctrineHelper;
33+
public function __construct(
34+
private Generator $generator,
35+
private DoctrineHelper $doctrineHelper,
36+
) {
4037
}
4138

4239
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false): string

src/Doctrine/EntityDetails.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@
2121
*/
2222
final class EntityDetails
2323
{
24-
private $metadata;
25-
26-
/**
27-
* @param ClassMetadata|LegacyClassMetadata $metadata
28-
*/
29-
public function __construct($metadata)
30-
{
31-
$this->metadata = $metadata;
24+
public function __construct(
25+
private ClassMetadata|LegacyClassMetadata $metadata
26+
) {
3227
}
3328

3429
public function getRepositoryClass(): ?string

src/Doctrine/EntityRegenerator.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@
2525
*/
2626
final class EntityRegenerator
2727
{
28-
private $doctrineHelper;
29-
private $fileManager;
30-
private $generator;
31-
private $entityClassGenerator;
32-
private $overwrite;
33-
34-
public function __construct(DoctrineHelper $doctrineHelper, FileManager $fileManager, Generator $generator, EntityClassGenerator $entityClassGenerator, bool $overwrite)
35-
{
36-
$this->doctrineHelper = $doctrineHelper;
37-
$this->fileManager = $fileManager;
38-
$this->generator = $generator;
39-
$this->entityClassGenerator = $entityClassGenerator;
40-
$this->overwrite = $overwrite;
28+
public function __construct(
29+
private DoctrineHelper $doctrineHelper,
30+
private FileManager $fileManager,
31+
private Generator $generator,
32+
private EntityClassGenerator $entityClassGenerator,
33+
private bool $overwrite
34+
) {
4135
}
4236

4337
public function regenerateEntities(string $classOrNamespace): void
@@ -263,7 +257,7 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array
263257
$targetFields = array_diff($targetFields, $traitProperties);
264258

265259
// exclude inherited properties
266-
$targetFields = array_filter($targetFields, function ($field) use ($classReflection) {
260+
$targetFields = array_filter($targetFields, static function ($field) use ($classReflection) {
267261
return $classReflection->hasProperty($field) &&
268262
$classReflection->getProperty($field)->getDeclaringClass()->getName() == $classReflection->getName();
269263
});

src/Doctrine/EntityRelation.php

Lines changed: 66 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,18 @@ final class EntityRelation
2121
public const MANY_TO_MANY = 'ManyToMany';
2222
public const ONE_TO_ONE = 'OneToOne';
2323

24-
private $type;
25-
26-
private $owningClass;
27-
28-
private $inverseClass;
29-
3024
private $owningProperty;
31-
3225
private $inverseProperty;
33-
34-
private $isNullable = false;
35-
36-
private $isSelfReferencing = false;
37-
38-
private $orphanRemoval = false;
39-
40-
private $mapInverseRelation = true;
41-
42-
public function __construct(string $type, string $owningClass, string $inverseClass)
43-
{
26+
private bool $isNullable = false;
27+
private bool $isSelfReferencing = false;
28+
private bool $orphanRemoval = false;
29+
private bool $mapInverseRelation = true;
30+
31+
public function __construct(
32+
private string $type,
33+
private string $owningClass,
34+
private string $inverseClass
35+
) {
4436
if (!\in_array($type, self::getValidRelationTypes())) {
4537
throw new \Exception(sprintf('Invalid relation type "%s"', $type));
4638
}
@@ -49,9 +41,6 @@ public function __construct(string $type, string $owningClass, string $inverseCl
4941
throw new \Exception('Use ManyToOne instead of OneToMany');
5042
}
5143

52-
$this->type = $type;
53-
$this->owningClass = $owningClass;
54-
$this->inverseClass = $inverseClass;
5544
$this->isSelfReferencing = $owningClass === $inverseClass;
5645
}
5746

@@ -89,78 +78,60 @@ public static function getValidRelationTypes(): array
8978
];
9079
}
9180

92-
public function getOwningRelation()
93-
{
94-
switch ($this->getType()) {
95-
case self::MANY_TO_ONE:
96-
return (new RelationManyToOne())
97-
->setPropertyName($this->owningProperty)
98-
->setTargetClassName($this->inverseClass)
99-
->setTargetPropertyName($this->inverseProperty)
100-
->setIsNullable($this->isNullable)
101-
->setIsSelfReferencing($this->isSelfReferencing)
102-
->setMapInverseRelation($this->mapInverseRelation)
103-
;
104-
break;
105-
case self::MANY_TO_MANY:
106-
return (new RelationManyToMany())
107-
->setPropertyName($this->owningProperty)
108-
->setTargetClassName($this->inverseClass)
109-
->setTargetPropertyName($this->inverseProperty)
110-
->setIsOwning(true)->setMapInverseRelation($this->mapInverseRelation)
111-
->setIsSelfReferencing($this->isSelfReferencing)
112-
;
113-
break;
114-
case self::ONE_TO_ONE:
115-
return (new RelationOneToOne())
116-
->setPropertyName($this->owningProperty)
117-
->setTargetClassName($this->inverseClass)
118-
->setTargetPropertyName($this->inverseProperty)
119-
->setIsNullable($this->isNullable)
120-
->setIsOwning(true)
121-
->setIsSelfReferencing($this->isSelfReferencing)
122-
->setMapInverseRelation($this->mapInverseRelation)
123-
;
124-
break;
125-
default:
126-
throw new \InvalidArgumentException('Invalid type');
127-
}
128-
}
129-
130-
public function getInverseRelation()
131-
{
132-
switch ($this->getType()) {
133-
case self::MANY_TO_ONE:
134-
return (new RelationOneToMany())
135-
->setPropertyName($this->inverseProperty)
136-
->setTargetClassName($this->owningClass)
137-
->setTargetPropertyName($this->owningProperty)
138-
->setOrphanRemoval($this->orphanRemoval)
139-
->setIsSelfReferencing($this->isSelfReferencing)
140-
;
141-
break;
142-
case self::MANY_TO_MANY:
143-
return (new RelationManyToMany())
144-
->setPropertyName($this->inverseProperty)
145-
->setTargetClassName($this->owningClass)
146-
->setTargetPropertyName($this->owningProperty)
147-
->setIsOwning(false)
148-
->setIsSelfReferencing($this->isSelfReferencing)
149-
;
150-
break;
151-
case self::ONE_TO_ONE:
152-
return (new RelationOneToOne())
153-
->setPropertyName($this->inverseProperty)
154-
->setTargetClassName($this->owningClass)
155-
->setTargetPropertyName($this->owningProperty)
156-
->setIsNullable($this->isNullable)
157-
->setIsOwning(false)
158-
->setIsSelfReferencing($this->isSelfReferencing)
159-
;
160-
break;
161-
default:
162-
throw new \InvalidArgumentException('Invalid type');
163-
}
81+
public function getOwningRelation(): RelationManyToMany|RelationOneToOne|RelationManyToOne
82+
{
83+
return match ($this->getType()) {
84+
self::MANY_TO_ONE => (new RelationManyToOne())
85+
->setPropertyName($this->owningProperty)
86+
->setTargetClassName($this->inverseClass)
87+
->setTargetPropertyName($this->inverseProperty)
88+
->setIsNullable($this->isNullable)
89+
->setIsSelfReferencing($this->isSelfReferencing)
90+
->setMapInverseRelation($this->mapInverseRelation),
91+
self::MANY_TO_MANY => (new RelationManyToMany())
92+
->setPropertyName($this->owningProperty)
93+
->setTargetClassName($this->inverseClass)
94+
->setTargetPropertyName($this->inverseProperty)
95+
->setIsOwning(true)->setMapInverseRelation(
96+
$this->mapInverseRelation
97+
)
98+
->setIsSelfReferencing($this->isSelfReferencing),
99+
self::ONE_TO_ONE => (new RelationOneToOne())
100+
->setPropertyName($this->owningProperty)
101+
->setTargetClassName($this->inverseClass)
102+
->setTargetPropertyName($this->inverseProperty)
103+
->setIsNullable($this->isNullable)
104+
->setIsOwning(true)
105+
->setIsSelfReferencing($this->isSelfReferencing)
106+
->setMapInverseRelation($this->mapInverseRelation),
107+
default => throw new \InvalidArgumentException('Invalid type'),
108+
};
109+
}
110+
111+
public function getInverseRelation(): RelationManyToMany|RelationOneToOne|RelationOneToMany
112+
{
113+
return match ($this->getType()) {
114+
self::MANY_TO_ONE => (new RelationOneToMany())
115+
->setPropertyName($this->inverseProperty)
116+
->setTargetClassName($this->owningClass)
117+
->setTargetPropertyName($this->owningProperty)
118+
->setOrphanRemoval($this->orphanRemoval)
119+
->setIsSelfReferencing($this->isSelfReferencing),
120+
self::MANY_TO_MANY => (new RelationManyToMany())
121+
->setPropertyName($this->inverseProperty)
122+
->setTargetClassName($this->owningClass)
123+
->setTargetPropertyName($this->owningProperty)
124+
->setIsOwning(false)
125+
->setIsSelfReferencing($this->isSelfReferencing),
126+
self::ONE_TO_ONE => (new RelationOneToOne())
127+
->setPropertyName($this->inverseProperty)
128+
->setTargetClassName($this->owningClass)
129+
->setTargetPropertyName($this->owningProperty)
130+
->setIsNullable($this->isNullable)
131+
->setIsOwning(false)
132+
->setIsSelfReferencing($this->isSelfReferencing),
133+
default => throw new \InvalidArgumentException('Invalid type'),
134+
};
164135
}
165136

166137
public function getType(): string
@@ -178,7 +149,7 @@ public function getInverseClass(): string
178149
return $this->inverseClass;
179150
}
180151

181-
public function getOwningProperty()
152+
public function getOwningProperty(): string
182153
{
183154
return $this->owningProperty;
184155
}
@@ -203,7 +174,7 @@ public function getMapInverseRelation(): bool
203174
return $this->mapInverseRelation;
204175
}
205176

206-
public function setMapInverseRelation(bool $mapInverseRelation)
177+
public function setMapInverseRelation(bool $mapInverseRelation): void
207178
{
208179
if ($mapInverseRelation && $this->inverseProperty) {
209180
throw new \Exception('Cannot set setMapInverseRelation() to true when the inverse relation property is set.');

src/Resources/config/services.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
</service>
7878

7979
<service id="maker.template_component_generator" class="Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator">
80-
<argument type="service" id="maker.php_compat_util" />
8180
</service>
8281
</services>
8382
</container>

src/Security/InteractiveSecurityHelper.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function guessFirewallName(SymfonyStyle $io, array $securityData, string
2525
{
2626
$realFirewalls = array_filter(
2727
$securityData['security']['firewalls'] ?? [],
28-
function ($item) {
28+
static function ($item) {
2929
return !isset($item['security']) || true === $item['security'];
3030
}
3131
);
@@ -53,13 +53,11 @@ public function guessUserClass(SymfonyStyle $io, array $providers, string $quest
5353
return $entityProvider['entity']['class'];
5454
}
5555

56-
$userClass = $io->ask(
56+
return $io->ask(
5757
$questionText ?? 'Enter the User class that you want to authenticate (e.g. <fg=yellow>App\\Entity\\User</>)',
5858
$this->guessUserClassDefault(),
5959
[Validator::class, 'classIsUserInterface']
6060
);
61-
62-
return $userClass;
6361
}
6462

6563
private function guessUserClassDefault(): string
@@ -147,7 +145,7 @@ public function guessPasswordField(SymfonyStyle $io, string $userClass): string
147145
public function getAuthenticatorClasses(array $firewallData): array
148146
{
149147
if (isset($firewallData['guard'])) {
150-
return array_filter($firewallData['guard']['authenticators'] ?? [], function ($authenticator) {
148+
return array_filter($firewallData['guard']['authenticators'] ?? [], static function ($authenticator) {
151149
return class_exists($authenticator);
152150
});
153151
}
@@ -158,7 +156,7 @@ public function getAuthenticatorClasses(array $firewallData): array
158156
$authenticators = [$authenticators];
159157
}
160158

161-
return array_filter($authenticators, function ($authenticator) {
159+
return array_filter($authenticators, static function ($authenticator) {
162160
return class_exists($authenticator);
163161
});
164162
}

src/Security/SecurityConfigUpdater.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
*/
2424
final class SecurityConfigUpdater
2525
{
26-
/** @var YamlSourceManipulator */
27-
private $manipulator;
26+
private ?YamlSourceManipulator $manipulator;
2827

29-
/** @var Logger|null */
30-
private $ysmLogger;
31-
32-
public function __construct(Logger $ysmLogger = null)
33-
{
34-
$this->ysmLogger = $ysmLogger;
28+
public function __construct(
29+
private ?Logger $ysmLogger = null
30+
) {
3531
}
3632

3733
/**

src/Security/SecurityControllerBuilder.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
*/
2323
final class SecurityControllerBuilder
2424
{
25-
private $phpCompatUtil;
26-
27-
public function __construct(PhpCompatUtil $phpCompatUtil)
28-
{
29-
$this->phpCompatUtil = $phpCompatUtil;
25+
public function __construct(
26+
private PhpCompatUtil $phpCompatUtil
27+
) {
3028
}
3129

3230
public function addLoginMethod(ClassSourceManipulator $manipulator): void

src/Security/UserClassConfiguration.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,14 @@
1818
*/
1919
final class UserClassConfiguration
2020
{
21-
private $isEntity;
22-
23-
private $identityPropertyName;
24-
25-
private $hasPassword;
26-
27-
private $useArgon2 = false;
28-
29-
private $userProviderClass;
30-
31-
public function __construct(bool $isEntity, string $identityPropertyName, bool $hasPassword)
32-
{
33-
$this->isEntity = $isEntity;
34-
$this->identityPropertyName = $identityPropertyName;
35-
$this->hasPassword = $hasPassword;
21+
private bool $useArgon2 = false;
22+
private string $userProviderClass;
23+
24+
public function __construct(
25+
private bool $isEntity,
26+
private string $identityPropertyName,
27+
private bool $hasPassword,
28+
) {
3629
}
3730

3831
public function isEntity(): bool

0 commit comments

Comments
 (0)