Issues (201)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

code/modifiers/MinMaxModifier.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 * @package: ecommerce
6
 * @sub-package: ecommerce_stockcontrol
7
 * @description: makes sure that a buyable quantity in cart stays between a min and a max
8
 */
9
class MinMaxModifier extends OrderModifier
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...
10
{
11
12
//--------------------------------------------------------------------*** static variables
13
14
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $db 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
        "Adjustments" => "HTMLText"
16
    );
17
18
    private static $singular_name = "Stock Adjustment";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
19
    public function i18n_singular_name()
20
    {
21
        self::$singular_name;
22
    }
23
24
    private static $plural_name = "Stock Adjustments";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
25
    public function i18n_plural_name()
26
    {
27
        return self::$plural_name;
28
    }
29
30
    private static $title = "MinMaxModifier";
0 ignored issues
show
The property $title 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...
31
32
    private static $default_min_quantity = 1;
33
34
    private static $default_max_quantity = 9999;
35
36
    private static $min_field = "MinQuantity";
37
38
    private static $max_field = "MaxQuantity";
39
40
    private static $adjustment_message = "Based on stock availability, quantities have been adjusted as follows: ";
41
42
    private static $sorry_message = "Sorry, your selected value not is available.";
43
44
    private static $use_stock_quantities = true;
45
46
    private static $ids_of_items_adjusted = array();
47
48
//-------------------------------------------------------------------- *** static functions
49
50
    public static function show_form()
51
    {
52
        self::apply_min_max();
53
        return false;
54
    }
55
56
    public static function get_form($controller)
0 ignored issues
show
The parameter $controller is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        return false;
59
    }
60
61
//-------------------------------------------------------------------- *** cms fuctions
62
63
    public function getCMSFields()
64
    {
65
        $fields = parent::getCMSFields();
66
        $fields->removeByName("Adjustments");
67
        $fields->addFieldToTab("Root.Debug", new ReadonlyField("AdjustmentsShown", "Adjustments", $this->Adjustments));
0 ignored issues
show
The property Adjustments does not exist on object<MinMaxModifier>. 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...
68
        return $fields;
69
    }
70
//-------------------------------------------------------------------- *** display functions
71
    public function CanBeRemoved()
72
    {
73
        return false;
74
    }
75
76
    public function ShowInTable()
77
    {
78
        return false;
79
    }
80
81
82
//--------------------------------------------------------------------*** table values
83
    public function LiveCalculatedTotal()
84
    {
85
        self::apply_min_max();
86
        return 0;
87
    }
88
89
    public function LiveTableValue()
90
    {
91
        return "";
92
    }
93
94
95
//--------------------------------------------------------------------*** table titles
96
    public function LiveName()
97
    {
98
        return "";
99
    }
100
101
//-------------------------------------------------------------------- *** calculations
102
    public static function apply_min_max()
103
    {
104
        if (self::$min_field || self::$max_field  || self::$default_min_quantity || self::$default_max_quantity) {
105
            $msgArray = array();
106
            $minFieldName = self::$min_field;
107
            $maxFieldName = self::$max_field;
108
            $currentOrder = ShoppingCart::current_order();
109
            if ($currentOrder->IsSubmitted()) {
110
                //too late!
111
                return;
112
            }
113
            $items = $currentOrder->Items();
114
            $i = 0;
115
            if ($items) {
116
                foreach ($items as $item) {
117
                    $buyable = $item->Buyable();
118
                    if ($buyable) {
119
                        $quantity = $item->Quantity;
120
                        $absoluteMin = self::$default_min_quantity;
121
                        $absoluteMax = self::$default_max_quantity;
122
                        $parent = $buyable->Parent();
123 View Code Duplication
                        if ($minFieldName) {
0 ignored issues
show
This code seems to be duplicated across 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...
124
                            if (isset($buyable->$minFieldName) && $buyable->$minFieldName > 0) {
125
                                $absoluteMin = $buyable->$minFieldName;
126
                            }
127
                            //product variations
128
                            elseif (!isset($buyable->$minFieldName)) {
129
                                if ($parent && isset($parent->$minFieldName) && $parent->$minFieldName > 0) {
130
                                    $absoluteMin = $parent->$minFieldName;
131
                                }
132
                            }
133
                        }
134 View Code Duplication
                        if ($maxFieldName) {
0 ignored issues
show
This code seems to be duplicated across 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...
135
                            if (isset($buyable->$maxFieldName) && $buyable->$maxFieldName > 0) {
136
                                $absoluteMax = $buyable->$maxFieldName;
137
                            }
138
                            //product variations
139
                            elseif (!isset($buyable->$maxFieldName)) {
140
                                if ($parent && isset($parent->$maxFieldName) && $parent->$maxFieldName > 0) {
141
                                    $absoluteMax = $parent->$maxFieldName;
142
                                }
143
                            }
144
                        }
145
                        if ($buyable->UnlimitedStock) {
0 ignored issues
show
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...
146
                            //nothing more to do
147
                        } elseif (self::$use_stock_quantities) {
148
                            $maxStockQuantity = $buyable->getActualQuantity();
149
                            if ($absoluteMax > $maxStockQuantity) {
150
                                $absoluteMax = $maxStockQuantity;
151
                            }
152
                            if ($absoluteMin > $maxStockQuantity) {
153
                                $absoluteMax = 0;
154
                                $maxStockQuantity = 0;
0 ignored issues
show
$maxStockQuantity 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...
155
                            }
156
                        }
157
                        $absoluteMin = intval($absoluteMin) - 0;
158
                        $absoluteMax = intval($absoluteMax) - 0;
159
                        $newValue = $quantity;
160
                        if ($quantity < $absoluteMin && $absoluteMin > 0) {
161
                            debug::log("adjusting for MIN: $quantity < $absoluteMin");
162
                            $newValue = $absoluteMin;
163
                        }
164
                        if ($quantity > $absoluteMax && $absoluteMax > 0) {
165
                            debug::log("adjusting for MAX: $quantity > $absoluteMax");
166
                            $newValue = $absoluteMax;
167
                        }
168
                        if ($quantity != $newValue) {
169
                            $item->Quantity = $newValue;
170
                            ShoppingCart::singleton()->setQuantity($buyable, $newValue);
171
                            $msgArray[$i] = $buyable->Title." changed from ".$quantity." to ".$newValue;
172
                            $i++;
173
                            $quantity = $newValue;
0 ignored issues
show
$quantity 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...
174
                            self::$ids_of_items_adjusted[$item->ID] = $item->ID;
175
                        }
176
                        if (Director::is_ajax()) {
0 ignored issues
show
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...
177
                            //do nothing
178
                        } else {
179
                            //IS THIS WORKING?
180
                            $fieldName = $item->AJAXDefinitions()->QuantityFieldName();
181
                            $js = '
182
                                MinMaxModifierData = [];
183
                                MinMaxModifierData.push(
184
                                    {
185
                                        selector: "input[name=\''.$fieldName.'\']",
186
                                        min: '.intval($absoluteMin).',
187
                                        max: '.intval($absoluteMax).',
188
                                        msg: "'.addslashes(self::$sorry_message).'"
189
                                    }
190
                                );';
191
                            Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js");
192
                            Requirements::customScript($js, $fieldName);
193
                        }
194
                    }
195
                }
196
            }
197
        }
198
        if (count($msgArray)) {
199
            if (self::$adjustment_message) {
200
                $msg = self::$adjustment_message."\n".implode("\n", $msgArray);
0 ignored issues
show
The variable $msgArray 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...
201
                if ($msg && !Director::is_ajax()) {
202
                    Requirements::customScript('alert("'.Convert::raw2js($msg).'");', "MinMaxModifierAlert");
203
                }
204
                //$this->Adjustments = $msg;
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...
205
            }
206
        }
207
    }
208
209
    public function updateForAjax(array $js)
210
    {
211
        parent::updateForAjax($js);
212
        self::apply_min_max();
213
        if (is_array(self::$ids_of_items_adjusted) && count(self::$ids_of_items_adjusted)) {
214
            $items = OrderItem::get()->filter(array('ID' => self::$ids_of_items_adjusted));
215
            if ($items->count()) {
216
                foreach ($items as $item) {
217
                    $item->updateForAjax($js);
218
                }
219
            }
220
        }
221
        return $js;
222
    }
223
224
225
//--------------------------------------------------------------------*** database functions
226
}
227