ImageHandlerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 31.76 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 27
loc 85
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 2
A createKernel() 0 5 1
A createFileHandler() 0 11 1
A testConstruct() 0 5 1
A testResizeWithRelativeSize() 9 9 1
A testResizeWithStrictSize() 9 9 1
A testCrop() 8 8 1
A testQualityAfterRepeatedResize() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
require_once 'Intraface/functions.php';
3
require_once 'Intraface/modules/filemanager/FileHandler.php';
4
require_once 'Intraface/modules/filemanager/ImageHandler.php';
5
require_once 'file_functions.php';
6
7
class ImageHandlerTest extends PHPUnit_Framework_TestCase
8
{
9
    private $file_name = 'wideonball.jpg';
10
11
    function setUp()
12
    {
13
        $db = MDB2::singleton(DB_DSN);
14
        $db->query('TRUNCATE file_handler');
15
        iht_deltree(PATH_UPLOAD.'1');
16
        if (file_exists(PATH_UPLOAD.'/1/1.jpeg')) {
17
            unlink(PATH_UPLOAD.'/1/1.jpeg');
18
        }
19
    }
20
21
    function createKernel()
22
    {
23
        $kernel = new Stub_Kernel;
24
        return $kernel;
25
    }
26
27
    function createFileHandler()
28
    {
29
30
        $data = array('file_name' => $this->file_name);
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
        $filehandler = new FileHandler($this->createKernel());
32
        copy(dirname(__FILE__) . '/'.$this->file_name, PATH_UPLOAD.$this->file_name);
33
        $filehandler->save(PATH_UPLOAD.$this->file_name, $this->file_name);
34
        $filehandler->load();
35
        $this->assertEquals('', $filehandler->error->view());
36
        return $filehandler;
37
    }
38
39
    ////////////////////////////////////////////////////////////////
40
41
    function testConstruct()
42
    {
43
        $image = new ImageHandler($this->createFileHandler());
44
        $this->assertEquals('ImageHandler', get_class($image));
45
    }
46
47
48 View Code Duplication
    function testResizeWithRelativeSize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
51
        $image = new ImageHandler($this->createFileHandler());
52
        $file = $image->resize(200, 600);
53
        $size = getimagesize($file);
54
        $this->assertEquals(200, $size[0]);
55
        $this->assertEquals(50, $size[1]);
56
    }
57
58 View Code Duplication
    function testResizeWithStrictSize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
61
        $image = new ImageHandler($this->createFileHandler());
62
        $file = $image->resize(200, 300, 'strict');
63
        $size = getimagesize($file);
64
        $this->assertEquals(200, $size[0]);
65
        $this->assertEquals(300, $size[1]);
66
    }
67
68 View Code Duplication
    function testCrop()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $image = new ImageHandler($this->createFileHandler());
71
        $file = $image->crop(100, 100, 200, 20);
72
        $size = getimagesize($file);
73
        $this->assertEquals(100, $size[0]);
74
        $this->assertEquals(100, $size[1]);
75
    }
76
77
    function testQualityAfterRepeatedResize()
78
    {
79
80
        $image = new ImageHandler($this->createFileHandler());
81
        $image->resize(500, 200);
82
        $image->resize(300, 200);
83
        $file1 = $image->resize(200, 200);
84
85
        $image = new ImageHandler($this->createFileHandler());
86
        $file2 = $image->resize(200, 200);
87
88
        // we accept 10% fall in quality! after several resize
89
        $this->assertEquals(filesize($file2), filesize($file1), '', filesize($file2)/100*10);
90
    }
91
}
92