Completed
Push — dev ( 72cd7e...c5a23f )
by James Ekow Abaka
05:21
created

ImagesHelper   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 146
Duplicated Lines 21.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 32
loc 146
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadImage() 16 16 4
A writeImage() 16 16 4
A resize() 0 29 3
A crop() 0 15 2
B thumbnail() 0 27 6
A quality() 0 5 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
namespace ntentan\honam\engines\php\helpers;
3
4
use ntentan\honam\engines\php\Helper;
5
6
7
/**
8
 * 
9
 * 
10
 * @todo Completely rewrite this helper to make it more efficient. Expose the
11
 * interfaces as they are. Consider using Image Magick.
12
 */
13
class ImagesHelper extends Helper
14
{
15
    private $quality = 90;
16
    
17 View Code Duplication
    private function loadImage($path)
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...
18
    {
19
    	$array = explode(".", $path);
20
        switch(strtolower(end($array)))
21
        {
22
            case 'png':
23
                $image = imagecreatefrompng($path);
24
                break;
25
26
            case 'jpeg':
27
            case 'jpg':
28
                $image = imagecreatefromjpeg($path);
29
                break;
30
        }
31
        return $image;
0 ignored issues
show
Bug introduced by
The variable $image does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
32
    }
33
    
34 View Code Duplication
    private function writeImage($image, $path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
35
    {
36
    	$array = explode(".", $path);
37
        switch(strtolower(end($array)))
38
        {
39
            case 'png':
40
                $image = imagepng($image, $path);
41
                break;
42
43
            case 'jpeg':
44
            case 'jpg':
45
                $image = imagejpeg($image, $path, $this->quality);
46
                break;
47
        }
48
        return $image;        
49
    }
50
    
51
    /**
52
     * Resizes an image.
53
     * 
54
     * @param string $src Path to the image to be resized
55
     * @param string $dest Path to sore the resized image
56
     * @param integer $width New width of the image
57
     * @param integer $height New Height of the image
58
     */
59
    public function resize($src, $dest, $width = 0, $height = 0)
60
    {
61
        $im = $this->loadImage($src);
62
        $outputWidth = imagesx($im);
63
        $outputHeight = imagesy($im);
64
65
        $aspect = $outputWidth / $outputHeight;
66
67
        if($width<=0)
68
        {
69
            $width = $aspect * $height;
70
        }
71
        else if($height<=0)
72
        {
73
            $height = $width / $aspect;
74
        }
75
76
        $destinationImage = imagecreatetruecolor($width, $height);
77
        imagealphablending($destinationImage, false );
78
        imagesavealpha($destinationImage, true );
79
        imagecopyresampled($destinationImage, $im, 0, 0, 0, 0, $width, $height, $outputWidth, $outputHeight);
80
81
        $this->writeImage($destinationImage, $dest);
82
        
83
        imagedestroy($im);
84
        imagedestroy($destinationImage);
85
86
        return $dest;
87
    }
88
89
    /**
90
     * Crops an image. This function crops by fitting the image into the center
91
     * of a new cropping area. If the cropping area is smaller than the image
92
     * the image is scaled to fit.
93
     *
94
     * @param string $src The path to the source image
95
     * @param string $dest The path to the destination image
96
     * @param int $width The cropping width
97
     * @param int $height The cropping height
98
     * @param boolean $head Place the cropping area on top of the image
99
     */
100
    public function crop($src, $dest, $width, $height,$head=false)
101
    {
102
        $im = $this->loadImage($src);
103
        $o_width = imagesx($im);
104
        $o_height = imagesy($im);
105
        if($head==false) $top = ($o_height/2)-($height/2); else $top=0;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
106
        $left = ($o_width/2)-($width/2);
107
        $im2 = imagecreatetruecolor ($width, $height);
108
        imagealphablending($im2, false );
109
        imagesavealpha($im2, true );
110
        imagecopyresampled($im2,$im,0,0,$left,$top,$width,$height,$width,$height);
111
        $this->writeImage($im2, $dest);
112
        imagedestroy($im);
113
        imagedestroy($im2);
114
    }
115
116
    /**
117
     * A smart thumbnailing function. Generates thumbnail images without
118
     * distorting the output of the final image.
119
     * @param string $file
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
120
     * @param string $width
121
     * @param string $height
122
     * @param string $head
123
     * @return string
124
     */
125
    public function thumbnail($source, $destination, $width, $height, $head=false, $overwrite=false)
126
    {
127
        if(!is_file($source)) return;
128
        if(!is_file($destination) || (filemtime($source)>filemtime($destination)) || $overwrite === true)
129
        {
130
            $image = $this->loadImage($source);
131
            $imageWidth = imagesx($image);
132
            $imageHeight = imagesy($image);
133
            imagedestroy($image);
134
            $tempImage = 'cache/' . uniqid() . ".png";
135
136
            $aspect = $imageWidth / $imageHeight;
137
138
            if($aspect * $height >= $width)
139
            {
140
                $this->resize($source, $tempImage, 0, $height);
141
            }
142
            else
143
            {
144
                $this->resize($source, $tempImage, $width, 0);
145
            }
146
147
            $this->crop( $tempImage, $destination, $width, $height, $head);
0 ignored issues
show
Bug introduced by
It seems like $head defined by parameter $head on line 125 can also be of type string; however, ntentan\honam\engines\ph...rs\ImagesHelper::crop() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and 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...
148
            unlink($tempImage);
149
        }
150
        return $destination;
151
    }
152
    
153
    public function quality($quality)
154
    {
155
        $this->quality = $quality;
156
        return $this;
157
    }
158
}
159