ProductQuestionImageSelectorField::Field()   B
last analyzed

Complexity

Conditions 9
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 7.7404
c 0
b 0
f 0
cc 9
nc 4
nop 1
1
<?php
2
3
/**
4
 * form field to select answer for question.
5
 *
6
 *
7
 */
8
9
10
class ProductQuestionImageSelectorField extends OptionsetField
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...
11
{
12
    protected $folderID = 0;
13
14
    protected $options = array();
15
16
    protected $objects = null;
17
18
    protected $width = 50;
19
20
    protected $height = 50;
21
22
    public function __construct($name, $title = '', $options = array(), $value = '', $folderID)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
23
    {
24
        $this->setOptions($options);
25
        $this->setFolderID($folderID);
26
        $this->createObjects();
27
        parent::__construct($name, $title, $options, $value);
28
    }
29
30
    public function setOptions($options)
31
    {
32
        $this->options = $options;
33
    }
34
35
    public function setFolderID($folderID)
36
    {
37
        $this->folderID = $folderID;
38
    }
39
40
    public function setWidth($width)
41
    {
42
        $this->width = $width;
43
    }
44
45
    public function setHeight($height)
46
    {
47
        $this->height = $height;
48
    }
49
50
    public function Field($properties = array())
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...
51
    {
52
        $options = '';
53
        $source = $this->getSource();
0 ignored issues
show
Unused Code introduced by
$source 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...
54
        $count = 0;
55
        if ($this->objects && $this->objects->count()) {
56
            foreach ($this->objects as $image) {
57
                $key = $image->Key;
58
                $itemID = $this->id() . "_" . $image->ID;
59
                $value = $image->Value;
60
                $labelHTML = $value;
61
                $resizedImageObject = $image->getFormattedImage("CroppedImage", $this->width, $this->height);
62
                if ($resizedImageObject) {
63
                    $labelHTML = '<img src="'.$resizedImageObject->Link().'" alt="'.$value.'" />';
64
                }
65
                if ($key == $this->value/* || $useValue */) {
66
                    $useValue = false;
0 ignored issues
show
Unused Code introduced by
$useValue 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...
67
                    $checked = " checked=\"checked\"";
68
                } else {
69
                    $checked="";
70
                }
71
                $odd = ($count + 1) % 2;
72
                $oddEven = $odd ? "odd" : "even";
73
                $extraClass = " val" . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $key);
74
                $position = " pos".$count;
75
                $disabled = $this->disabled ? 'disabled="disabled"' : '';
76
                $options .= "<li class=\"".$oddEven.$extraClass.$position."\"><input id=\"$itemID\" name=\"$this->name\" type=\"radio\" value=\"$key\"$checked $disabled class=\"radio\" /> <label for=\"$itemID\">$labelHTML</label></li>\n";
77
                $count++;
78
            }
79
            $id = $this->id();
80
        }
81
        if (empty($id)) {
82
            $id = 0;
83
        }
84
        return "
85
            <ul id=\"$id\" class=\"optionset {$this->extraClass()}\">
86
            \n$options
87
            </ul>\n";
88
    }
89
90
    protected function createObjects()
91
    {
92
        $this->objects = new ArrayList();
93
        if ($this->options && is_array($this->options) && count($this->options)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
94
            foreach ($this->options as $option) {
95
                $imageOptions = ProductQuestion::create_file_array_from_option($option);
96
                $image = DataObject::get_one(
97
                    'Image',
98
                    array("ParentID" => $this->folderID, "Name" => $imageOptions),
99
                    $cacheDataObjectGetOne = false
100
                );
101
                if ($image) {
102
                    $image->Key = $option;
103
                    $image->Value = $option;
104
                    $this->objects->push($image);
105
                }
106
            }
107
        }
108
    }
109
}
110