FileSizeScalerTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 44.55 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 45
loc 101
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A __destruct() 0 5 2
A setUp() 0 5 1
A test_scale_down() 8 8 1
A test_scale_down_with_max_width() 11 11 2
A test_scale_down_with_max_filesize() 9 9 1
A test_scale_down_with_include_source_disabled() 9 9 1
A createImageObject() 0 5 1
A createSourceFile() 0 6 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
3
namespace Brendt\Image\Tests\Phpunit\Scaler;
4
5
use Brendt\Image\Config\DefaultConfigurator;
6
use Brendt\Image\Scaler\FileSizeScaler;
7
use Intervention\Image\ImageManager;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
12
class FileSizeScalerTest extends TestCase
13
{
14
15
    /**
16
     * @var Filesystem
17
     */
18
    private $fs;
19
20
    /**
21
     * @var string
22
     */
23
    private $publicPath = './tests/public';
24
25
    /**
26
     * @var FileSizeScaler
27
     */
28
    private $scaler;
29
30
    /**
31
     * @var ImageManager
32
     */
33
    private $engine;
34
35 View Code Duplication
    public function __construct() {
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...
36
        parent::__construct();
37
38
        $this->fs = new Filesystem();
39
        $this->engine = new ImageManager([
40
            'driver' => 'gd',
41
        ]);
42
    }
43
44
    public function __destruct() {
45
        if ($this->fs->exists($this->publicPath)) {
46
            $this->fs->remove($this->publicPath);
47
        }
48
    }
49
50
    public function setUp() {
51
        $this->scaler = new FileSizeScaler(new DefaultConfigurator([
52
            'publicPath' => $this->publicPath,
53
        ]));
54
    }
55
56 View Code Duplication
    public function test_scale_down() {
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...
57
        $sourceFile = $this->createSourceFile();
58
        $imageObject = $this->createImageObject();
59
60
        $sizes = $this->scaler->scale($sourceFile, $imageObject);
61
62
        $this->assertTrue(count($sizes) > 1);
63
    }
64
65 View Code Duplication
    public function test_scale_down_with_max_width() {
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...
66
        $sourceFile = $this->createSourceFile();
67
        $imageObject = $this->createImageObject();
68
69
        $this->scaler->setMaxWidth(1000);
70
        $sizes = $this->scaler->scale($sourceFile, $imageObject);
71
72
        foreach ($sizes as $width => $height) {
73
            $this->assertTrue($width < 1000);
74
        }
75
    }
76
77 View Code Duplication
    public function test_scale_down_with_max_filesize() {
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...
78
        $sourceFile = $this->createSourceFile();
79
        $imageObject = $this->createImageObject();
80
81
        $this->scaler->setMaxFileSize(100000);
82
        $sizes = $this->scaler->scale($sourceFile, $imageObject);
83
84
        $this->assertCount(3, $sizes);
85
    }
86
87 View Code Duplication
    public function test_scale_down_with_include_source_disabled() {
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...
88
        $sourceFile = $this->createSourceFile();
89
        $imageObject = $this->createImageObject();
90
91
        $this->scaler->setIncludeSource(false);
92
        $sizes = $this->scaler->scale($sourceFile, $imageObject);
93
94
        $this->assertFalse(array_key_exists(1920, $sizes));
95
    }
96
97
    // TODO: test algorithm
98
99
    private function createImageObject() {
100
        $imageObject = $this->engine->make('./tests/img/image.jpeg');
101
102
        return $imageObject;
103
    }
104
105
    private function createSourceFile() {
106
        $sourceFiles = Finder::create()->files()->in('./tests/img')->name('image.jpeg')->getIterator();
107
        $sourceFiles->rewind();
108
109
        return $sourceFiles->current();
110
    }
111
112
}
113