8000 Use named arguments as Nicolas Grekas suggested · symfonyaml/symfony@6d607a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d607a1

Browse files
symfonyamlsymfonyaml
authored and
symfonyaml
committed
Use named arguments as Nicolas Grekas suggested
1 parent 37a138a commit 6d607a1

File tree

2 files changed

+66
-79
lines changed

2 files changed

+66
-79
lines changed

src/Symfony/Component/Validator/Constraints/Video.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Attribute\HasNamedArguments;
15+
1416
/**
1517
* @author Kev <https://github.com/symfonyaml>
1618
*/
@@ -55,11 +57,6 @@ class Video extends File
5557
self::CORRUPTED_VIDEO_ERROR => 'CORRUPTED_VIDEO_ERROR',
5658
];
5759

58-
/**
59-
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
60-
*/
61-
protected static $errorNames = self::ERROR_NAMES;
62-
6360
public array|string $mimeTypes = 'video/*';
6461
public ?int $minWidth = null;
6562
public ?int $maxWidth = null;
@@ -89,8 +86,8 @@ class Video extends File
8986
public string $allowPortraitMessage = 'The video is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented videos are not allowed.';
9087
public string $corruptedMessage = 'The video file is corrupted.';
9188

89+
#[HasNamedArguments]
9290
public function __construct(
93-
?array $options = null,
9491
int|string|null $maxSize = null,
9592
?bool $binaryFormat = null,
9693
array|string|null $mimeTypes = null,
@@ -136,8 +133,12 @@ public function __construct(
136133
?array $groups = null,
137134
mixed $payload = null,
138135
) {
136+
if (!class_exists(\FFMpeg\FFProbe::class)) {
137+
throw new \LogicException('The "php-ffmpeg/php-ffmpeg" library must be installed to use the Video constraint. Try running "composer require php-ffmpeg/php-ffmpeg".');
138+
}
139+
139140
parent::__construct(
140-
$options,
141+
[], // File constraint do not use named arguments yet
141142
$maxSize,
142143
$binaryFormat,
143144
$mimeTypes,
@@ -185,10 +186,6 @@ public function __construct(
185186
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
186187
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;
187188

188-
if (!class_exists(\FFMpeg\FFProbe::class)) {
189-
throw new \LogicException('The "php-ffmpeg/php-ffmpeg" library must be installed to use the Video constraint. Try running "composer require php-ffmpeg/php-ffmpeg".');
190-
}
191-
192189
if (!\in_array('video/*', (array) $this->mimeTypes, true) && !\array_key_exists('mimeTypesMessage', $options ?? []) && null === $mimeTypesMessage) {
193190
$this->mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
194191
}

src/Symfony/Component/Validator/Tests/Constraints/VideoValidatorTest.php

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,20 @@ public function testFileNotFound(Video $constraint)
8585

8686
public static function provideConstraintsWithNotFoundMessage(): iterable
8787
{
88-
yield 'Doctrine style' => [new Video([
89-
'notFoundMessage' => 'myMessage',
90-
])];
88+
yield 'Doctrine style' => [new Video(notFoundMessage: 'myMessage')];
9189
yield 'Named arguments' => [
9290
new Video(notFoundMessage: 'myMessage'),
9391
];
9492
}
9593

9694
public function testValidSize()
9795
{
98-
$constraint = new Video([
99-
'minWidth' => 1,
100-
'maxWidth' => 2,
101-
'minHeight' => 1,
102-
'maxHeight' => 2,
103-
]);
96+
$constraint = new Video(
97+
minWidth: 1,
98+
maxWidth: 2,
99+
minHeight: 1,
100+
maxHeight: 2,
101+
);
104102

105103
$this->validator->validate($this->video, $constraint);
106104

@@ -123,10 +121,10 @@ public function testWidthTooSmall(Video $constraint)
123121

124122
public static function provideMinWidthConstraints(): iterable
125123
{
126-
yield 'Doctrine style' => [new Video([
127-
'minWidth' => 3,
128-
'minWidthMessage' => 'myMessage',
129-
])];
124+
yield 'Doctrine style' => [new Video(
125+
minWidth: 3,
126+
minWidthMessage: 'myMessage',
127+
)];
130128
yield 'Named arguments' => [
131129
new Video(minWidth: 3, minWidthMessage: 'myMessage'),
132130
];
@@ -148,10 +146,10 @@ public function testWidthTooBig(Video $constraint)
148146

149147
public static function provideMaxWidthConstraints(): iterable
150148
{
151-
yield 'Doctrine style' => [new Video([
152-
'maxWidth' => 1,
153-
'maxWidthMessage' => 'myMessage',
154-
])];
149+
yield 'Doctrine style' => [new Video(
150+
maxWidth: 1,
151+
maxWidthMessage: 'myMessage',
152+
)];
155153
yield 'Named arguments' => [
156154
new Video(maxWidth: 1, maxWidthMessage: 'myMessage'),
157155
];
@@ -173,10 +171,10 @@ public function testHeightTooSmall(Video $constraint)
173171

174172
public static function provideMinHeightConstraints(): iterable
175173
{
176-
yield 'Doctrine style' => [new Video([
177-
'minHeight' => 3,
178-
'minHeightMessage' => 'myMessage',
179-
])];
174+
yield 'Doctrine style' => [new Video(
175+
minHeight: 3,
176+
minHeightMessage: 'myMessage',
177+
)];
180178
yield 'Named arguments' => [
181179
new Video(minHeight: 3, minHeightMessage: 'myMessage'),
182180
];
@@ -198,10 +196,10 @@ public function testHeightTooBig(Video $constraint)
198196

199197
public static function provideMaxHeightConstraints(): iterable
200198
{
201-
yield 'Doctrine style' => [new Video([
202-
'maxHeight' => 1,
203-
'maxHeightMessage' => 'myMessage',
204-
])];
199+
yield 'Doctrine style' => [new Video(
200+
maxHeight: 1,
201+
maxHeightMessage: 'myMessage',
202+
)];
205203
yield 'Named arguments' => [
206204
new Video(maxHeight: 1, maxHeightMessage: 'myMessage'),
207205
];
@@ -225,10 +223,10 @@ public function testPixelsTooFew(Video $constraint)
225223

226224
public static function provideMinPixelsConstraints(): iterable
227225
{
228-
yield 'Doctrine style' => [new Video([
229-
'minPixels' => 5,
230-
'minPixelsMessage' => 'myMessage',
231-
])];
226+
yield 'Doctrine style' => [new Video(
227+
minPixels: 5,
228+
minPixelsMessage: 'myMessage',
229+
)];
232230
yield 'Named arguments' => [
233231
new Video(minPixels: 5, minPixelsMessage: 'myMessage'),
234232
];
@@ -252,10 +250,10 @@ public function testPixelsTooMany(Video $constraint)
252250

253251
public static function provideMaxPixelsConstraints(): iterable
254252
{
255-
yield 'Doctrine style' => [new Video([
256-
'maxPixels' => 3,
257-
'maxPixelsMessage' => 'myMessage',
258-
])];
253+
yield 'Doctrine style' => [new Video(
254+
maxPixels: 3,
255+
maxPixelsMessage: 'myMessage',
256+
)];
259257
yield 'Named arguments' => [
260258
new Video(maxPixels: 3, maxPixelsMessage: 'myMessage'),
261259
];
@@ -277,10 +275,10 @@ public function testRatioTooSmall(Video $constraint)
277275

278276
public static function provideMinRatioConstraints(): iterable
279277
{
280-
yield 'Doctrine style' => [new Video([
281-
'minRatio' => 2,
282-
'minRatioMessage' => 'myMessage',
283-
])];
278+
yield 'Doctrine style' => [new Video(
279+
minRatio: 2,
280+
minRatioMessage: 'myMessage',
281+ F987
)];
284282
yield 'Named arguments' => [
285283
new Video(minRatio: 2, minRatioMessage: 'myMessage'),
286284
];
@@ -302,20 +300,18 @@ public function testRatioTooBig(Video $constraint)
302300

303301
public static function provideMaxRatioConstraints(): iterable
304302
{
305-
yield 'Doctrine style' => [new Video([
306-
'maxRatio' => 0.5,
307-
'maxRatioMessage' => 'myMessage',
308-
])];
303+
yield 'Doctrine style' => [new Video(
304+
maxRatio: 0.5,
305+
maxRatioMessage: 'myMessage',
306+
)];
309307
yield 'Named arguments' => [
310308
new Video(maxRatio: 0.5, maxRatioMessage: 'myMessage'),
311309
];
312310
}
313311

314312
public function testMaxRatioUsesTwoDecimalsOnly()
315313
{
316-
$constraint = new Video([
317-
'maxRatio' => 1.33,
318-
]);
314+
$constraint = new Video(maxRatio: 1.33);
319315

320316
$this->validator->validate($this->video4By3, $constraint);
321317

@@ -324,9 +320,7 @@ public function testMaxRatioUsesTwoDecimalsOnly()
324320

325321
public function testMinRatioUsesInputMoreDecimals()
326322
{
327-
$constraint = new Video([
328-
'minRatio' => 4 / 3,
329-
]);
323+
$constraint = new Video(minRatio: 4 / 3);
330324

331325
$this->validator->validate($this->video4By3, $constraint);
332326

@@ -335,9 +329,7 @@ public function testMinRatioUsesInputMoreDecimals()
335329

336330
public function testMaxRatioUsesInputMoreDecimals()
337331
{
338-
$constraint = new Video 1241 ([
339-
'maxRatio' => 16 / 9,
340-
]);
332+
$constraint = new Video(maxRatio: 16 / 9);
341333

342334
$this->validator->validate($this->video16By9, $constraint);
343335

@@ -360,10 +352,10 @@ public function testSquareNotAllowed(Video $constraint)
360352

361353
public static function provideAllowSquareConstraints(): iterable
362354
{
363-
yield 'Doctrine style' => [new Video([
364-
'allowSquare' => false,
365-
'allowSquareMessage' => 'myMessage',
366-
])];
355+
yield 'Doctrine style' => [new Video(
356+
allowSquare: false,
357+
allowSquareMessage: 'myMessage',
358+
)];
367359
yield 'Named arguments' => [
368360
new Video(allowSquare: false, allowSquareMessage: 'myMessage'),
369361
];
@@ -385,10 +377,10 @@ public function testLandscapeNotAllowed(Video $constraint)
385377

386378
public static function provideAllowLandscapeConstraints(): iterable
387379
{
388-
yield 'Doctrine style' => [new Video([
389-
'allowLandscape' => false,
390-
'allowLandscapeMessage' => 'myMessage',
391-
])];
380+
yield 'Doctrine style' => [new Video(
381+
allowLandscape: false,
382+
allowLandscapeMessage: 'myMessage',
383+
)];
392384
yield 'Named arguments' => [
393385
new Video(allowLandscape: false, allowLandscapeMessage: 'myMessage'),
394386
];
@@ -410,20 +402,18 @@ public function testPortraitNotAllowed(Video $constraint)
410402

411403
public static function provideAllowPortraitConstraints(): iterable
412404
{
413-
yield 'Doctrine style' => [new Video([
414-
'allowPortrait' => false,
415-
'allowPortraitMessage' => 'myMessage',
416-
])];
405+
yield 'Doctrine style' => [new Video(
406+
allowPortrait: false,
407+
allowPortraitMessage: 'myMessage',
408+
)];
417409
yield 'Named arguments' => [
418410
new Video(allowPortrait: false, allowPortraitMessage: 'myMessage'),
419411
];
420412
}
421413

422414
public function testCorrupted()
423415
{
424-
$constraint = new Video([
425-
'maxRatio' => 1,
426-
]);
416+
$constraint = new Video(maxRatio: 1);
427417

428418
$this->validator->validate($this->videoCorrupted, $constraint);
429419

@@ -466,12 +456,12 @@ public function testInvalidMimeTypeWithNarrowedSet(Video $constraint)
466456

467457
public static function provideInvalidMimeTypeWithNarrowedSet()
468458
{
469-
yield 'Doctrine style' => [new Video([
470-
'mimeTypes' => [
459+
yield 'Doctrine style' => [new Video(
460+
mimeTypes: [
471461
'video/mkv',
472462
'video/mov',
473463
],
474-
])];
464+
)];
475465
yield 'Named arguments' => [
476466
new Video(mimeTypes: [
477467
'video/mkv',

0 commit comments

Comments
 (0)
0