Completed
Push — master ( fc47f1...dfe1c8 )
by Nicolaas
01:41
created

PerfectCMSImageDataExtension::get_sizes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * defines the image sizes
5
 * and default upload folder.
6
 */
7
class PerfectCMSImageDataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * background image for padded images...
11
     *
12
     * @var string
13
     */
14
    private static $perfect_cms_images_background_padding_color = '#cccccc';
0 ignored issues
show
Unused Code introduced by
The property $perfect_cms_images_background_padding_color is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    /***
17
     * sizes of the images
18
     *     width: 3200
19
     *     height: 3200
20
     *     folder: "myfolder"
21
     *     filetype: "try jpg"
22
     *
23
     * @var array
24
     *
25
     */
26
    private static $perfect_cms_images_image_definitions = array();
0 ignored issues
show
Unused Code introduced by
The property $perfect_cms_images_image_definitions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28
    /**
29
     * @var string $name name of Image Field template
30
     * @return string (link)
31
     */
32
    public function PerfectCMSImageLinkNonRetina($name)
33
    {
34
        return $this->PerfectCMSImageLink($name, null, '', false);
35
    }
36
37
    /**
38
     * @var string $name name of Image Field template
39
     * @return string (link)
40
     */
41
    public function PerfectCMSImageLinkRetina($name)
42
    {
43
        return $this->PerfectCMSImageLink($name, null, '', true);
44
    }
45
46
    /**
47
     * @param string            $name
48
     * @param object (optional) $backupObject
49
     * @param string (optional) $backupField
50
     *
51
     * @return string
52
     */
53
    public function PerfectCMSImageLink(
54
        $name,
55
        $backupObject = null,
56
        $backupField = '',
57
        $isRetina = true
58
    ) {
59
        if (! Config::inst()->get('Image', 'force_resample')) {
60
            Config::inst()->update('Image', 'force_resample', true);
61
        }
62
        $image = $this->owner;
63
        if ($image && $image->exists()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
64
            //we are all good ...
65
        } else {
66
            if (!$backupObject) {
67
                $backupObject = SiteConfig::current_site_config();
68
            }
69
            if (!$backupField) {
70
                $backupField = $name;
71
            }
72
            if ($backupObject->hasMethod($backupField)) {
73
                $image = $backupObject->$backupField();
74
            }
75
        }
76
77
        $perfectWidth = (intval(self::get_width($name)) - 0);
78
        $perfectHeight = (intval(self::get_height($name)) - 0);
79
        if ($isRetina) {
80
            $perfectWidth = $perfectWidth * 2;
81
            $perfectHeight = $perfectHeight  * 2;
82
        }
83
        if ($image) {
84
            if ($image instanceof Image) {
85
                if ($image->exists()) {
86
                    //get preferred width and height
87
                    $myWidth = $image->getWidth();
88
                    $myHeight = $image->getHeight();
89
                    $backEndString = Image::get_backend();
90
                    $backend = Injector::inst()->get($backEndString);
0 ignored issues
show
Unused Code introduced by
$backend 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...
91
                    if ($perfectWidth && $perfectHeight) {
92
                        if ($myWidth == $perfectWidth || $myHeight ==  $perfectHeight) {
93
                            $link = $image->ScaleWidth($myWidth)->Link();
94
                        } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) {
95
                            $link = $image->Pad(
96
                                $perfectWidth,
97
                                $perfectHeight,
98
                                Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_background_padding_color')
99
                            )->Link();
100
                        } elseif ($myWidth > $perfectWidth || $myHeight > $perfectHeight) {
101
                            $link = $image->FitMax($perfectWidth, $perfectHeight)->Link();
102
                        }
103
                    } elseif ($perfectWidth) {
104
                        $link = $image->ScaleWidth($perfectWidth)->Link();
105
                    } elseif ($perfectHeight) {
106
                        $link = $image->ScaleHeight($perfectHeight)->Link();
107
                    } else {
108
                        $link = $image->ScaleWidth($myWidth)->Link();
109
                    }
110
                    if (class_exists('HashPathExtension')) {
111
                        if ($curr = Controller::curr()) {
112
                            if ($curr->hasMethod('HashPath')) {
113
                                return $curr->HashPath($link, false);
0 ignored issues
show
Bug introduced by
The variable $link 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...
114
                            }
115
                        }
116
                    }
117
                    return $link;
118
                }
119
            }
120
        }
121
        // no image -> provide placeholder
122
        if ($perfectWidth || $perfectHeight) {
123
            if (!$perfectWidth) {
124
                $perfectWidth = $perfectHeight;
125
            }
126
            if (!$perfectHeight) {
127
                $perfectHeight = $perfectWidth;
128
            }
129
            $text = "$perfectWidth x $perfectHeight /2 = ".round($perfectWidth/2)." x ".round($perfectHeight/2)."";
130
131
            return 'https://placehold.it/'.($perfectWidth).'x'.($perfectHeight).'?text='.urlencode($text);
132
        } else {
133
            return 'https://placehold.it/500x500?text='.urlencode('no size set');
134
        }
135
    }
136
137
    /**
138
     * @param string           $name
139
     * @param Image (optional) $image
140
     *
141
     * @return int
142
     */
143
    public static function get_width($name)
144
    {
145
        return self::get_one_value_for_image($name, "width", 0);
146
    }
147
148
    /**
149
     * @param string           $name
150
     * @param Image (optional) $image
151
     *
152
     * @return int
153
     */
154
    public static function get_height($name)
155
    {
156
        return self::get_one_value_for_image($name, "height", 0);
157
    }
158
159
    /**
160
     * @param string           $name
161
     * @param Image (optional) $image
162
     *
163
     * @return string
164
     */
165
    public static function get_folder($name)
166
    {
167
        return self::get_one_value_for_image($name, "folder", 'other-images');
168
    }
169
170
    /**
171
     * @param string           $name
172
     * @param Image (optional) $image
173
     *
174
     * @return string
175
     */
176
    public static function get_file_type($name)
177
    {
178
        return self::get_one_value_for_image($name, "filetype", 'jpg');
179
    }
180
181
    /**
182
     * @param string           $name
183
     * @param Image (optional) $image
184
     *
185
     * @return boolean
186
     */
187
    public static function get_enforce_size($name)
188
    {
189
        return self::get_one_value_for_image($name, "enforce_size", true);
190
    }
191
192
    /**
193
     * @param string $name
194
     * @param int    $key
195
     * @param mixed  $default
196
     *
197
     * @return mixed
198
     */
199
    private static function get_one_value_for_image($name, $key, $default = '')
200
    {
201
        $sizes = self::get_all_values_for_images();
202
        //print_r($sizes);die();
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
        if (isset($sizes[$name])) {
204
            if (isset($sizes[$name][$key])) {
205
                return $sizes[$name][$key];
206
            }
207
        } else {
208
            user_error('no information for image with name: '.$name);
209
        }
210
211
        return $default;
212
    }
213
214
    /**
215
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
216
     */
217
    private static function get_all_values_for_images()
218
    {
219
        return Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_image_definitions');
220
    }
221
}
222