Completed
Push — master ( 77e198...eed86b )
by Nicolaas
02:00
created

EVCPage_Controller::BackgroundImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
4
class EVCPage extends Page
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...
5
{
6
}
7
8
9
class EVCPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
10
{
11
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions 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...
12
        "show" => true,
13
        "save" => true,
14
        "retrieve" => true,
15
        "reset" => true,
16
        "previous" => true,
17
        "lock" => true
18
    );
19
20
    public function init()
21
    {
22
        parent::init();
23
        Requirements::javascript("framework/thirdparty/jquery/jquery.js");
24
        if(Director::isLive()) {
25
            Requirements::themedCSS('ElectricVehicleCalculator.min', 'electric-vehicle-calculator');
26
            // Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
27
            Requirements::javascript("electric-vehicle-calculator/thirdparty/chartjs/Chart.min.js");
28
            Requirements::javascript("electric-vehicle-calculator/javascript/ElectricVehicleCalculator.min.js");
29
        } else {
30
            Requirements::themedCSS('ElectricVehicleCalculator', 'electric-vehicle-calculator');
31
            Requirements::javascript("electric-vehicle-calculator/thirdparty/chartjs/Chart.min.js");
32
            // Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
33
            Requirements::javascript("electric-vehicle-calculator/javascript/ElectricVehicleCalculator.js");
34
        }
35
    }
36
37
    protected $evcDataSet = null;
38
39
    public function index()
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...
40
    {
41
        $code = Session::get("EVCLastCode");
42
        $this->evcDataSet = EVCDataSet::find_or_create($code, true);
43
        if (!$code) {
44
            $code = $this->evcDataSet->Code;
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<EVCDataSet>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
            Session::set("EVCLastCode", $code);
46
        }
47
        return $this->redirect($this->evcDataSet->MyLink($this, "show"));
48
    }
49
50
    function BackgroundImage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52
        if(rand(0,1) == 1) {
53
            return '/electric-vehicle-calculator/images/nz-road1.jpg';
54
        } else {
55
            return '/electric-vehicle-calculator/images/nz-road2.jpg';
56
        }
57
    }
58
59
    public function HasCustomTitle()
60
    {
61
        if ($this->evcDataSet && $this->evcDataSet->Title) {
62
            return true;
63
        }
64
    }
65
66 View Code Duplication
    public function Title()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68
        if ($this->HasCustomTitle()) {
69
            return Convert::raw2xml(urldecode($this->evcDataSet->Title));
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
        }
71
        return Convert::raw2xml($this->Title);
72
    }
73
74 View Code Duplication
    public function MetaTitle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        if ($this->HasCustomTitle()) {
77
            return Convert::raw2xml(urldecode($this->evcDataSet->Title));
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
        }
79
        return Convert::raw2xml($this->MetaTitle);
80
    }
81
82
    public function show($request)
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...
83
    {
84
        $code = $request->param("ID");
85
        $this->evcDataSet = EVCDataSet::find_or_create($code, false);
86
        if ($this->evcDataSet && $this->evcDataSet->exists()) {
87
            Requirements::javascript('electric-vehicle-calculator/javascript/ElectricVehicleCalculator.js');
88
            $link = $this->Link();
89
            if($link === '/') {
90
                $link = '/home/';
91
            }
92
            //Requirements::javascript("assets/evc/translations.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
93
            Requirements::customScript("
94
                EVC.serverInteraction.baseLink = '".$link."';
95
                EVC.serverInteraction.serverKey = '".$this->evcDataSet->Code."';
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
            ", "EVCSetBasics");
97
            Requirements::customScript($this->evcDataSet->returnValuesAsJS(), "EVCreturnValuesAsJS");
98
            return array();
99
        } else {
100
            return $this->httpError(404);
101
        }
102
    }
103
104
    /**
105
     * returns empty string on error...
106
     * ajax method ...
107
     * @return string
108
     */
109
    public function save($request)
110
    {
111
        $code = $request->param("ID");
112
        $this->evcDataSet = EVCDataSet::find_or_create($code, true);
113
        //save it
114
        $key = Convert::raw2sql($request->getVar("key"));
115
        $value = Convert::raw2sql($request->getVar("value"));
116
        if ($newCode = $this->evcDataSet->setValue($key, $value)) {
0 ignored issues
show
Bug introduced by
It seems like $key defined by \Convert::raw2sql($request->getVar('key')) on line 114 can also be of type array; however, EVCDataSet::setValue() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $value defined by \Convert::raw2sql($request->getVar('value')) on line 115 can also be of type array; however, EVCDataSet::setValue() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
            Session::set("EVCLastCode", $newCode);
118
            Session::save();
119
            return $newCode;
120
        }
121
    }
122
123
    public function retrieve($request)
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...
124
    {
125
        $code = $request->param("ID");
126
        $this->evcDataSet = EVCDataSet::find_or_create($code, false);
127
        if ($this->evcDataSet && $this->evcDataSet->exists() && $this->evcDataSet->Data) {
0 ignored issues
show
Documentation introduced by
The property Data does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
            Session::set("EVCLastCode", $this->evcDataSet->Code);
0 ignored issues
show
Documentation introduced by
The property Code does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
            Session::save();
130
            return $this->redirect($this->evcDataSet->MyLink($this, "show"));
131
        } else {
132
            return $this->httpError(404);
133
        }
134
    }
135
136
    public function reset($request)
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...
137
    {
138
        Session::set("EVCLastCode", "");
139
        Session::clear("EVCLastCode");
140
        Session::save();
141
        return $this->redirect($this->Link());
142
    }
143
144
    /**
145
     * ajax method ...
146
     *
147
     */
148
    public function lock($request)
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...
149
    {
150
        $code = $request->param("ID");
151
        $this->evcDataSet = EVCDataSet::find_or_create($code, false);
152
        $title = $request->getVar("title");
153
        if ($title && $this->evcDataSet && $this->evcDataSet->exists()) {
154
            if ($title == "ignore") {
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...
155
                //no need to do anything
156
            } else {
157
                $this->evcDataSet = $this->evcDataSet->getCopyIfNeeded();
158
                $this->evcDataSet->Locked = true;
0 ignored issues
show
Documentation introduced by
The property Locked does not exist on object<EVCDataSet>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
                $this->evcDataSet->Title = Convert::raw2sql(urldecode($title));
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EVCDataSet>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
160
                $this->evcDataSet->write();
161
            }
162
            return $this->evcDataSet->MyLink($this, "retrieve");
163
        }
164
        return "ERROR!";
165
    }
166
167
    public function EVCDataSet()
168
    {
169
        return $this->evcDataSet;
170
    }
171
172
    public function IsLocked()
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...
173
    {
174
        return $this->evcDataSet->Locked;
0 ignored issues
show
Documentation introduced by
The property Locked does not exist on object<EVCDataSet>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
    }
176
177
178
    protected $previousCalculations = null;
179
180
    public function previous($request)
181
    {
182
        $this->previousCalculations = PaginatedList::create(EVCDataSet::get()->filter(array("Locked" => 1))->where("Title IS NOT NULL AND Title <> ''"));
183
        $this->previousCalculations->setPageLength(100);
184
        return array();
185
    }
186
187
    public function PreviousCalculations()
188
    {
189
        return $this->previousCalculations;
190
    }
191
}
192