Thumb   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 21 2
B save() 0 44 8
A exists() 0 4 1
1
<?php
2
3
/**
4
 * Class thumb
5
 */
6
class Thumb
7
{
8
    /**
9
     * @param $filename
10
     * @param $maxWidth
11
     * @param $maxHeight
12
     * @return array
13
     */
14
    public static function make($filename, $maxWidth, $maxHeight)
15
    {
16
        $imgPath = XOOPS_UPLOAD_PATH . '/apcal/' . $filename;
17
18
        $imgInfo = getimagesize($imgPath);
19
        $oWidth  = $imgInfo[0];
20
        $oHeight = $imgInfo[1];
21
        $ratio   = $oHeight / $oWidth;
22
        $nWidth  = $maxWidth;
0 ignored issues
show
Unused Code introduced by
$nWidth 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...
23
        $nHeight = $maxHeight;
0 ignored issues
show
Unused Code introduced by
$nHeight 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...
24
25
        if ($ratio > 1) {
26
            $nHeight = $maxHeight;
27
            $nWidth  = $nHeight / $ratio;
28
        } else {
29
            $nWidth  = $maxWidth;
30
            $nHeight = $nWidth * $ratio;
31
        }
32
33
        return array('width' => $nWidth, 'height' => $nHeight);
34
    }
35
36
    /**
37
     * @param $filename
38
     * @param $maxWidth
39
     * @param $maxHeight
40
     */
41
    public static function save($filename, $maxWidth, $maxHeight)
42
    {
43
        $thumbPath = XOOPS_UPLOAD_PATH . '/apcal/thumbs/' . $filename;
44
        $imgPath   = XOOPS_UPLOAD_PATH . '/apcal/' . $filename;
45
        $nSize     = self::make($filename, $maxWidth, $maxHeight);
46
        $imgInfo   = getimagesize($imgPath);
47
        $oWidth    = $imgInfo[0];
48
        $oHeight   = $imgInfo[1];
49
        $fileType  = $imgInfo[2];
50
51
        switch ($fileType) {
52
            case IMAGETYPE_JPEG:
53
            default:
54
                $img = imagecreatefromjpeg($imgPath);
55
                if (!$img) {
56
                    $img = imagecreatefromstring(file_get_contents($imgPath));
57
                }
58
                break;
59
            case IMAGETYPE_GIF:
0 ignored issues
show
Unused Code introduced by
case IMAGETYPE_GIF: ...f($imgPath); 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...
60
                $img = imagecreatefromgif($imgPath);
61
                break;
62
            case IMAGETYPE_PNG:
63
                $img = imagecreatefrompng($imgPath);
64
                break;
65
        }
66
67
        $nImg    = imagecreatetruecolor($nSize['width'], $nSize['height']);
68
        $bgColor = imagecolorallocate($nImg, 0xFF, 0xFF, 0xFF);
69
        imagefill($nImg, 0, 0, $bgColor);
70
        imagecopyresampled($nImg, $img, 0, 0, 0, 0, $nSize['width'], $nSize['height'], $oWidth, $oHeight);
71
72
        switch ($fileType) {
73
            case IMAGETYPE_JPEG:
74
            default:
75
                imagejpeg($nImg, $thumbPath, 75);
76
                break;
77
            case IMAGETYPE_GIF:
0 ignored issues
show
Unused Code introduced by
case IMAGETYPE_GIF: ...$thumbPath); 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...
78
                imagegif($nImg, $thumbPath);
79
                break;
80
            case IMAGETYPE_PNG:
81
                imagepng($nImg, $thumbPath);
82
                break;
83
        }
84
    }
85
86
    /**
87
     * @param $filename
88
     * @return bool
89
     */
90
    public static function exists($filename)
91
    {
92
        return file_exists(XOOPS_UPLOAD_PATH . '/apcal/thumbs/' . $filename);
93
    }
94
}
95