Skip to content
Permalink
Browse files

Add missing try/catch and refactor duplicate code.

  • Loading branch information...
markstory committed Oct 11, 2016
1 parent 30e118c commit 526e96415f50947b687ffb01d72fdbba804de35f
Showing with 30 additions and 18 deletions.
  1. +29 −17 src/Validation/Validation.php
  2. +1 −1 tests/TestCase/Validation/ValidationTest.php
@@ -957,17 +957,9 @@ public static function luhn($check)
*/
public static function mimeType($check, $mimeTypes = [])
{
if ($check instanceof UploadedFileInterface) {
try {
// Uploaded files throw exceptions on upload errors.
$file = $check->getStream()->getMetadata('uri');
} catch (RuntimeException $e) {
return false;
}
} elseif (is_array($check) && isset($check['tmp_name'])) {
$file = $check['tmp_name'];
} else {
$file = $check;
$file = static::getFilename($check);
if ($file === false) {
return false;
}
if (!function_exists('finfo_open')) {
@@ -998,6 +990,29 @@ public static function mimeType($check, $mimeTypes = [])
return in_array($mime, $mimeTypes);
}
/**
* Helper for reading the file out of the various file implementations
* we accept.
*
* @param string|array|\Psr\Http\Message\UploadedFileInterface $check The data to read a filename out of.
* @return string|bool Either the filename or false on failure.
*/
protected static function getFilename($check)
{
if ($check instanceof UploadedFileInterface) {
try {
// Uploaded files throw exceptions on upload errors.
return $check->getStream()->getMetadata('uri');
} catch (RuntimeException $e) {
return false;
}
}
if (is_array($check) && isset($check['tmp_name'])) {
return $check['tmp_name'];
}
return $check;
}
/**
* Checks the filesize
*
@@ -1012,12 +1027,9 @@ public static function mimeType($check, $mimeTypes = [])
*/
public static function fileSize($check, $operator = null, $size = null)
{
if ($check instanceof UploadedFileInterface) {
$file = $check->getStream()->getMetadata('uri');
} elseif (is_array($check) && isset($check['tmp_name'])) {
$file = $check['tmp_name'];
} else {
$file = $check;
$file = static::getFilename($check);
if ($file === false) {
return false;
}
if (is_string($size)) {
@@ -21,8 +21,8 @@
use Cake\TestSuite\TestCase;
use Cake\Validation\Validation;
use Cake\Validation\Validator;
use Zend\Diactoros\UploadedFile;
use Locale;
use Zend\Diactoros\UploadedFile;
require_once __DIR__ . '/stubs.php';

0 comments on commit 526e964

Please sign in to comment.
You can’t perform that action at this time.