Skip to content

Commit c845776

Browse files
committed
rector php 8 support
1 parent bbb845c commit c845776

18 files changed

+83
-93
lines changed

src/DependencyBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,6 @@ private function calculateMissingDependencies(array $dependencies): array
139139
return [];
140140
}
141141

142-
return array_unique(array_merge($missingPackages, $missingOptionalPackages));
142+
return array_unique([...$missingPackages, ...$missingOptionalPackages]);
143143
}
144144
}

src/Docker/DockerDatabaseServices.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,11 @@ public static function getSuggestedServiceVersion(string $name): string
8383

8484
public static function getMissingExtensionName(string $name): ?string
8585
{
86-
switch ($name) {
87-
case 'mariadb':
88-
case 'mysql':
89-
$driver = 'mysql';
90-
break;
91-
case 'postgres':
92-
$driver = 'pgsql';
93-
break;
94-
default:
95-
self::throwInvalidDatabase($name);
96-
}
86+
$driver = match ($name) {
87+
'mariadb', 'mysql' => 'mysql',
88+
'postgres' => 'pgsql',
89+
default => self::throwInvalidDatabase($name),
90+
};
9791

9892
if (!\in_array($driver, \PDO::getAvailableDrivers(), true)) {
9993
return $driver;

src/Doctrine/DoctrineHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool
6868
try {
6969
/** @var EntityManagerInterface $em */
7070
$em = $this->getRegistry()->getManagerForClass($className);
71-
} catch (\ReflectionException $exception) {
71+
} catch (\ReflectionException) {
7272
// this exception will be thrown by the registry if the class isn't created yet.
7373
// an example case is the "make:entity" command, which needs to know which driver is used for the class to determine
7474
// if the class should be generated with attributes or annotations. If this exception is thrown, we will check based on the
@@ -167,7 +167,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
167167
if ($disconnected) {
168168
try {
169169
$loaded = $cmf->getAllMetadata();
170-
} catch (ORMMappingException|PersistenceMappingException $e) {
170+
} catch (ORMMappingException|PersistenceMappingException) {
171171
$loaded = $this->isInstanceOf($cmf, AbstractClassMetadataFactory::class) ? $cmf->getLoadedMetadata() : [];
172172
}
173173

src/Doctrine/EntityDetails.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public function getFormFields(): array
5252

5353
if (!empty($this->metadata->embeddedClasses)) {
5454
foreach (array_keys($this->metadata->embeddedClasses) as $embeddedClassKey) {
55-
$fields = array_filter($fields, function ($v) use ($embeddedClassKey) {
56-
return 0 !== strpos($v, $embeddedClassKey.'.');
57-
});
55+
$fields = array_filter($fields, static fn ($v) => !str_starts_with($v, $embeddedClassKey.'.'));
5856
}
5957
}
6058

src/Doctrine/EntityRegenerator.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function regenerateEntities(string $classOrNamespace): void
3838
{
3939
try {
4040
$metadata = $this->doctrineHelper->getMetadata($classOrNamespace);
41-
} catch (MappingException|LegacyCommonMappingException|PersistenceMappingException $mappingException) {
41+
} catch (MappingException|LegacyCommonMappingException|PersistenceMappingException) {
4242
$metadata = $this->doctrineHelper->getMetadata($classOrNamespace, true);
4343
}
4444

@@ -72,7 +72,7 @@ public function regenerateEntities(string $classOrNamespace): void
7272
$embeddedClasses = [];
7373

7474
foreach ($classMetadata->embeddedClasses as $fieldName => $mapping) {
75-
if (false !== strpos($fieldName, '.')) {
75+
if (str_contains($fieldName, '.')) {
7676
continue;
7777
}
7878

@@ -91,7 +91,7 @@ public function regenerateEntities(string $classOrNamespace): void
9191

9292
foreach ($classMetadata->fieldMappings as $fieldName => $mapping) {
9393
// skip embedded fields
94-
if (false !== strpos($fieldName, '.')) {
94+
if (str_contains($fieldName, '.')) {
9595
[$fieldName, $embeddedFiledName] = explode('.', $fieldName);
9696

9797
$operations[$embeddedClasses[$fieldName]]->addEntityField($embeddedFiledName, $mapping);
@@ -238,11 +238,11 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array
238238
/** @var \ReflectionClass $classReflection */
239239
$classReflection = $classMetadata->reflClass;
240240

241-
$targetFields = array_merge(
242-
array_keys($classMetadata->fieldMappings),
243-
array_keys($classMetadata->associationMappings),
244-
array_keys($classMetadata->embeddedClasses)
245-
);
241+
$targetFields = [
242+
...array_keys($classMetadata->fieldMappings),
243+
...array_keys($classMetadata->associationMappings),
244+
...array_keys($classMetadata->embeddedClasses),
245+
];
246246

247247
if ($classReflection) {
248248
// exclude traits
@@ -257,10 +257,8 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array
257257
$targetFields = array_diff($targetFields, $traitProperties);
258258

259259
// exclude inherited properties
260-
$targetFields = array_filter($targetFields, static function ($field) use ($classReflection) {
261-
return $classReflection->hasProperty($field) &&
262-
$classReflection->getProperty($field)->getDeclaringClass()->getName() == $classReflection->getName();
263-
});
260+
$targetFields = array_filter($targetFields, static fn ($field) => $classReflection->hasProperty($field) &&
261+
$classReflection->getProperty($field)->getDeclaringClass()->getName() === $classReflection->getName());
264262
}
265263

266264
return $targetFields;

src/Maker/AbstractMaker.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
*/
2323
abstract class AbstractMaker implements MakerInterface
2424
{
25+
/**
26+
* @return void
27+
*/
2528
public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
2629
{
2730
}
2831

32+
/**
33+
* @return void
34+
*/
2935
protected function writeSuccessMessage(ConsoleStyle $io)
3036
{
3137
$io->newLine();
@@ -44,7 +50,7 @@ protected function addDependencies(array $dependencies, string $message = null):
4450
}
4551

4652
return $dependencyBuilder->getMissingPackagesMessage(
47-
$this->getCommandName(),
53+
static::getCommandName(),
4854
$message
4955
);
5056
}

src/Maker/MakeAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
218218
);
219219
$generator->dumpFile($path, $newYaml);
220220
$securityYamlUpdated = true;
221-
} catch (YamlManipulationFailedException $e) {
221+
} catch (YamlManipulationFailedException) {
222222
}
223223

224224
if (self::AUTH_TYPE_FORM_LOGIN === $input->getArgument('authenticator-type')) {

src/Maker/MakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
5858
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
5959
{
6060
$commandName = trim($input->getArgument('name'));
61-
$commandNameHasAppPrefix = 0 === strpos($commandName, 'app:');
61+
$commandNameHasAppPrefix = str_starts_with($commandName, 'app:');
6262

6363
$commandClassNameDetails = $generator->createClassNameDetails(
6464
$commandNameHasAppPrefix ? substr($commandName, 4) : $commandName,

src/Maker/MakeDockerDatabase.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,27 @@
3030
*/
3131
final class MakeDockerDatabase extends AbstractMaker
3232
{
33-
private $fileManager;
3433
private $composeFilePath;
3534

36-
/**
37-
* @var ComposeFileManipulator
38-
*/
39-
private $composeFileManipulator;
35+
private ?ComposeFileManipulator $composeFileManipulator = null;
4036

4137
/**
4238
* @var string type of database selected by the user
4339
*/
44-
private $databaseChoice;
40+
private ?string $databaseChoice = null;
4541

4642
/**
4743
* @var string Service identifier to be set in docker-compose.yaml
4844
*/
49-
private $serviceName = 'database';
45+
private string $serviceName = 'database';
5046

5147
/**
5248
* @var string Version set in docker-compose.yaml for the service. e.g. latest
5349
*/
54-
private $serviceVersion = 'latest';
50+
private string $serviceVersion = 'latest';
5551

56-
public function __construct(FileManager $fileManager)
52+
public function __construct(private FileManager $fileManager)
5753
{
58-
$this->fileManager = $fileManager;
5954
}
6055

6156
public static function getCommandName(): string
@@ -77,6 +72,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7772

7873
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
7974
{
75+
$serviceNameMsg = [];
8076
$io->section('- Docker Compose Setup-');
8177

8278
$this->composeFileManipulator = new ComposeFileManipulator($this->getComposeFileContents($io));
@@ -116,6 +112,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
116112

117113
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
118114
{
115+
$closing = [];
119116
$io->newLine();
120117

121118
$service = DockerDatabaseServices::getDatabaseSkeleton($this->databaseChoice, $this->serviceVersion);

src/Maker/MakeEntity.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
369369
$defaultType = 'datetime_immutable';
370370
} elseif ('_id' === $suffix) {
371371
$defaultType = 'integer';
372-
} elseif (0 === strpos($snakeCasedField, 'is_')) {
372+
} elseif (str_starts_with($snakeCasedField, 'is_')) {
373373
$defaultType = 'boolean';
374-
} elseif (0 === strpos($snakeCasedField, 'has_')) {
374+
} elseif (str_starts_with($snakeCasedField, 'has_')) {
375375
$defaultType = 'boolean';
376376
} elseif ('uuid' === $snakeCasedField) {
377377
$defaultType = 'uuid';
@@ -479,9 +479,9 @@ private function printAvailableTypes(ConsoleStyle $io): void
479479
if (\is_string($subTypes) && $subTypes) {
480480
$line .= sprintf(' (%s)', $subTypes);
481481
} elseif (\is_array($subTypes) && !empty($subTypes)) {
482-
$line .= sprintf(' (or %s)', implode(', ', array_map(function ($subType) {
483-
return sprintf('<comment>%s</comment>', $subType);
484-
}, $subTypes)));
482+
$line .= sprintf(' (or %s)', implode(', ', array_map(
483+
static fn ($subType) => sprintf('<comment>%s</comment>', $subType), $subTypes))
484+
);
485485

486486
foreach ($subTypes as $subType) {
487487
unset($allTypes[$subType]);
@@ -508,9 +508,7 @@ private function printAvailableTypes(ConsoleStyle $io): void
508508

509509
$io->writeln('<info>Other Types</info>');
510510
// empty the values
511-
$allTypes = array_map(function () {
512-
return [];
513-
}, $allTypes);
511+
$allTypes = array_map(static fn () => [], $allTypes);
514512
$printSection($allTypes);
515513
}
516514

@@ -551,31 +549,27 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla
551549
$type = $this->askRelationType($io, $generatedEntityClass, $targetEntityClass);
552550
}
553551

554-
$askFieldName = function (string $targetClass, string $defaultValue) use ($io) {
555-
return $io->ask(
556-
sprintf('New field name inside %s', Str::getShortClassName($targetClass)),
557-
$defaultValue,
558-
function ($name) use ($targetClass) {
559-
// it's still *possible* to create duplicate properties - by
560-
// trying to generate the same property 2 times during the
561-
// same make:entity run. property_exists() only knows about
562-
// properties that *originally* existed on this class.
563-
if (property_exists($targetClass, $name)) {
564-
throw new \InvalidArgumentException(sprintf('The "%s" class already has a "%s" property.', $targetClass, $name));
565-
}
566-
567-
return Validator::validateDoctrineFieldName($name, $this->doctrineHelper->getRegistry());
552+
$askFieldName = fn (string $targetClass, string $defaultValue) => $io->ask(
553+
sprintf('New field name inside %s', Str::getShortClassName($targetClass)),
554+
$defaultValue,
555+
function ($name) use ($targetClass) {
556+
// it's still *possible* to create duplicate properties - by
557+
// trying to generate the same property 2 times during the
558+
// same make:entity run. property_exists() only knows about
559+
// properties that *originally* existed on this class.
560+
if (property_exists($targetClass, $name)) {
561+
throw new \InvalidArgumentException(sprintf('The "%s" class already has a "%s" property.', $targetClass, $name));
568562
}
569-
);
570-
};
571563

572-
$askIsNullable = static function (string $propertyName, string $targetClass) use ($io) {
573-
return $io->confirm(sprintf(
574-
'Is the <comment>%s</comment>.<comment>%s</comment> property allowed to be null (nullable)?',
575-
Str::getShortClassName($targetClass),
576-
$propertyName
577-
));
578-
};
564+
return Validator::validateDoctrineFieldName($name, $this->doctrineHelper->getRegistry());
565+
}
566+
);
567+
568+
$askIsNullable = static fn (string $propertyName, string $targetClass) => $io->confirm(sprintf(
569+
'Is the <comment>%s</comment>.<comment>%s</comment> property allowed to be null (nullable)?',
570+
Str::getShortClassName($targetClass),
571+
$propertyName
572+
));
579573

580574
$askOrphanRemoval = static function (string $owningClass, string $inverseClass) use ($io) {
581575
$io->text([
@@ -841,9 +835,7 @@ private function getPropertyNames(string $class): array
841835

842836
$reflClass = new \ReflectionClass($class);
843837

844-
return array_map(static function (\ReflectionProperty $prop) {
845-
return $prop->getName();
846-
}, $reflClass->getProperties());
838+
return array_map(static fn (\ReflectionProperty $prop) => $prop->getName(), $reflClass->getProperties());
847839
}
848840

849841
/** @legacy Drop when Annotations are no longer supported */

src/Maker/MakeForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
6767
$entities = $this->entityHelper->getEntitiesForAutocomplete();
6868

6969
$question = new Question($argument->getDescription());
70-
$question->setValidator(function ($answer) use ($entities) {return Validator::existsOrNull($answer, $entities); });
70+
$question->setValidator(fn ($answer) => Validator::existsOrNull($answer, $entities));
7171
$question->setAutocompleterValues($entities);
7272
$question->setMaxAttempts(3);
7373

src/Maker/MakeMessage.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
5858
{
5959
$command->addArgument('chosen-transport', InputArgument::OPTIONAL);
6060

61+
$messengerData = [];
62+
6163
try {
6264
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents('config/packages/messenger.yaml'));
6365
$messengerData = $manipulator->getData();
64-
} catch (\Exception $e) {
66+
} catch (\Exception) {
6567
}
6668

67-
if (!isset($messengerData, $messengerData['framework']['messenger']['transports'])) {
69+
if (!isset($messengerData['framework']['messenger']['transports'])) {
6870
return;
6971
}
7072

src/Maker/MakeMigration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
104104

105105
$migrationOutput = $commandOutput->fetch();
106106

107-
if (false !== strpos($migrationOutput, 'No changes detected')) {
107+
if (str_contains($migrationOutput, 'No changes detected')) {
108108
$this->noChangesMessage($io);
109109

110110
return;
111111
}
112-
} catch (\Doctrine\Migrations\Generator\Exception\NoChangesDetected $exception) {
112+
} catch (\Doctrine\Migrations\Generator\Exception\NoChangesDetected) {
113113
$this->noChangesMessage($io);
114114

115115
return;

src/Maker/MakeRegistrationForm.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ final class MakeRegistrationForm extends AbstractMaker
7070
private $userClass;
7171
private $usernameField;
7272
private $passwordField;
73-
private $willVerifyEmail = false;
74-
private $verifyEmailAnonymously = false;
73+
private bool $willVerifyEmail = false;
74+
private bool $verifyEmailAnonymously = false;
7575
private $idGetter;
7676
private $emailGetter;
7777
private $fromEmailAddress;
@@ -80,7 +80,7 @@ final class MakeRegistrationForm extends AbstractMaker
8080
private $firewallName;
8181
private $redirectRouteName;
8282
private $addUniqueEntityConstraint;
83-
private $useNewAuthenticatorSystem = false;
83+
private bool $useNewAuthenticatorSystem = false;
8484

8585
public function __construct(
8686
private FileManager $fileManager,
@@ -109,6 +109,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
109109

110110
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
111111
{
112+
$emailText = [];
112113
$interactiveSecurityHelper = new InteractiveSecurityHelper();
113114

114115
if (!$this->fileManager->fileExists($path = 'config/packages/security.yaml')) {
@@ -228,7 +229,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
228229
$userDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($userClassNameDetails->getFullName());
229230

230231
$userRepoVars = [
231-
'repository_full_class_name' => 'Doctrine\ORM\EntityManagerInterface',
232+
'repository_full_class_name' => EntityManagerInterface::class,
232233
'repository_class_name' => 'EntityManagerInterface',
233234
'repository_var' => '$manager',
234235
];
@@ -415,6 +416,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
415416

416417
private function successMessage(ConsoleStyle $io, bool $emailVerification, string $userClass): void
417418
{
419+
$closing = [];
418420
$closing[] = 'Next:';
419421

420422
if (!$emailVerification) {

0 commit comments

Comments
 (0)