Skip to content

Commit 122bd8a

Browse files
authored
minor #1450 housekeeping - add rector rules - one step closer to static analysis
1 parent c953816 commit 122bd8a

20 files changed

+64
-125
lines changed

src/Command/MakerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
8686
continue;
8787
}
8888

89-
$value = $this->io->ask($argument->getDescription(), $argument->getDefault(), [Validator::class, 'notBlank']);
89+
$value = $this->io->ask($argument->getDescription(), $argument->getDefault(), Validator::notBlank(...));
9090
$input->setArgument($argument->getName(), $value);
9191
}
9292

src/Docker/DockerDatabaseServices.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function getMissingExtensionName(string $name): ?string
9999
/**
100100
* @throws RuntimeCommandException
101101
*/
102-
private static function throwInvalidDatabase(string $name): void
102+
private static function throwInvalidDatabase(string $name): never
103103
{
104104
throw new RuntimeCommandException(sprintf('%s is not a valid / supported docker database type.', $name));
105105
}

src/Doctrine/EntityRegenerator.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,13 @@ public function regenerateEntities(string $classOrNamespace): void
116116
continue;
117117
}
118118

119-
switch ($mapping['type']) {
120-
case ClassMetadata::MANY_TO_ONE:
121-
$manipulator->addManyToOneRelation(RelationManyToOne::createFromObject($mapping));
122-
123-
break;
124-
case ClassMetadata::ONE_TO_MANY:
125-
$manipulator->addOneToManyRelation(RelationOneToMany::createFromObject($mapping));
126-
127-
break;
128-
case ClassMetadata::MANY_TO_MANY:
129-
$manipulator->addManyToManyRelation(RelationManyToMany::createFromObject($mapping));
130-
131-
break;
132-
case ClassMetadata::ONE_TO_ONE:
133-
$manipulator->addOneToOneRelation(RelationOneToOne::createFromObject($mapping));
134-
135-
break;
136-
default:
137-
throw new \Exception('Unknown association type.');
138-
}
119+
match ($mapping['type']) {
120+
ClassMetadata::MANY_TO_ONE => $manipulator->addManyToOneRelation(RelationManyToOne::createFromObject($mapping)),
121+
ClassMetadata::ONE_TO_MANY => $manipulator->addOneToManyRelation(RelationOneToMany::createFromObject($mapping)),
122+
ClassMetadata::MANY_TO_MANY => $manipulator->addManyToManyRelation(RelationManyToMany::createFromObject($mapping)),
123+
ClassMetadata::ONE_TO_ONE => $manipulator->addOneToOneRelation(RelationOneToOne::createFromObject($mapping)),
124+
default => throw new \Exception('Unknown association type.'),
125+
};
139126
}
140127
}
141128

src/Generator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ class Generator
2525
{
2626
private GeneratorTwigHelper $twigHelper;
2727
private array $pendingOperations = [];
28-
private ?TemplateComponentGenerator $templateComponentGenerator;
2928
private array $generatedFiles = [];
3029

3130
public function __construct(
3231
private FileManager $fileManager,
3332
private string $namespacePrefix,
3433
?PhpCompatUtil $phpCompatUtil = null,
35-
?TemplateComponentGenerator $templateComponentGenerator = null,
34+
private ?TemplateComponentGenerator $templateComponentGenerator = null,
3635
) {
3736
$this->twigHelper = new GeneratorTwigHelper($fileManager);
3837
$this->namespacePrefix = trim($namespacePrefix, '\\');
3938

4039
if (null !== $phpCompatUtil) {
4140
trigger_deprecation('symfony/maker-bundle', 'v1.44.0', 'Initializing Generator while providing an instance of PhpCompatUtil is deprecated.');
4241
}
43-
44-
$this->templateComponentGenerator = $templateComponentGenerator;
4542
}
4643

4744
/**
@@ -152,7 +149,7 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st
152149
try {
153150
Validator::classDoesNotExist($className);
154151
$className = rtrim($fullNamespacePrefix, '\\').'\\'.$className;
155-
} catch (RuntimeCommandException $e) {
152+
} catch (RuntimeCommandException) {
156153
}
157154
}
158155

src/GeneratorTwigHelper.php

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,20 @@ public function __construct(
2323

2424
public function getEntityFieldPrintCode($entity, $field): string
2525
{
26-
$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', static function ($s) {
27-
return strtoupper($s[1]);
28-
}, $field['fieldName']);
26+
$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', static fn ($s) => strtoupper($s[1]), $field['fieldName']);
2927
$printCode = $entity.'.'.str_replace('_', '', $twigField);
3028

31-
switch ($field['type']) {
32-
case 'datetimetz_immutable':
33-
case 'datetimetz':
34-
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s T\') : \'\'';
35-
break;
36-
case 'datetime_immutable':
37-
case 'datetime':
38-
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s\') : \'\'';
39-
break;
40-
case 'dateinterval':
41-
$printCode .= ' ? '.$printCode.'.format(\'%y year(s), %m month(s), %d day(s)\') : \'\'';
42-
break;
43-
case 'date_immutable':
44-
case 'date':
45-
$printCode .= ' ? '.$printCode.'|date(\'Y-m-d\') : \'\'';
46-
break;
47-
case 'time_immutable':
48-
case 'time':
49-
$printCode .= ' ? '.$printCode.'|date(\'H:i:s\') : \'\'';
50-
break;
51-
case 'json':
52-
$printCode .= ' ? '.$printCode.'|json_encode : \'\'';
53-
break;
54-
case 'array':
55-
$printCode .= ' ? '.$printCode.'|join(\', \') : \'\'';
56-
break;
57-
case 'boolean':
58-
$printCode .= ' ? \'Yes\' : \'No\'';
59-
break;
60-
}
29+
match ($field['type']) {
30+
'datetimetz_immutable', 'datetimetz' => $printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s T\') : \'\'',
31+
'datetime_immutable', 'datetime' => $printCode .= ' ? '.$printCode.'|date(\'Y-m-d H:i:s\') : \'\'',
32+
'dateinterval' => $printCode .= ' ? '.$printCode.'.format(\'%y year(s), %m month(s), %d day(s)\') : \'\'',
33+
'date_immutable', 'date' => $printCode .= ' ? '.$printCode.'|date(\'Y-m-d\') : \'\'',
34+
'time_immutable', 'time' => $printCode .= ' ? '.$printCode.'|date(\'H:i:s\') : \'\'',
35+
'json' => $printCode .= ' ? '.$printCode.'|json_encode : \'\'',
36+
'array' => $printCode .= ' ? '.$printCode.'|join(\', \') : \'\'',
37+
'boolean' => $printCode .= ' ? \'Yes\' : \'No\'',
38+
default => $printCode,
39+
};
6140

6241
return $printCode;
6342
}

src/Maker/MakeAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function ($answer) {
157157
$io->ask(
158158
'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)',
159159
'SecurityController',
160-
[Validator::class, 'validateClassName']
160+
Validator::validateClassName(...)
161161
)
162162
);
163163

src/Maker/MakeDockerDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
102102

103103
$io->text($serviceNameMsg);
104104

105-
$this->serviceName = $io->ask(sprintf('What name should we call the new %s service? (e.g. <fg=yellow>database</>)', $this->serviceName), null, [Validator::class, 'notBlank']);
105+
$this->serviceName = $io->ask(sprintf('What name should we call the new %s service? (e.g. <fg=yellow>database</>)', $this->serviceName), null, Validator::notBlank(...));
106106
}
107107

108108
$this->checkForPDOSupport($this->databaseChoice, $io);

src/Maker/MakeEntity.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
111111
'This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.',
112112
'To overwrite any existing methods, re-run this command with the --overwrite flag',
113113
], null, 'fg=yellow');
114-
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', $this->getEntityNamespace(), [Validator::class, 'notBlank']);
114+
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', $this->getEntityNamespace(), Validator::notBlank(...));
115115

116116
$input->setArgument('name', $classOrNamespace);
117117

@@ -407,13 +407,13 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
407407

408408
if ('string' === $type) {
409409
// default to 255, avoid the question
410-
$classProperty->length = $io->ask('Field length', 255, [Validator::class, 'validateLength']);
410+
$classProperty->length = $io->ask('Field length', 255, Validator::validateLength(...));
411411
} elseif ('decimal' === $type) {
412412
// 10 is the default value given in \Doctrine\DBAL\Schema\Column::$_precision
413-
$classProperty->precision = $io->ask('Precision (total number of digits stored: 100.00 would be 5)', 10, [Validator::class, 'validatePrecision']);
413+
$classProperty->precision = $io->ask('Precision (total number of digits stored: 100.00 would be 5)', 10, Validator::validatePrecision(...));
414414

415415
// 0 is the default value given in \Doctrine\DBAL\Schema\Column::$_scale
416-
$classProperty->scale = $io->ask('Scale (number of decimals to store: 100.00 would be 2)', 0, [Validator::class, 'validateScale']);
416+
$classProperty->scale = $io->ask('Scale (number of decimals to store: 100.00 would be 2)', 0, Validator::validateScale(...));
417417
}
418418

419419
if ($io->confirm('Can this field be null in the database (nullable)', false)) {
@@ -525,7 +525,7 @@ private function printAvailableTypes(ConsoleStyle $io): void
525525
private function createEntityClassQuestion(string $questionText): Question
526526
{
527527
$question = new Question($questionText);
528-
$question->setValidator([Validator::class, 'notBlank']);
528+
$question->setValidator(Validator::notBlank(...));
529529
$question->setAutocompleterValues($this->doctrineHelper->getEntitiesForAutocomplete());
530530

531531
return $question;

src/Maker/MakeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
9898
$io->listing($this->eventRegistry->listActiveEvents($events));
9999
$question = new Question(sprintf(' <fg=green>%s</>', $command->getDefinition()->getArgument('event')->getDescription()));
100100
$question->setAutocompleterValues($events);
101-
$question->setValidator([Validator::class, 'notBlank']);
101+
$question->setValidator(Validator::notBlank(...));
102102
$event = $io->askQuestion($question);
103103
$input->setArgument('event', $event);
104104
}

src/Maker/MakeRegistrationForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
158158
$this->fromEmailAddress = $io->ask(
159159
'What email address will be used to send registration confirmations? (e.g. <fg=yellow>mailer@your-domain.com</>)',
160160
null,
161-
[Validator::class, 'validateEmailAddress']
161+
Validator::validateEmailAddress(...)
162162
);
163163

164164
$this->fromEmailName = $io->ask(
165165
'What "name" should be associated with that email address? (e.g. <fg=yellow>Acme Mail Bot</>)',
166166
null,
167-
[Validator::class, 'notBlank']
167+
Validator::notBlank(...)
168168
);
169169
}
170170

0 commit comments

Comments
 (0)