diff --git a/src/DependencyBuilder.php b/src/DependencyBuilder.php index bab704514b..a15fc4d928 100644 --- a/src/DependencyBuilder.php +++ b/src/DependencyBuilder.php @@ -139,6 +139,6 @@ private function calculateMissingDependencies(array $dependencies): array return []; } - return array_unique(array_merge($missingPackages, $missingOptionalPackages)); + return array_unique([...$missingPackages, ...$missingOptionalPackages]); } } diff --git a/src/Docker/DockerDatabaseServices.php b/src/Docker/DockerDatabaseServices.php index 29f1aac807..e0548de31c 100644 --- a/src/Docker/DockerDatabaseServices.php +++ b/src/Docker/DockerDatabaseServices.php @@ -83,17 +83,11 @@ public static function getSuggestedServiceVersion(string $name): string public static function getMissingExtensionName(string $name): ?string { - switch ($name) { - case 'mariadb': - case 'mysql': - $driver = 'mysql'; - break; - case 'postgres': - $driver = 'pgsql'; - break; - default: - self::throwInvalidDatabase($name); - } + $driver = match ($name) { + 'mariadb', 'mysql' => 'mysql', + 'postgres' => 'pgsql', + default => self::throwInvalidDatabase($name), + }; if (!\in_array($driver, \PDO::getAvailableDrivers(), true)) { return $driver; diff --git a/src/Doctrine/DoctrineHelper.php b/src/Doctrine/DoctrineHelper.php index 551053f797..290e69047d 100644 --- a/src/Doctrine/DoctrineHelper.php +++ b/src/Doctrine/DoctrineHelper.php @@ -68,7 +68,7 @@ public function doesClassUseDriver(string $className, string $driverClass): bool try { /** @var EntityManagerInterface $em */ $em = $this->getRegistry()->getManagerForClass($className); - } catch (\ReflectionException $exception) { + } catch (\ReflectionException) { // this exception will be thrown by the registry if the class isn't created yet. // an example case is the "make:entity" command, which needs to know which driver is used for the class to determine // 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 if ($disconnected) { try { $loaded = $cmf->getAllMetadata(); - } catch (ORMMappingException|PersistenceMappingException $e) { + } catch (ORMMappingException|PersistenceMappingException) { $loaded = $this->isInstanceOf($cmf, AbstractClassMetadataFactory::class) ? $cmf->getLoadedMetadata() : []; } diff --git a/src/Doctrine/EntityDetails.php b/src/Doctrine/EntityDetails.php index 73d07b1088..d0aa63e292 100644 --- a/src/Doctrine/EntityDetails.php +++ b/src/Doctrine/EntityDetails.php @@ -52,9 +52,7 @@ public function getFormFields(): array if (!empty($this->metadata->embeddedClasses)) { foreach (array_keys($this->metadata->embeddedClasses) as $embeddedClassKey) { - $fields = array_filter($fields, function ($v) use ($embeddedClassKey) { - return 0 !== strpos($v, $embeddedClassKey.'.'); - }); + $fields = array_filter($fields, static fn ($v) => !str_starts_with($v, $embeddedClassKey.'.')); } } diff --git a/src/Doctrine/EntityRegenerator.php b/src/Doctrine/EntityRegenerator.php index e8f5e88b45..6124d90c82 100644 --- a/src/Doctrine/EntityRegenerator.php +++ b/src/Doctrine/EntityRegenerator.php @@ -38,7 +38,7 @@ public function regenerateEntities(string $classOrNamespace): void { try { $metadata = $this->doctrineHelper->getMetadata($classOrNamespace); - } catch (MappingException|LegacyCommonMappingException|PersistenceMappingException $mappingException) { + } catch (MappingException|LegacyCommonMappingException|PersistenceMappingException) { $metadata = $this->doctrineHelper->getMetadata($classOrNamespace, true); } @@ -72,7 +72,7 @@ public function regenerateEntities(string $classOrNamespace): void $embeddedClasses = []; foreach ($classMetadata->embeddedClasses as $fieldName => $mapping) { - if (false !== strpos($fieldName, '.')) { + if (str_contains($fieldName, '.')) { continue; } @@ -91,7 +91,7 @@ public function regenerateEntities(string $classOrNamespace): void foreach ($classMetadata->fieldMappings as $fieldName => $mapping) { // skip embedded fields - if (false !== strpos($fieldName, '.')) { + if (str_contains($fieldName, '.')) { [$fieldName, $embeddedFiledName] = explode('.', $fieldName); $operations[$embeddedClasses[$fieldName]]->addEntityField($embeddedFiledName, $mapping); @@ -239,11 +239,11 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array /** @var \ReflectionClass $classReflection */ $classReflection = $classMetadata->reflClass; - $targetFields = array_merge( - array_keys($classMetadata->fieldMappings), - array_keys($classMetadata->associationMappings), - array_keys($classMetadata->embeddedClasses) - ); + $targetFields = [ + ...array_keys($classMetadata->fieldMappings), + ...array_keys($classMetadata->associationMappings), + ...array_keys($classMetadata->embeddedClasses), + ]; if ($classReflection) { // exclude traits @@ -258,10 +258,8 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata): array $targetFields = array_diff($targetFields, $traitProperties); // exclude inherited properties - $targetFields = array_filter($targetFields, static function ($field) use ($classReflection) { - return $classReflection->hasProperty($field) && - $classReflection->getProperty($field)->getDeclaringClass()->getName() == $classReflection->getName(); - }); + $targetFields = array_filter($targetFields, static fn ($field) => $classReflection->hasProperty($field) && + $classReflection->getProperty($field)->getDeclaringClass()->getName() === $classReflection->getName()); } return $targetFields; diff --git a/src/Maker/AbstractMaker.php b/src/Maker/AbstractMaker.php index 3d5a848414..50d8fcf6e1 100644 --- a/src/Maker/AbstractMaker.php +++ b/src/Maker/AbstractMaker.php @@ -22,10 +22,16 @@ */ abstract class AbstractMaker implements MakerInterface { + /** + * @return void + */ public function interact(InputInterface $input, ConsoleStyle $io, Command $command) { } + /** + * @return void + */ protected function writeSuccessMessage(ConsoleStyle $io) { $io->newLine(); @@ -44,7 +50,7 @@ protected function addDependencies(array $dependencies, string $message = null): } return $dependencyBuilder->getMissingPackagesMessage( - $this->getCommandName(), + static::getCommandName(), $message ); } diff --git a/src/Maker/MakeAuthenticator.php b/src/Maker/MakeAuthenticator.php index f0c68413dc..c21ddc08af 100644 --- a/src/Maker/MakeAuthenticator.php +++ b/src/Maker/MakeAuthenticator.php @@ -218,7 +218,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen ); $generator->dumpFile($path, $newYaml); $securityYamlUpdated = true; - } catch (YamlManipulationFailedException $e) { + } catch (YamlManipulationFailedException) { } if (self::AUTH_TYPE_FORM_LOGIN === $input->getArgument('authenticator-type')) { diff --git a/src/Maker/MakeCommand.php b/src/Maker/MakeCommand.php index 1773df4aaa..36e24b4921 100644 --- a/src/Maker/MakeCommand.php +++ b/src/Maker/MakeCommand.php @@ -58,7 +58,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { $commandName = trim($input->getArgument('name')); - $commandNameHasAppPrefix = 0 === strpos($commandName, 'app:'); + $commandNameHasAppPrefix = str_starts_with($commandName, 'app:'); $commandClassNameDetails = $generator->createClassNameDetails( $commandNameHasAppPrefix ? substr($commandName, 4) : $commandName, diff --git a/src/Maker/MakeDockerDatabase.php b/src/Maker/MakeDockerDatabase.php index 2c32c857df..624b4c9bc4 100644 --- a/src/Maker/MakeDockerDatabase.php +++ b/src/Maker/MakeDockerDatabase.php @@ -30,32 +30,27 @@ */ final class MakeDockerDatabase extends AbstractMaker { - private $fileManager; private $composeFilePath; - /** - * @var ComposeFileManipulator - */ - private $composeFileManipulator; + private ?ComposeFileManipulator $composeFileManipulator = null; /** * @var string type of database selected by the user */ - private $databaseChoice; + private ?string $databaseChoice = null; /** * @var string Service identifier to be set in docker-compose.yaml */ - private $serviceName = 'database'; + private string $serviceName = 'database'; /** * @var string Version set in docker-compose.yaml for the service. e.g. latest */ - private $serviceVersion = 'latest'; + private string $serviceVersion = 'latest'; - public function __construct(FileManager $fileManager) + public function __construct(private FileManager $fileManager) { - $this->fileManager = $fileManager; } public static function getCommandName(): string @@ -77,6 +72,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void { + $serviceNameMsg = []; $io->section('- Docker Compose Setup-'); $this->composeFileManipulator = new ComposeFileManipulator($this->getComposeFileContents($io)); @@ -116,6 +112,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { + $closing = []; $io->newLine(); $service = DockerDatabaseServices::getDatabaseSkeleton($this->databaseChoice, $this->serviceVersion); diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 63116832de..7a0c57bc9f 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -369,9 +369,9 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity $defaultType = 'datetime_immutable'; } elseif ('_id' === $suffix) { $defaultType = 'integer'; - } elseif (0 === strpos($snakeCasedField, 'is_')) { + } elseif (str_starts_with($snakeCasedField, 'is_')) { $defaultType = 'boolean'; - } elseif (0 === strpos($snakeCasedField, 'has_')) { + } elseif (str_starts_with($snakeCasedField, 'has_')) { $defaultType = 'boolean'; } elseif ('uuid' === $snakeCasedField) { $defaultType = 'uuid'; @@ -479,9 +479,9 @@ private function printAvailableTypes(ConsoleStyle $io): void if (\is_string($subTypes) && $subTypes) { $line .= sprintf(' (%s)', $subTypes); } elseif (\is_array($subTypes) && !empty($subTypes)) { - $line .= sprintf(' (or %s)', implode(', ', array_map(function ($subType) { - return sprintf('%s', $subType); - }, $subTypes))); + $line .= sprintf(' (or %s)', implode(', ', array_map( + static fn ($subType) => sprintf('%s', $subType), $subTypes)) + ); foreach ($subTypes as $subType) { unset($allTypes[$subType]); @@ -508,9 +508,7 @@ private function printAvailableTypes(ConsoleStyle $io): void $io->writeln('Other Types'); // empty the values - $allTypes = array_map(function () { - return []; - }, $allTypes); + $allTypes = array_map(static fn () => [], $allTypes); $printSection($allTypes); } @@ -551,31 +549,27 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla $type = $this->askRelationType($io, $generatedEntityClass, $targetEntityClass); } - $askFieldName = function (string $targetClass, string $defaultValue) use ($io) { - return $io->ask( - sprintf('New field name inside %s', Str::getShortClassName($targetClass)), - $defaultValue, - function ($name) use ($targetClass) { - // it's still *possible* to create duplicate properties - by - // trying to generate the same property 2 times during the - // same make:entity run. property_exists() only knows about - // properties that *originally* existed on this class. - if (property_exists($targetClass, $name)) { - throw new \InvalidArgumentException(sprintf('The "%s" class already has a "%s" property.', $targetClass, $name)); - } - - return Validator::validateDoctrineFieldName($name, $this->doctrineHelper->getRegistry()); + $askFieldName = fn (string $targetClass, string $defaultValue) => $io->ask( + sprintf('New field name inside %s', Str::getShortClassName($targetClass)), + $defaultValue, + function ($name) use ($targetClass) { + // it's still *possible* to create duplicate properties - by + // trying to generate the same property 2 times during the + // same make:entity run. property_exists() only knows about + // properties that *originally* existed on this class. + if (property_exists($targetClass, $name)) { + throw new \InvalidArgumentException(sprintf('The "%s" class already has a "%s" property.', $targetClass, $name)); } - ); - }; - $askIsNullable = static function (string $propertyName, string $targetClass) use ($io) { - return $io->confirm(sprintf( - 'Is the %s.%s property allowed to be null (nullable)?', - Str::getShortClassName($targetClass), - $propertyName - )); - }; + return Validator::validateDoctrineFieldName($name, $this->doctrineHelper->getRegistry()); + } + ); + + $askIsNullable = static fn (string $propertyName, string $targetClass) => $io->confirm(sprintf( + 'Is the %s.%s property allowed to be null (nullable)?', + Str::getShortClassName($targetClass), + $propertyName + )); $askOrphanRemoval = static function (string $owningClass, string $inverseClass) use ($io) { $io->text([ @@ -837,9 +831,7 @@ private function getPropertyNames(string $class): array $reflClass = new \ReflectionClass($class); - return array_map(static function (\ReflectionProperty $prop) { - return $prop->getName(); - }, $reflClass->getProperties()); + return array_map(static fn (\ReflectionProperty $prop) => $prop->getName(), $reflClass->getProperties()); } /** @legacy Drop when Annotations are no longer supported */ diff --git a/src/Maker/MakeForm.php b/src/Maker/MakeForm.php index 4c93a35274..d5ee7ec84e 100644 --- a/src/Maker/MakeForm.php +++ b/src/Maker/MakeForm.php @@ -67,7 +67,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma $entities = $this->entityHelper->getEntitiesForAutocomplete(); $question = new Question($argument->getDescription()); - $question->setValidator(function ($answer) use ($entities) {return Validator::existsOrNull($answer, $entities); }); + $question->setValidator(fn ($answer) => Validator::existsOrNull($answer, $entities)); $question->setAutocompleterValues($entities); $question->setMaxAttempts(3); diff --git a/src/Maker/MakeMessage.php b/src/Maker/MakeMessage.php index feaabdc55b..1a2442014e 100644 --- a/src/Maker/MakeMessage.php +++ b/src/Maker/MakeMessage.php @@ -58,13 +58,15 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma { $command->addArgument('chosen-transport', InputArgument::OPTIONAL); + $messengerData = []; + try { $manipulator = new YamlSourceManipulator($this->fileManager->getFileContents('config/packages/messenger.yaml')); $messengerData = $manipulator->getData(); - } catch (\Exception $e) { + } catch (\Exception) { } - if (!isset($messengerData, $messengerData['framework']['messenger']['transports'])) { + if (!isset($messengerData['framework']['messenger']['transports'])) { return; } diff --git a/src/Maker/MakeMigration.php b/src/Maker/MakeMigration.php index e52d8ed151..4640d21a17 100644 --- a/src/Maker/MakeMigration.php +++ b/src/Maker/MakeMigration.php @@ -104,12 +104,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $migrationOutput = $commandOutput->fetch(); - if (false !== strpos($migrationOutput, 'No changes detected')) { + if (str_contains($migrationOutput, 'No changes detected')) { $this->noChangesMessage($io); return; } - } catch (\Doctrine\Migrations\Generator\Exception\NoChangesDetected $exception) { + } catch (\Doctrine\Migrations\Generator\Exception\NoChangesDetected) { $this->noChangesMessage($io); return; diff --git a/src/Maker/MakeRegistrationForm.php b/src/Maker/MakeRegistrationForm.php index 7d1e8c2b6b..3383fbca9e 100644 --- a/src/Maker/MakeRegistrationForm.php +++ b/src/Maker/MakeRegistrationForm.php @@ -70,8 +70,8 @@ final class MakeRegistrationForm extends AbstractMaker private $userClass; private $usernameField; private $passwordField; - private $willVerifyEmail = false; - private $verifyEmailAnonymously = false; + private bool $willVerifyEmail = false; + private bool $verifyEmailAnonymously = false; private $idGetter; private $emailGetter; private $fromEmailAddress; @@ -80,7 +80,7 @@ final class MakeRegistrationForm extends AbstractMaker private $firewallName; private $redirectRouteName; private $addUniqueEntityConstraint; - private $useNewAuthenticatorSystem = false; + private bool $useNewAuthenticatorSystem = false; public function __construct( private FileManager $fileManager, @@ -109,6 +109,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void { + $emailText = []; $interactiveSecurityHelper = new InteractiveSecurityHelper(); if (!$this->fileManager->fileExists($path = 'config/packages/security.yaml')) { @@ -228,7 +229,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $userDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($userClassNameDetails->getFullName()); $userRepoVars = [ - 'repository_full_class_name' => 'Doctrine\ORM\EntityManagerInterface', + 'repository_full_class_name' => EntityManagerInterface::class, 'repository_class_name' => 'EntityManagerInterface', 'repository_var' => '$manager', ]; @@ -425,6 +426,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen private function successMessage(ConsoleStyle $io, bool $emailVerification, string $userClass): void { + $closing = []; $closing[] = 'Next:'; if (!$emailVerification) { diff --git a/src/Maker/MakeResetPassword.php b/src/Maker/MakeResetPassword.php index 9363d9ff80..56e26fb7e9 100644 --- a/src/Maker/MakeResetPassword.php +++ b/src/Maker/MakeResetPassword.php @@ -131,6 +131,7 @@ public function configureDependencies(DependencyBuilder $dependencies): void public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void { + $emailText = []; $io->title('Let\'s make a password reset feature!'); $interactiveSecurityHelper = new InteractiveSecurityHelper(); @@ -356,7 +357,7 @@ private function setBundleConfig(ConsoleStyle $io, Generator $generator, string * reset_password.yaml exists, and was probably created by flex; * Let's replace it with a "clean" file. */ - if (1 >= \count($data[$symfonyCastsKey])) { + if (1 >= (is_countable($data[$symfonyCastsKey]) ? \count($data[$symfonyCastsKey]) : 0)) { $yaml = [ $symfonyCastsKey => [ 'request_password_repository' => $repositoryClassFullName, @@ -382,6 +383,7 @@ private function setBundleConfig(ConsoleStyle $io, Generator $generator, string private function successMessage(InputInterface $input, ConsoleStyle $io, string $requestClassName): void { + $closing = []; $closing[] = 'Next:'; $closing[] = sprintf(' 1) Run "php bin/console make:migration" to generate a migration for the new "%s" entity.', $requestClassName); $closing[] = ' 2) Review forms in "src/Form" to customize validation and labels.'; diff --git a/src/Maker/MakeSerializerNormalizer.php b/src/Maker/MakeSerializerNormalizer.php index 65b9186ca1..1299017858 100644 --- a/src/Maker/MakeSerializerNormalizer.php +++ b/src/Maker/MakeSerializerNormalizer.php @@ -52,7 +52,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $normalizerClassNameDetails = $generator->createClassNameDetails( $input->getArgument('name'), 'Serializer\\Normalizer\\', - 'Normalizer' + \Normalizer::class ); $useStatements = new UseStatementGenerator([ diff --git a/src/Maker/MakeStimulusController.php b/src/Maker/MakeStimulusController.php index 2e15d38581..029d6c7700 100644 --- a/src/Maker/MakeStimulusController.php +++ b/src/Maker/MakeStimulusController.php @@ -176,9 +176,9 @@ private function askForNextValue(ConsoleStyle $io, array $values, bool $isFirstV if ('_id' === $suffix = substr($snakeCasedField, -3)) { $defaultType = 'Number'; - } elseif (0 === strpos($snakeCasedField, 'is_')) { + } elseif (str_starts_with($snakeCasedField, 'is_')) { $defaultType = 'Boolean'; - } elseif (0 === strpos($snakeCasedField, 'has_')) { + } elseif (str_starts_with($snakeCasedField, 'has_')) { $defaultType = 'Boolean'; } diff --git a/src/Maker/MakeUser.php b/src/Maker/MakeUser.php index d75f707f5c..a778b0b944 100644 --- a/src/Maker/MakeUser.php +++ b/src/Maker/MakeUser.php @@ -114,6 +114,7 @@ class_exists(DoctrineBundle::class) public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { + $customProviderPath = null; $userClassConfiguration = new UserClassConfiguration( $input->getOption('is-entity'), $input->getOption('identity-property-name'), @@ -190,7 +191,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen ); $generator->dumpFile($path, $newYaml); $securityYamlUpdated = true; - } catch (YamlManipulationFailedException $e) { + } catch (YamlManipulationFailedException) { } } @@ -225,9 +226,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $nextSteps[] = 'Create a way to authenticate! See https://symfony.com/doc/current/security.html'; - $nextSteps = array_map(function ($step) { - return sprintf(' - %s', $step); - }, $nextSteps); + $nextSteps = array_map(static fn ($step) => sprintf(' - %s', $step), $nextSteps); $io->text($nextSteps); }