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

PerfectCMSImage_Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setFieldName() 0 4 1
C validate() 0 26 7
A isImageCorrectWidth() 0 10 3
A getWidthOrHeight() 0 21 4
1
<?php
2
3
class PerfectCMSImage_Validator extends Upload_Validator
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...
4
{
5
    protected $fieldName = '';
6
7
    public function setFieldName($fieldName)
8
    {
9
        $this->fieldName = $fieldName;
10
    }
11
    /**
12
     * Looser check validation that doesn't do is_upload_file()
13
     * checks as we're faking a POST request that PHP didn't generate
14
     * itself.
15
     *
16
     * @return boolean
17
     */
18
    public function validate()
19
    {
20
        $hasError = false;
21
        if(PerfectCMSImageDataExtension::get_enforce_size($this->fieldName)) {
22
            $widthRecommendation = (PerfectCMSImageDataExtension::get_width($this->fieldName) * 2);
23
            $heightRecommendation = (PerfectCMSImageDataExtension::get_height($this->fieldName) * 2);
24
            if ($widthRecommendation) {
25
                if (! $this->isImageCorrectWidth(true, $widthRecommendation)) {
26
                    $this->errors[] = "Expected width: " . $widthRecommendation . "px;";
27
                    $hasError = true;
28
                }
29
            }
30
31
            if ($heightRecommendation) {
32
                if (! $this->isImageCorrectWidth(false, $heightRecommendation)) {
33
                    $this->errors[] = "Expected height: " . $heightRecommendation . "px;";
34
                    $hasError = true;
35
                }
36
            }
37
        }
38
        $parentResult = parent::validate();
39
        if ($hasError) {
40
            return false;
41
        }
42
        return $parentResult;
43
    }
44
45
    protected function isImageCorrectWidth($isWidth, $recommendedWidthOrHeight)
46
    {
47
        $actualWidthOrHeight = $this->getWidthOrHeight($isWidth);
48
        if ($actualWidthOrHeight) {
49
            if ($actualWidthOrHeight != $recommendedWidthOrHeight) {
50
                return false;
51
            }
52
        }
53
        return true;
54
    }
55
56
57
    protected function getWidthOrHeight($isWidth)
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...
58
    {
59
        $imageSize = false;
60
        if (isset($this->tmpFile["tmp_name"])) {
61
            $imageSize = getimagesize($this->tmpFile["tmp_name"]);
62
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
63
            // $imagefile = $this->getFullPath();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
64
            // if($this->exists() && file_exists($imageFile)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
65
            //     $imageSize = getimagesize($imagefile);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
66
            // }
67
        }
68
        if ($imageSize === false) {
69
            return false;
70
        } else {
71
            if ($isWidth) {
72
                return $imageSize[0];
73
            } else {
74
                return $imageSize[1];
75
            }
76
        }
77
    }
78
}
79