Completed
Push — master ( d09fc3...fde49e )
by samayo
05:21
created

func.image-resize.php ➔ resize()   D

Complexity

Conditions 13
Paths 200

Size

Total Lines 77
Code Lines 52

Duplication

Lines 28
Ratio 36.36 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 13
eloc 52
c 3
b 1
f 1
nc 200
nop 8
dl 28
loc 77
rs 4.8488

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Image Resizing Function. 
4
 *
5
 * @author     Daniel, Simon <[email protected]>
6
 * @link       https://github.com/samayo/bulletproof
7
 * @copyright  Copyright (c) 2015 Simon Daniel
8
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
9
 */
10
namespace Bulletproof;
11
12
function resize($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight, $ratio = FALSE, $upsize = TRUE){           
13
    
14
    // First, calculate the height.
15
    $height = intval($newWidth / $imgWidth * $imgHeight);
16
17
    // If the height is too large, set it to the maximum height and calculate the width.
18 View Code Duplication
    if ($height > $newHeight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
19
20
        $height = $newHeight;
21
        $newWidth = intval($height / $imgHeight * $imgWidth);
22
    }
23
24
    // If we don't allow upsizing check if the new width or height are too big.
25
    if (!$upsize) {
26
        // If the given width is larger then the image height, then resize it.
27 View Code Duplication
        if ($newWidth > $imgWidth) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            $newWidth = $imgWidth;
29
            $height = intval($newWidth / $imgWidth * $imgHeight);
30
        }
31
32
        // If the given height is larger then the image height, then resize it.
33 View Code Duplication
        if ($height > $imgHeight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $height = $imgHeight;
35
            $newWidth = intval($height / $imgHeight * $imgWidth);
36
        }
37
    }
38
39
    if ($ratio == true)
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...
40
    {
41
        $source_aspect_ratio = $imgWidth / $imgHeight;
42
        $thumbnail_aspect_ratio = $newWidth / $newHeight;
43
        if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) {
44
            $newWidth = $imgWidth;
45
            $newHeight = $imgHeight;
46
        } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
47
            $newWidth = (int) ($newHeight * $source_aspect_ratio);
48
            $newHeight = $newHeight;
0 ignored issues
show
Bug introduced by
Why assign $newHeight to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
49
        } else {
50
            $newWidth = $newWidth;
0 ignored issues
show
Bug introduced by
Why assign $newWidth to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
51
            $newHeight = (int) ($newWidth / $source_aspect_ratio);
52
        }
53
    }
54
            
55
    $imgString = file_get_contents($image);
56
57
    $imageFromString = imagecreatefromstring($imgString);
58
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
59
    imagecopyresampled(
60
        $tmp,
61
        $imageFromString,
62
        0,
63
        0,
64
        0,
65
        0,
66
        $newWidth,
67
        $newHeight,
68
        $imgWidth,
69
        $imgHeight
70
    );
71
72 View Code Duplication
    switch ($mimeType) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
        case "jpeg":
74
        case "jpg":
75
            imagejpeg($tmp, $image, 90);
76
            break;
77
        case "png":
78
            imagepng($tmp, $image, 0);
79
            break;
80
        case "gif":
81
            imagegif($tmp, $image);
82
            break;
83
        default:
84
            throw new \Exception(" Only jpg, jpeg, png and gif files can be resized ");
85
            break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
86
    }
87
 
88
}
89
90
91