Image   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 81
Duplicated Lines 27.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 22
loc 81
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resizeByWidth() 11 11 1
A resizeByHeight() 11 11 1
A resize() 0 7 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
 * Image.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File\component;
8
9
use Imagine;
10
11
/**
12
 * Class Image
13
 * @package rmrevin\yii\module\File\component
14
 */
15
class Image extends \yii\imagine\Image
16
{
17
18
    /**
19
     * Resize an image by width.
20
     *
21
     * For example,
22
     *
23
     * ~~~
24
     * $obj->resizeByWidth('path\to\image.jpg', 200);
25
     * $obj->resizeByWidth('path\to\image.jpg', 150, \Imagine\Image\ImageInterface::FILTER_UNDEFINED);
26
     * ~~~
27
     *
28
     * @param string $filename the image file path or path alias.
29
     * @param integer $width the resize width
30
     * @param string $filter
31
     * @return \Imagine\Image\ImageInterface
32
     */
33 1 View Code Duplication
    public static function resizeByWidth($filename, $width, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED)
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...
34
    {
35 1
        $img = static::getImagine()
36 1
            ->open(\Yii::getAlias($filename));
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias($filename) targeting yii\BaseYii::getAlias() can also be of type boolean; however, Imagine\Image\ImagineInterface::open() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
38 1
        $height = $img->getSize()->getHeight() / $img->getSize()->getWidth() * $width;
39
40
        return $img
41 1
            ->copy()
42 1
            ->resize(new Imagine\Image\Box($width, $height), $filter);
43
    }
44
45
    /**
46
     * Resize an image by height.
47
     *
48
     * For example,
49
     *
50
     * ~~~
51
     * $obj->resizeByHeight('path\to\image.jpg', 200);
52
     * $obj->resizeByHeight('path\to\image.jpg', 250, \Imagine\Image\ImageInterface::FILTER_UNDEFINED);
53
     * ~~~
54
     *
55
     * @param string $filename the image file path or path alias.
56
     * @param integer $height the resize height
57
     * @param string $filter
58
     * @return \Imagine\Image\ImageInterface
59
     */
60 1 View Code Duplication
    public static function resizeByHeight($filename, $height, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED)
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...
61
    {
62 1
        $img = static::getImagine()
63 1
            ->open(\Yii::getAlias($filename));
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias($filename) targeting yii\BaseYii::getAlias() can also be of type boolean; however, Imagine\Image\ImagineInterface::open() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
65 1
        $width = $img->getSize()->getWidth() / $img->getSize()->getHeight() * $height;
66
67
        return $img
68 1
            ->copy()
69 1
            ->resize(new Imagine\Image\Box($width, $height), $filter);
70
    }
71
72
    /**
73
     * Strict resize an image.
74
     *
75
     * For example,
76
     *
77
     * ~~~
78
     * $obj->resize('path\to\image.jpg', 200, 200);
79
     * $obj->resize('path\to\image.jpg', 150, 250, \Imagine\Image\ImageInterface::FILTER_UNDEFINED);
80
     * ~~~
81
     *
82
     * @param string $filename the image file path or path alias.
83
     * @param integer $width the resize width
84
     * @param integer $height the resize height
85
     * @param string $filter
86
     * @return \Imagine\Image\ImageInterface
87
     */
88 1
    public static function resize($filename, $width, $height, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED)
89
    {
90 1
        return static::getImagine()
91 1
            ->open(\Yii::getAlias($filename))
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias($filename) targeting yii\BaseYii::getAlias() can also be of type boolean; however, Imagine\Image\ImagineInterface::open() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92 1
            ->copy()
93 1
            ->resize(new Imagine\Image\Box($width, $height), $filter);
94
    }
95
}