Refactor mime.lib.php function to a static method

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
This commit is contained in:
Maurício Meneghini Fauth 2017-09-12 12:22:45 -03:00
parent 7bd3a54cbb
commit e143d36344
4 changed files with 52 additions and 46 deletions

View File

@ -0,0 +1,39 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* MIME detection code.
*
* @package PhpMyAdmin
* @todo Maybe we could try to use fileinfo module if loaded
*/
namespace PhpMyAdmin;
/**
* PhpMyAdmin\Mime class;
*
* @package PhpMyAdmin
*/
class Mime
{
/**
* Tries to detect MIME type of content.
*
* @param string &$test First few bytes of content to use for detection
*
* @return string
*/
public static function detect(&$test)
{
$len = mb_strlen($test);
if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) {
return 'image/jpeg';
}
if ($len >= 3 && substr($test, 0, 3) == 'GIF') {
return 'image/gif';
}
if ($len >= 4 && mb_substr($test, 0, 4) == "\x89PNG") {
return 'image/png';
}
return 'application/octet-stream';
}
}

View File

@ -1,30 +0,0 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* MIME detection code.
*
* @package PhpMyAdmin
* @todo Maybe we could try to use fileinfo module if loaded
*/
/**
* Tries to detect MIME type of content.
*
* @param string &$test First few bytes of content to use for detection
*
* @return string
*/
function PMA_detectMIME(&$test)
{
$len = mb_strlen($test);
if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) {
return 'image/jpeg';
}
if ($len >= 3 && substr($test, 0, 3) == 'GIF') {
return 'image/gif';
}
if ($len >= 4 && mb_substr($test, 0, 4) == "\x89PNG") {
return 'image/png';
}
return 'application/octet-stream';
}

View File

@ -7,6 +7,7 @@
*/
use PhpMyAdmin\Core;
use PhpMyAdmin\Mime;
/**
* Common functions.
@ -15,7 +16,6 @@ use PhpMyAdmin\Core;
// data
define('PMA_BYPASS_GET_INSTANCE', 1);
require_once 'libraries/common.inc.php';
require_once 'libraries/mime.lib.php';
/* Check parameters */
PhpMyAdmin\Util::checkParameters(
@ -53,7 +53,7 @@ if ($result === false) {
Core::downloadHeader(
$table . '-' . $_GET['transform_key'] . '.bin',
PMA_detectMIME($result),
Mime::detect($result),
strlen($result)
);
echo $result;

View File

@ -1,48 +1,45 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* tests for mime.lib.php
* tests for PhpMyAdmin\Mime
*
* @package PhpMyAdmin-test
*/
namespace PhpMyAdmin;
/*
* Include to test.
*/
require_once 'libraries/mime.lib.php';
use PhpMyAdmin\Mime;
/**
* Test for mime detection.
*
* @package PhpMyAdmin-test
*/
class PMA_MIME_Test extends PHPUnit_Framework_TestCase
class MimeTest extends \PHPUnit_Framework_TestCase
{
/**
* Test for PMA_detectMIME
* Test for Mime::detect
*
* @param string $test MIME to test
* @param string $output Expected output
*
* @return void
* @dataProvider providerForTestDetectMIME
* @dataProvider providerForTestDetect
*/
public function testDetectMIME($test, $output)
public function testDetect($test, $output)
{
$this->assertEquals(
PMA_detectMIME($test),
Mime::detect($test),
$output
);
}
/**
* Provider for testPMA_detectMIME
* Provider for testDetect
*
* @return array data for testPMA_detectMIME
* @return array data for testDetect
*/
public function providerForTestDetectMIME()
public function providerForTestDetect()
{
return array(
array(