BuyableStockCalculatedQuantity   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 303
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 48
lcom 2
cbo 8
dl 0
loc 303
rs 8.4864
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A canView() 0 4 1
A Link() 0 4 1
A HistoryLink() 0 4 1
A Buyable() 0 4 1
A getBuyable() 0 7 3
A UnlimitedStock() 0 4 1
A getUnlimitedStock() 0 6 2
A Name() 0 4 1
A getName() 0 7 2
A canDoAnything() 0 9 3
A get_quantity_by_buyable() 0 12 3
B get_by_buyable() 0 29 6
A calculatedBaseQuantity() 0 14 3
A calculatedBaseQuantities() 0 11 4
D workoutActualQuantity() 0 94 13

How to fix   Complexity   

Complex Class

Complex classes like BuyableStockCalculatedQuantity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BuyableStockCalculatedQuantity, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *@author: Nicolaas [at] Sunny Side Up . Co . Nz
4
 *@description:
5
 * works out the quantity available for each buyable
6
 * based on the the number of items sold, recorded in BuyableStockOrderEntry,
7
 * and manual corrections, recorded in BuyableStockManualUpdate.
8
 *
9
 *
10
 **/
11
12
class BuyableStockCalculatedQuantity extends DataObject
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...
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...
Unused Code introduced by
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
        "BaseQuantity" => "Int",
16
        "BuyableID" => "Int",
17
        "BuyableClassName" => "Varchar"
18
    );
19
20
    private static $has_many = 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...
Unused Code introduced by
The property $has_many 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...
21
        "BuyableStockOrderEntry" => "BuyableStockOrderEntry",
22
        "BuyableStockManualUpdate" => "BuyableStockManualUpdate"
23
    );
24
25
    private static $defaults = 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...
Unused Code introduced by
The property $defaults 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...
26
        "BaseQuantity" => 0
27
    );
28
29
    private static $casting = 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...
Unused Code introduced by
The property $casting 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...
30
        "Name" => "Varchar",
31
        "Buyable" => "DataObject",
32
        "UnlimitedStock" => "Boolean"
33
    );
34
35
    //MODEL ADMIN STUFF
36
    private static $searchable_fields = 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...
Unused Code introduced by
The property $searchable_fields 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...
37
        "BaseQuantity"
38
    );
39
40
    private static $field_labels = 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...
Unused Code introduced by
The property $field_labels 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...
41
        "BaseQuantity" => "Calculated Quantity On Hand",
42
        "BuyableID" => "Buyable ID",
43
        "LastEdited" => "Last Calculated"
44
    );
45
46
    private static $summary_fields = 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...
Unused Code introduced by
The property $summary_fields 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...
47
        "Name",
48
        "BaseQuantity",
49
        "LastEdited"
50
    );
51
52
    private static $indexes = 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...
Unused Code introduced by
The property $indexes 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...
53
        "BuyableClassName" => true,
54
        "BuyableID" => true,
55
        'BaseQuantity' => true
56
    );
57
58
    private static $default_sort = [
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...
Unused Code introduced by
The property $default_sort 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...
59
        'BuyableClassName' => 'ASC',
60
        'BaseQuantity' => 'DESC',
61
        'ID' => 'ASC'
62
    ];
63
64
    private static $singular_name = "Stock Calculated Quantity";
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...
Unused Code introduced by
The property $singular_name 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...
65
66
    private static $plural_name = "Stock Calculated Quantities";
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...
Unused Code introduced by
The property $plural_name 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...
67
68
    private static $calculation_done = array();
0 ignored issues
show
Unused Code introduced by
The property $calculation_done 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...
69
70
    public function canCreate($member = null)
71
    {
72
        return false;
73
    }
74
75
    public function canEdit($member = null)
76
    {
77
        return false;
78
    }
79
80
    public function canDelete($member = null)
81
    {
82
        return false;
83
    }
84
85
    public function canView($member = null)
86
    {
87
        return $this->canDoAnything();
88
    }
89
90
    public function Link($action = "update")
91
    {
92
        return "/update-stock/".$action."/".$this->ID."/";
93
    }
94
95
    public function HistoryLink()
96
    {
97
        return $this->Link("history");
98
    }
99
100
    public function Buyable()
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...
101
    {
102
        return $this->getBuyable();
103
    }
104
    public function getBuyable()
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...
105
    {
106
        if ($this->BuyableID && class_exists($this->BuyableClassName)) {
0 ignored issues
show
Documentation introduced by
The property BuyableID does not exist on object<BuyableStockCalculatedQuantity>. 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...
Bug introduced by
The property BuyableClassName does not seem to exist. Did you mean ClassName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107
            $className = $this->BuyableClassName;
0 ignored issues
show
Bug introduced by
The property BuyableClassName does not seem to exist. Did you mean ClassName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
            return $className::get()->byID($this->BuyableID);
0 ignored issues
show
Documentation introduced by
The property BuyableID does not exist on object<BuyableStockCalculatedQuantity>. 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...
109
        }
110
    }
111
112
    public function UnlimitedStock()
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...
113
    {
114
        return $this->geUnlimitedStock();
0 ignored issues
show
Bug introduced by
The method geUnlimitedStock() does not exist on BuyableStockCalculatedQuantity. Did you maybe mean UnlimitedStock()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
115
    }
116
    public function getUnlimitedStock()
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...
117
    {
118
        if ($buyable = $this->getBuyable()) {
119
            return $buyable->UnlimitedStock;
120
        }
121
    }
122
123
    public function Name()
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
        return $this->getName();
126
    }
127
    public function getName()
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...
128
    {
129
        if ($buyable = $this->getBuyable()) {
130
            return $buyable->getTitle();
131
        }
132
        return "no name";
133
    }
134
135
    protected function canDoAnything($member = null)
136
    {
137
        if ($buyable = $this->getBuyable()) {
138
            if ($buyable->canEdit($member)) {
139
                return true;
140
            }
141
        }
142
        Security::permissionFailure($this, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.'));
0 ignored issues
show
Documentation introduced by
$this is of type this<BuyableStockCalculatedQuantity>, but the function expects a object<Controller>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
    }
144
145
    public static function get_quantity_by_buyable($buyable)
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...
146
    {
147
        $value = 0;
148
        $item = self::get_by_buyable($buyable);
149
        if ($item) {
150
            $value = $item->calculatedBaseQuantity();
151
            if ($value < 0) {
152
                $value = 0;
153
            }
154
        }
155
        return $value;
156
    }
157
158
    public static function get_by_buyable($buyable)
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...
159
    {
160
        $obj = BuyableStockCalculatedQuantity::get()
161
                        ->filter(
162
                            array(
163
                                'BuyableID' => $buyable->ID,
164
                                'BuyableClassName' => $buyable->ClassName
165
                            )
166
                        )
167
                        ->First();
168
        if ($obj) {
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...
169
            //do nothing
170
        } else {
171
            $obj = new BuyableStockCalculatedQuantity();
172
            $obj->BuyableID = $buyable->ID;
0 ignored issues
show
Documentation introduced by
The property BuyableID does not exist on object<BuyableStockCalculatedQuantity>. 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...
173
            $obj->BuyableClassName = $buyable->ClassName;
0 ignored issues
show
Bug introduced by
The property BuyableClassName does not seem to exist. Did you mean ClassName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
174
        }
175
        if ($obj) {
176
            if (isset($obj->ID) && $obj->exists() && $obj->UnlimitedStock == $buyable->UnlimitedStock) {
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...
177
                //do nothing
178
            } else {
179
                $obj->UnlimitedStock = $buyable->UnlimitedStock;
180
                //we must write here to calculate quantities
181
                $obj->write();
182
            }
183
            return $obj;
184
        }
185
        user_error("Could not find / create BuyableStockCalculatedQuantity for buyable with ID / ClassName ".$buyableID."/".$buyableClassName, E_WARNING);
0 ignored issues
show
Bug introduced by
The variable $buyableID does not exist. Did you mean $buyable?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $buyableClassName does not exist. Did you mean $buyable?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
186
    }
187
188
    public function calculatedBaseQuantity()
189
    {
190
        if (!$this->ID) {
191
            return 0;
192
        }
193
        $actualQuantity = $this->workoutActualQuantity();
194
        if ($actualQuantity != $this->BaseQuantity) {
0 ignored issues
show
Documentation introduced by
The property BaseQuantity does not exist on object<BuyableStockCalculatedQuantity>. 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...
195
            $this->BaseQuantity = $actualQuantity;
0 ignored issues
show
Documentation introduced by
The property BaseQuantity does not exist on object<BuyableStockCalculatedQuantity>. 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...
196
            $this->write();
197
            return $actualQuantity;
198
        } else {
199
            return $this->getField("BaseQuantity");
200
        }
201
    }
202
203
    protected function calculatedBaseQuantities($buyables = null)
204
    {
205
        if ($buyables) {
206
            foreach ($buyables as $buyable) {
207
                $buyableStockCalculatedQuantity = BuyableStockCalculatedQuantity::get_by_buyable($buyable);
208
                if ($buyableStockCalculatedQuantity) {
209
                    $buyableStockCalculatedQuantity->calculatedBaseQuantity();
210
                }
211
            }
212
        }
213
    }
214
215
    /**
216
     * TODO: change to submitted from CustomerCanEdit criteria
217
     */
218
219
220
    protected function workoutActualQuantity()
0 ignored issues
show
Coding Style introduced by
workoutActualQuantity uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
221
    {
222
        $actualQuantity = 0;
223
        if ($buyable = $this->getBuyable()) {
0 ignored issues
show
Unused Code introduced by
$buyable 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...
224
            $query = Order::get()
225
                ->where('
226
                    "OrderItem"."BuyableID" = '.(intval($this->BuyableID) - 0).'
0 ignored issues
show
Documentation introduced by
The property BuyableID does not exist on object<BuyableStockCalculatedQuantity>. 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...
227
                    AND
228
                    "OrderItem"."BuyableClassName" = \''.$this->BuyableClassName.'\'
0 ignored issues
show
Bug introduced by
The property BuyableClassName does not seem to exist. Did you mean ClassName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
229
                    AND
230
                    "OrderStep"."CustomerCanEdit" = 0
231
                    AND
232
                    "Order"."ID" <> '.ShoppingCart::current_order()->ID.'
233
                ')
234
                ->innerJoin('OrderAttribute', '"OrderAttribute"."OrderID" = "Order"."ID"')
235
                ->innerJoin('OrderItem', '"OrderAttribute"."ID" = "OrderItem"."ID"')
236
                ->innerJoin('OrderStep', '"OrderStep"."ID" = "Order"."StatusID"');
237
            $amountPerOrder = array();
238
            if($query->count()) {
239
                foreach ($query as $row) {
240
                    if(!isset($amountPerOrder[$row->OrderID])) {
241
                        $amountPerOrder[$row->OrderID] = 0;
242
                    }
243
                    $amountPerOrder[$row->OrderID] += $row->Quantity;
244
                }
245
                foreach($amountPerOrder as $orderID => $sum) {
246
                    if ($orderID && $sum) {
247
                        $buyableStockOrderEntry = BuyableStockOrderEntry::get()
248
                            ->filter(
249
                                array(
250
                                    'OrderID' => $orderID,
251
                                    'ParentID' => $this->ID
252
                                )
253
                            )
254
                            ->First();
255
                        if ($buyableStockOrderEntry) {
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...
256
                            //do nothing
257
                        } else {
258
                            $buyableStockOrderEntry = new BuyableStockOrderEntry();
259
                            $buyableStockOrderEntry->OrderID = $orderID;
0 ignored issues
show
Documentation introduced by
The property OrderID does not exist on object<BuyableStockOrderEntry>. 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...
260
                            $buyableStockOrderEntry->ParentID = $this->ID;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<BuyableStockOrderEntry>. 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...
261
                            $buyableStockOrderEntry->IncludeInCurrentCalculation = 1;
0 ignored issues
show
Documentation introduced by
The property IncludeInCurrentCalculation does not exist on object<BuyableStockOrderEntry>. 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...
262
                            $buyableStockOrderEntry->Quantity = 0;
0 ignored issues
show
Documentation introduced by
The property Quantity does not exist on object<BuyableStockOrderEntry>. 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...
263
                        }
264
                        if ($buyableStockOrderEntry->Quantity != $sum) {
265
                            $buyableStockOrderEntry->Quantity = $sum;
266
                            $buyableStockOrderEntry->write();
267
                        }
268
                    }
269
                }
270
            }
271
            //find last adjustment
272
            $latestManualUpdate = BuyableStockManualUpdate::get()
273
                                                            ->filter(array('ParentID' => $this->ID))
274
                                                            ->sort(array('LastEdited' => 'DESC'))
275
                                                            ->First();
276
            //nullify order quantities that were entered before last adjustment
277
            if ($latestManualUpdate) {
278
                $latestManualUpdateQuantity = $latestManualUpdate->Quantity;
279
                DB::query("
280
                    UPDATE \"BuyableStockOrderEntry\"
281
                    SET \"IncludeInCurrentCalculation\" = 0
282
                    WHERE
283
                    \"LastEdited\" < '".$latestManualUpdate->LastEdited."'
284
                        AND
285
                        \"ParentID\" = ".$this->ID
286
                );
287
            } else {
288
                $latestManualUpdateQuantity = 0;
289
            }
290
            //work out additional purchases
291
            $orderQuantityToDeduct = BuyableStockOrderEntry::get()
292
                                        ->filter(
293
                                            array(
294
                                                'ParentID' => $this->ID,
295
                                                'IncludeInCurrentCalculation' => 1
296
                                            )
297
                                        )->sum('Quantity');
298
            if (!$orderQuantityToDeduct) {
299
                $orderQuantityToDeduct = 0;
300
            }
301
            //work out base total
302
            $actualQuantity = $latestManualUpdateQuantity - $orderQuantityToDeduct;
303
            if (isset($_GET["debug"])) {
304
                echo "<hr />";
305
                echo $this->Name;
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<BuyableStockCalculatedQuantity>. 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...
306
                echo " | Manual SUM: ".$latestManualUpdateQuantity;
307
                echo " | Order SUM: ".$orderQuantityToDeduct;
308
                echo " | Total SUM: ".$this->BaseQuantity;
0 ignored issues
show
Documentation introduced by
The property BaseQuantity does not exist on object<BuyableStockCalculatedQuantity>. 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...
309
                echo "<hr />";
310
            }
311
        }
312
        return $actualQuantity;
313
    }
314
}
315