Skip to content

Commit 78cba1f

Browse files
committed
Use ::class instead of FQCN
1 parent f41c7b4 commit 78cba1f

File tree

4 files changed

+85
-51
lines changed

4 files changed

+85
-51
lines changed

Extension/Core/Type/FileType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\FormEvents;
1919
use Symfony\Component\Form\FormInterface;
2020
use Symfony\Component\Form\FormView;
21+
use Symfony\Component\HttpFoundation\File\File;
2122
use Symfony\Component\OptionsResolver\Options;
2223
use Symfony\Component\OptionsResolver\OptionsResolver;
2324
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -114,9 +115,9 @@ public function finishView(FormView $view, FormInterface $form, array $options)
114115
public function configureOptions(OptionsResolver $resolver)
115116
{
116117
$dataClass = null;
117-
if (class_exists(\Symfony\Component\HttpFoundation\File\File::class)) {
118+
if (class_exists(File::class)) {
118119
$dataClass = function (Options $options) {
119-
return $options['multiple'] ? null : 'Symfony\Component\HttpFoundation\File\File';
120+
return $options['multiple'] ? null : File::class;
120121
};
121122
}
122123

Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(ServerParams $serverParams = null)
4141
public function handleRequest(FormInterface $form, mixed $request = null)
4242
{
4343
if (!$request instanceof Request) {
44-
throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
44+
throw new UnexpectedTypeException($request, Request::class);
4545
}
4646

4747
$name = $form->getName();

Extension/Validator/ValidatorExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(ValidatorInterface $validator, bool $legacyErrorMess
3636
{
3737
$this->legacyErrorMessages = $legacyErrorMessages;
3838

39-
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');
39+
$metadata = $validator->getMetadataFor(\Symfony\Component\Form\Form::class);
4040

4141
// Register the form constraints in the validator programmatically.
4242
// This functionality is required when using the Form component without

Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,47 @@
1111

1212
namespace Symfony\Component\Form\Extension\Validator;
1313

14+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
15+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
16+
use Symfony\Component\Form\Extension\Core\Type\CountryType;
17+
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
18+
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
19+
use Symfony\Component\Form\Extension\Core\Type\DateType;
20+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
21+
use Symfony\Component\Form\Extension\Core\Type\FileType;
22+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
23+
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
24+
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
25+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
26+
use Symfony\Component\Form\Extension\Core\Type\TextType;
27+
use Symfony\Component\Form\Extension\Core\Type\TimeType;
28+
use Symfony\Component\Form\Extension\Core\Type\UrlType;
1429
use Symfony\Component\Form\FormTypeGuesserInterface;
1530
use Symfony\Component\Form\Guess\Guess;
1631
use Symfony\Component\Form\Guess\TypeGuess;
1732
use Symfony\Component\Form\Guess\ValueGuess;
1833
use Symfony\Component\Validator\Constraint;
34+
use Symfony\Component\Validator\Constraints\Count;
35+
use Symfony\Component\Validator\Constraints\Country;
36+
use Symfony\Component\Validator\Constraints\Currency;
37+
use Symfony\Component\Validator\Constraints\Date;
38+
use Symfony\Component\Validator\Constraints\DateTime;
39+
use Symfony\Component\Validator\Constraints\Email;
40+
use Symfony\Component\Validator\Constraints\File;
41+
use Symfony\Component\Validator\Constraints\Image;
42+
use Symfony\Component\Validator\Constraints\Ip;
43+
use Symfony\Component\Validator\Constraints\IsFalse;
1944
use Symfony\Component\Validator\Constraints\IsTrue;
45+
use Symfony\Component\Validator\Constraints\Language;
46+
use Symfony\Component\Validator\Constraints\Length;
47+
use Symfony\Component\Validator\Constraints\Locale;
2048
use Symfony\Component\Validator\Constraints\NotBlank;
2149
use Symfony\Component\Validator\Constraints\NotNull;
50+
use Symfony\Component\Validator\Constraints\Range;
51+
use Symfony\Component\Validator\Constraints\Regex;
52+
use Symfony\Component\Validator\Constraints\Time;
53+
use Symfony\Component\Validator\Constraints\Type;
54+
use Symfony\Component\Validator\Constraints\Url;
2255
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
2356
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2457

@@ -79,86 +112,86 @@ public function guessPattern(string $class, string $property): ?ValueGuess
79112
public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess
80113
{
81114
switch (\get_class($constraint)) {
82-
case 'Symfony\Component\Validator\Constraints\Type':
115+
case Type::class:
83116
switch ($constraint->type) {
84117
case 'array':
85-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
118+
return new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE);
86119
case 'boolean':
87120
case 'bool':
88-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::MEDIUM_CONFIDENCE);
121+
return new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE);
89122

90123
case 'double':
91124
case 'float':
92125
case 'numeric':
93126
case 'real':
94-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
127+
return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE);
95128

96129
case 'integer':
97130
case 'int':
98131
case 'long':
99-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
132+
return new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE);
100133

101134
case \DateTime::class:
102135
case '\DateTime':
103-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::MEDIUM_CONFIDENCE);
136+
return new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE);
104137

105138
case 'string':
106-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
139+
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
107140
}
108141
break;
109142

110-
case 'Symfony\Component\Validator\Constraints\Country':
111-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CountryType', [], Guess::HIGH_CONFIDENCE);
143+
case Country::class:
144+
return new TypeGuess(CountryType::class, [], Guess::HIGH_CONFIDENCE);
112145

113-
case 'Symfony\Component\Validator\Constraints\Currency':
114-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CurrencyType', [], Guess::HIGH_CONFIDENCE);
146+
case Currency::class:
147+
return new TypeGuess(CurrencyType::class, [], Guess::HIGH_CONFIDENCE);
115148

116-
case 'Symfony\Component\Validator\Constraints\Date':
117-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'string'], Guess::HIGH_CONFIDENCE);
149+
case Date::class:
150+
return new TypeGuess(DateType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
118151

119-
case 'Symfony\Component\Validator\Constraints\DateTime':
120-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'string'], Guess::HIGH_CONFIDENCE);
152+
case DateTime::class:
153+
return new TypeGuess(DateTimeType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
121154

122-
case 'Symfony\Component\Validator\Constraints\Email':
123-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\EmailType', [], Guess::HIGH_CONFIDENCE);
155+
case Email::class:
156+
return new TypeGuess(EmailType::class, [], Guess::HIGH_CONFIDENCE);
124157

125-
case 'Symfony\Component\Validator\Constraints\File':
126-
case 'Symfony\Component\Validator\Constraints\Image':
158+
case File::class:
159+
case Image::class:
127160
$options = [];
128161
if ($constraint->mimeTypes) {
129162
$options = ['attr' => ['accept' => implode(',', (array) $constraint->mimeTypes)]];
130163
}
131164

132-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\FileType', $options, Guess::HIGH_CONFIDENCE);
165+
return new TypeGuess(FileType::class, $options, Guess::HIGH_CONFIDENCE);
133166

134-
case 'Symfony\Component\Validator\Constraints\Language':
135-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\LanguageType', [], Guess::HIGH_CONFIDENCE);
167+
case Language::class:
168+
return new TypeGuess(LanguageType::class, [], Guess::HIGH_CONFIDENCE);
136169

137-
case 'Symfony\Component\Validator\Constraints\Locale':
138-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\LocaleType', [], Guess::HIGH_CONFIDENCE);
170+
case Locale::class:
171+
return new TypeGuess(LocaleType::class, [], Guess::HIGH_CONFIDENCE);
139172

140-
case 'Symfony\Component\Validator\Constraints\Time':
141-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'string'], Guess::HIGH_CONFIDENCE);
173+
case Time::class:
174+
return new TypeGuess(TimeType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
142175

143-
case 'Symfony\Component\Validator\Constraints\Url':
144-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\UrlType', [], Guess::HIGH_CONFIDENCE);
176+
case Url::class:
177+
return new TypeGuess(UrlType::class, [], Guess::HIGH_CONFIDENCE);
145178

146-
case 'Symfony\Component\Validator\Constraints\Ip':
147-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
179+
case Ip::class:
180+
return new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE);
148181

149-
case 'Symfony\Component\Validator\Constraints\Length':
150-
case 'Symfony\Component\Validator\Constraints\Regex':
151-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
182+
case Length::class:
183+
case Regex::class:
184+
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
152185

153-
case 'Symfony\Component\Validator\Constraints\Range':
154-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::LOW_CONFIDENCE);
186+
case Range::class:
187+
return new TypeGuess(NumberType::class, [], Guess::LOW_CONFIDENCE);
155188

156-
case 'Symfony\Component\Validator\Constraints\Count':
157-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::LOW_CONFIDENCE);
189+
case Count::class:
190+
return new TypeGuess(CollectionType::class, [], Guess::LOW_CONFIDENCE);
158191

159-
case 'Symfony\Component\Validator\Constraints\IsTrue':
160-
case 'Symfony\Component\Validator\Constraints\IsFalse':
161-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::MEDIUM_CONFIDENCE);
192+
case IsTrue::class:
193+
case IsFalse::class:
194+
return new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE);
162195
}
163196

164197
return null;
@@ -183,19 +216,19 @@ public function guessRequiredForConstraint(Constraint $constraint): ?ValueGuess
183216
public function guessMaxLengthForConstraint(Constraint $constraint): ?ValueGuess
184217
{
185218
switch (\get_class($constraint)) {
186-
case 'Symfony\Component\Validator\Constraints\Length':
219+
case Length::class:
187220
if (is_numeric($constraint->max)) {
188221
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
189222
}
190223
break;
191224

192-
case 'Symfony\Component\Validator\Constraints\Type':
225+
case Type::class:
193226
if (\in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) {
194227
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
195228
}
196229
break;
197230

198-
case 'Symfony\Component\Validator\Constraints\Range':
231+
case Range::class:
199232
if (is_numeric($constraint->max)) {
200233
return new ValueGuess(\strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
201234
}
@@ -211,27 +244,27 @@ public function guessMaxLengthForConstraint(Constraint $constraint): ?ValueGuess
211244
public function guessPatternForConstraint(Constraint $constraint): ?ValueGuess
212245
{
213246
switch (\get_class($constraint)) {
214-
case 'Symfony\Component\Validator\Constraints\Length':
247+
case Length::class:
215248
if (is_numeric($constraint->min)) {
216249
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
217250
}
218251
break;
219252

220-
case 'Symfony\Component\Validator\Constraints\Regex':
253+
case Regex::class:
221254
$htmlPattern = $constraint->getHtmlPattern();
222255

223256
if (null !== $htmlPattern) {
224257
return new ValueGuess($htmlPattern, Guess::HIGH_CONFIDENCE);
225258
}
226259
break;
227260

228-
case 'Symfony\Component\Validator\Constraints\Range':
261+
case Range::class:
229262
if (is_numeric($constraint->min)) {
230263
return new ValueGuess(sprintf('.{%s,}', \strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
231264
}
232265
break;
233266

234-
case 'Symfony\Component\Validator\Constraints\Type':
267+
case Type::class:
235268
if (\in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) {
236269
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
237270
}

0 commit comments

Comments
 (0)