Skip to content

Commit

Permalink
Update dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed Jul 15, 2018
1 parent 9166440 commit 8edd73f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 72 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"require-dev": {
"arachne/codeception": "^0.8.0",
"arachne/forms": "^0.4.0 || ^0.5.0",
"codeception/codeception": "^2.3.2",
"codeception/codeception": "^2.4.3",
"eloquent/phony": "^3.0.0",
"eloquent/phony-phpunit": "^4.0.0",
"eloquent/phpstan-phony": "^0.1.1",
"eloquent/phpstan-phony": "^0.3.0",
"friendsofphp/php-cs-fixer": "^2.8.0",
"symfony/validator": "^4.0.0",
"phpstan/phpstan": "^0.9.1",
"phpstan/phpstan-nette": "^0.9.0"
"phpstan/phpstan": "^0.10.0",
"phpstan/phpstan-nette": "^0.10.0",
"phpstan/phpstan-strict-rules": "^0.10.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/eloquent/phpstan-phony/phony.neon
- vendor/arachne/codeception/extension.neon

Expand Down
28 changes: 19 additions & 9 deletions src/Constraint/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FileValidator extends ConstraintValidator
];

/**
* {@inheritdoc}
* @param mixed $value
*/
public function validate($value, Constraint $constraint): void
{
Expand All @@ -50,7 +50,7 @@ public function validate($value, Constraint $constraint): void
switch ($value->getError()) {
case UPLOAD_ERR_INI_SIZE:
$iniLimitSize = self::getMaxFilesize();
if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
if ($constraint->maxSize !== null && $constraint->maxSize < $iniLimitSize) {
$limitInBytes = $constraint->maxSize;
$binaryFormat = $constraint->binaryFormat;
} else {
Expand Down Expand Up @@ -111,7 +111,7 @@ public function validate($value, Constraint $constraint): void
}
}

if (!is_scalar($value) && !$value instanceof FileUpload && !(is_object($value) && method_exists($value, '__toString'))) {
if (!$value instanceof FileUpload && !$this->isCastableToString($value)) {
throw new UnexpectedTypeException($value, 'string');
}

Expand All @@ -135,7 +135,7 @@ public function validate($value, Constraint $constraint): void
return;
}

$sizeInBytes = filesize($path);
$sizeInBytes = (int) filesize($path);

if (0 === $sizeInBytes) {
$this->context->buildViolation($constraint->disallowEmptyMessage)
Expand All @@ -146,7 +146,7 @@ public function validate($value, Constraint $constraint): void
return;
}

if ($constraint->maxSize) {
if ($constraint->maxSize !== null) {
$limitInBytes = $constraint->maxSize;

if ($sizeInBytes > $limitInBytes) {
Expand All @@ -163,7 +163,7 @@ public function validate($value, Constraint $constraint): void
}
}

if ($constraint->mimeTypes) {
if ($constraint->mimeTypes !== []) {
$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value instanceof FileUpload ? $value->getContentType() : finfo_file(finfo_open(FILEINFO_MIME_TYPE), $value);

Expand All @@ -172,8 +172,10 @@ public function validate($value, Constraint $constraint): void
return;
}

if ($discrete = strstr($mimeType, '/*', true)) {
if (strstr($mime, '/', true) === $discrete) {
$discrete = strstr($mimeType, '/*', true);

if ((bool) $discrete) {
if ($mime !== null && strstr($mime, '/', true) === $discrete) {
return;
}
}
Expand Down Expand Up @@ -236,7 +238,7 @@ private function factorizeSizes(int $size, int $limit, bool $binaryFormat): arra
*/
public static function getMaxFilesize(): int
{
$iniMax = strtolower(ini_get('upload_max_filesize'));
$iniMax = strtolower((string) ini_get('upload_max_filesize'));
if ('' === $iniMax) {
return PHP_INT_MAX;
}
Expand All @@ -260,4 +262,12 @@ public static function getMaxFilesize(): int

return $max;
}

/**
* @param mixed $value
*/
private function isCastableToString($value): bool
{
return is_scalar($value) || (is_object($value) && method_exists($value, '__toString'));
}
}
2 changes: 1 addition & 1 deletion src/Type/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults(
[
'data_class' => function (Options $options) {
return $options['multiple'] ? null : FileUpload::class;
return (bool) $options['multiple'] ? null : FileUpload::class;
},
]
);
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/src/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function testMaxSize($maxSize, int $bytes, bool $binaryFormat): void
{
$file = new File(['maxSize' => $maxSize]);

$this->assertSame($bytes, $file->maxSize);
$this->assertSame($binaryFormat, $file->binaryFormat);
self::assertSame($bytes, $file->maxSize);
self::assertSame($binaryFormat, $file->binaryFormat);
}

/**
Expand All @@ -33,8 +33,8 @@ public function testMaxSizeCanBeSetAfterInitialization($maxSize, int $bytes, boo
$file = new File();
$file->maxSize = $maxSize;

$this->assertSame($bytes, $file->maxSize);
$this->assertSame($binaryFormat, $file->binaryFormat);
self::assertSame($bytes, $file->maxSize);
self::assertSame($binaryFormat, $file->binaryFormat);
}

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize
} catch (ConstraintDefinitionException $e) {
}

$this->assertSame(1000, $file->maxSize);
self::assertSame(1000, $file->maxSize);
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ public function testBinaryFormat($maxSize, ?bool $guessedFormat, bool $binaryFor
{
$file = new File(['maxSize' => $maxSize, 'binaryFormat' => $guessedFormat]);

$this->assertSame($binaryFormat, $file->binaryFormat);
self::assertSame($binaryFormat, $file->binaryFormat);
}

public function provideFormats(): array
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/src/FileTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ public function testSetData(): void
$form = $this->factory->createBuilder(FileType::class)->getForm();
$data = $this->createUploadedFileMock('abcdef', 'original.jpg', true);
$form->setData($data);
$this->assertSame($data, $form->getData());
self::assertSame($data, $form->getData());
}

public function testSubmit(): void
{
$form = $this->factory->createBuilder(FileType::class)->getForm();
$data = $this->createUploadedFileMock('abcdef', 'original.jpg', true);
$form->submit($data);
$this->assertSame($data, $form->getData());
self::assertSame($data, $form->getData());
}

public function testSubmitEmpty(): void
{
$form = $this->factory->createBuilder(FileType::class)->getForm();
$form->submit(null);
$this->assertNull($form->getData());
self::assertNull($form->getData());
}

public function testSubmitEmptyMultiple(): void
Expand All @@ -64,7 +64,7 @@ public function testSubmitEmptyMultiple(): void
// submitted data when an input file is uploaded without choosing any file
$form->submit([null]);

$this->assertSame([], $form->getData());
self::assertSame([], $form->getData());
}

public function testSetDataMultiple(): void
Expand All @@ -85,7 +85,7 @@ public function testSetDataMultiple(): void
];

$form->setData($data);
$this->assertSame($data, $form->getData());
self::assertSame($data, $form->getData());
}

public function testSubmitMultiple(): void
Expand All @@ -106,10 +106,10 @@ public function testSubmitMultiple(): void
];

$form->submit($data);
$this->assertSame($data, $form->getData());
self::assertSame($data, $form->getData());
$view = $form->createView();
$this->assertSame('arachne_file[]', $view->vars['full_name']);
$this->assertArrayHasKey('multiple', $view->vars['attr']);
self::assertSame('arachne_file[]', $view->vars['full_name']);
self::assertArrayHasKey('multiple', $view->vars['attr']);
}

public function testDontPassValueToView(): void
Expand All @@ -119,7 +119,7 @@ public function testDontPassValueToView(): void
FileType::class => $this->createUploadedFileMock('abcdef', 'original.jpg', true),
]);
$view = $form->createView();
$this->assertEquals('', $view->vars['value']);
self::assertEquals('', $view->vars['value']);
}

private function createUploadedFileMock(string $name, string $originalName, bool $valid): FileUpload
Expand Down
Loading

0 comments on commit 8edd73f

Please sign in to comment.