CountryPrice_TestController   F
last analyzed

Complexity

Total Complexity 88

Size/Duplication

Total Lines 502
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 88
lcom 1
cbo 19
dl 49
loc 502
rs 2
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A index() 10 30 5
A Link() 0 4 1
B addproducts() 0 23 8
A resetmycountry() 0 10 2
A setmycountry() 0 20 2
A resetSessionVars() 0 7 1
A currencypercountry() 0 25 5
A countrieswithoutdistributor() 0 12 3
A stockistcountrieswithoutcountry() 0 9 2
C whoami() 0 86 13
A MyCurrency() 0 4 1
A MyCountryCode() 0 4 1
A MyBackupCountryCode() 0 4 1
A MyCountryTitle() 0 5 1
A MyDefaultDistributor() 0 4 1
A MyDeliveryCostNote() 13 13 4
A MyShippingDeliveryInfo() 13 13 4
A MyDistributor() 0 6 1
A MyShippingReturnInfo() 13 13 4
A MyProductNotAvailableNote() 0 16 5
C MyStockistCountryPage() 0 45 12
A MyDistributorCountry() 0 5 1
A StockistSearchPage() 0 4 1
A MyStockistSearchPage() 0 19 4
A MyCountryFAQPage() 0 7 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CountryPrice_TestController 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 CountryPrice_TestController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
4
class CountryPrice_TestController extends ContentController
5
{
6
    private static $allowed_actions = 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...
7
        "whoami" => true,
8
        "addproducts" => true,
9
        "setmycountry" => true,
10
        "resetmycountry" => true,
11
        "currencypercountry" => true,
12
        "countrieswithoutdistributor" => true,
13
        "stockistcountrieswithoutcountry" => true
14
    );
15
16
    public function init()
17
    {
18
        parent::init();
19
        if (! Permission::check('ADMIN')) {
20
            return Security::permissionFailure($this, 'This page is secured and you need administrator rights to access it. You can also save the page in the CMS to get 15 minutes access without being logged in.');
21
        }
22
    }
23
24
    public function index()
25
    {
26
        echo "<hr />";
27
        echo "<hr />";
28
        echo "<hr />";
29
30
        foreach (self::$allowed_actions as $action => $notNeeded) {
31
            if (!in_array($action, array("setmycountry"))) {
32
                DB::alteration_message("<a href=\"".$this->Link($action)."\">$action</a>");
33
            }
34
        }
35
        DB::alteration_message("<a href=\"/dev/ecommerce/\">All E-commerce Tools</a>");
36
        echo "<hr />";
37
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list();
38
        $countryLinks = array();
39 View Code Duplication
        foreach ($countries as $country) {
0 ignored issues
show
Duplication introduced by
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...
40
            $code = $country->Code;
41
            $name = $country->Name;
42
            $countryLinks[] = "<a href=\"".$this->Link("setmycountry/$code/?countryfortestingonly=$code")."\">$name</a>";
43
        }
44
        DB::alteration_message("Available Countries: ".implode(", ", $countryLinks));
45
        $countries = EcommerceCountry::get()->exclude(array('Code' => $countries->column('Code')));
46
        $countryLinks = array();
47 View Code Duplication
        foreach ($countries as $country) {
0 ignored issues
show
Duplication introduced by
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...
48
            $code = $country->Code;
49
            $name = $country->Name;
50
            $countryLinks[] = "<a href=\"".$this->Link("setmycountry/$code/?countryfortestingonly=$code")."\">$name</a>";
51
        }
52
        DB::alteration_message("Other Countries: ".implode(", ", $countryLinks));
53
    }
54
55
56
    public function Link($action = null)
57
    {
58
        return "/distributors-test/".$action;
59
    }
60
61
    public function addproducts()
62
    {
63
        $sc = ShoppingCart::singleton();
64
        $count = 1;
65
        $products = Product::get()->filter("AllowPurchase", 1)->limit(20)->Sort("Rand()");
66
        foreach ($products as $product) {
67
            if ($product->canPurchase() && $count < 3) {
68
                $count++;
69
                $sc->addBuyable($product, rand(1, 5));
70
            }
71
        }
72
        $count = 1;
73
        if (class_exists('ProductVariation')) {
74
            $productVariations = ProductVariation::get()->filter("AllowPurchase", 1)->limit(20)->Sort("Rand()");
75
            foreach ($productVariations as $productVariation) {
76
                if ($productVariation->canPurchase() && $count < 3) {
77
                    $count++;
78
                    $sc->addBuyable($productVariation, rand(1, 5));
79
                }
80
            }
81
        }
82
        return $this->redirect($sc->Link());
83
    }
84
85
86
    public function resetmycountry($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
87
    {
88
        $this->resetSessionVars();
89
        $o = ShoppingCart::current_order();
90
        if ($o) {
91
            $o->delete();
92
        }
93
        DB::alteration_message("Cleared countries");
94
        return $this->redirect("/dev/ecommerce/ecommercetaskcartmanipulation_current");
95
    }
96
97
98
    public function setmycountry($request)
99
    {
100
        $value = strtoupper(Convert::raw2sql($request->param("ID")));
101
        if ($o = ShoppingCart::current_order()) {
102
            $this->resetSessionVars();
103
            $o->SetCountryFields($value);
104
            $o->CurrencyCountry = $value;
105
            $o->OriginatingCountryCode = $value;
106
            $o->write();
107
            $o->SetCountryFields($value);
108
            $o->CurrencyCountry = $value;
109
            $o->OriginatingCountryCode = $value;
110
            $o->write();
111
            CountryPrice_OrderDOD::localise_order();
112
            CountryPrice_OrderDOD::localise_order();
113
            CountryPrice_OrderDOD::localise_order();
114
            return $this->redirectBack('/dev/ecommerce/ecommercetaskcartmanipulation_current/');
0 ignored issues
show
Unused Code introduced by
The call to CountryPrice_TestController::redirectBack() has too many arguments starting with '/dev/ecommerce/ecommerc...tmanipulation_current/'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
        }
116
        user_error("There is no cart available.");
117
    }
118
119
    private function resetSessionVars()
120
    {
121
        Session::clear("countryfortestingonly");
122
        Session::clear("MyCloudFlareCountry");
123
        Session::clear("ipfortestingonly");
124
        Session::save();
125
    }
126
127
    public function currencypercountry($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
128
    {
129
        $currencies = CountryPrice_EcommerceCurrency::get_currency_per_country();
130
        $usedCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country_used_ones();
131
        $useArray = array();
132
        $nonUseArray = array();
133
        foreach ($currencies as $country => $currency) {
134
            $countryName = EcommerceCountry::find_title($country);
135
            $currencyObject = EcommerceCurrency::create_new($currency);
136
            if (isset($usedCurrencies[$country])) {
137
                $useArray[] = "$country: $currency ($countryName, ".$currencyObject->getTitle().")";
138
            } else {
139
                $nonUseArray[] = "$country: $currency ($countryName, ".$currencyObject->getTitle().")";
140
            }
141
        }
142
        echo "<h2>Countries ready for sale with their currency:</h2>";
143
        foreach ($useArray as $line) {
144
            DB::alteration_message($line);
145
        }
146
        echo "<h2>Countries NOT ready for sale with their currency:</h2>";
147
        foreach ($nonUseArray as $line) {
148
            DB::alteration_message($line);
149
        }
150
        return $this->index();
151
    }
152
153
    public function countrieswithoutdistributor($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
154
    {
155
        $list = EcommerceCountry::get()->filter(array("DistributorID" => 0));
156
        if (!$list->count()) {
157
            DB::alteration_message('All countries have distributors', 'created');
158
        } else {
159
            foreach ($list as $country) {
160
                DB::alteration_message("<a href=\"".$country->CMSEditLink()."\">".$country->Code." ".$country->Name."</a>");
161
            }
162
        }
163
        return $this->index();
164
    }
165
166
    public function stockistcountrieswithoutcountry($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
167
    {
168
        $mainPage = StockistSearchPage::get()->first();
169
        $list = StockistCountryPage::get()->where("Country = '' OR Country IS NULL")->exclude(array("ParentID" => $mainPage->ID));
170
        foreach ($list as $stockistCountry) {
171
            DB::alteration_message("<a href=\"".$stockistCountry->CMSEditLink()."\">".$stockistCountry->Title."</a>");
172
        }
173
        return $this->index();
174
    }
175
176
177
    public function whoami()
178
    {
179
        $descriptionArray = array(
180
            "MyCountryCode" => "This is the key function",
181
            "MyCountryTitle" => "",
182
            "MyCurrency" => "Based on the currency set in the order.",
183
            "MyDistributor" => "You have to set the ". _t('Distributor.SINGULAR_NAME', 'Distributor') . "'s countries in order to work out someone's ". _t('Distributor.SINGULAR_NAME', 'Distributor') . ".",
184
            "MyDistributorCountry" => "This is the country that is being used for the sale.  For dodgy countries, we use the backup country.",
185
            "MyDeliveryCostNote" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.",
186
            "MyShippingDeliveryInfo" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.",
187
            "MyShippingReturnInfo" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.",
188
            "MyProductNotAvailableNote" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . ", default country, AND the Ecommerce Config.",
189
            "MyStockistSearchPage" => "",
190
            "MyStockistCountryPage" => "The stockist page that is related to the visitor's country",
191
            "MyCountryFAQPage" => "",
192
            "MyBackupCountryCode" => "This country is used if the information for the selected country is not available.",
193
            "MyDefaultDistributor" => "You can set one default ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " (head office) with a tickbox for any ". _t('Distributor.SINGULAR_NAME', 'Distributor') ,
194
195
        );
196
        $array = array(
197
            "MyCountryCode" => "String",
198
            "MyCountryTitle" => "String",
199
            "MyCurrency" => "Object",
200
            "MyDistributor" => "Object",
201
            "MyDistributorCountry" => "Object",
202
            "MyDeliveryCostNote" => "String",
203
            "MyShippingDeliveryInfo" => "String",
204
            "MyShippingReturnInfo" => "String",
205
            "MyProductNotAvailableNote" => "String",
206
            "MyStockistSearchPage" => "Object",
207
            "MyStockistCountryPage" => "Object",
208
            "MyCountryFAQPage" => "Object",
209
            "MyBackupCountryCode" => "String",
210
            "MyDefaultDistributor" => "Object",
211
        );
212
        echo "<h1>Current Settings</h1>";
213
        echo "<p>Most of these settings can be adjusted in the country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and generic e-commerce settings.</p>";
214
        foreach ($array as $name => $type) {
215
            $style = "created";
216
            $notSet = false;
0 ignored issues
show
Unused Code introduced by
$notSet 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...
217
            $string = "";
0 ignored issues
show
Unused Code introduced by
$string 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...
218
            if ($type == "String") {
219
                $string = $this->$name();
220
                if (is_object($string)) {
221
                    $string = $string->raw();
222
                }
223
            } else {
224
                $obj = $this->$name();
225
                if (!$obj) {
226
                    $string = "Object Not Found";
227
                    $notSet = true;
0 ignored issues
show
Unused Code introduced by
$notSet 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...
228
                    $style = "deleted";
229
                } else {
230
                    switch ($name) {
231
                        case "MyDistributorCountry":
232
                        case "MyCountryFAQPage":
233
                        case "MyDefaultDistributor":
234
                        case "MyDistributor":
235
                            $string = $obj->Name;
236
                            break;
237
238
                        case "MyStockistSearchPage":
239
                        case "MyStockistCountryPage":
240
                            $string = $obj->Title;
241
                            break;
242
                        default:
243
                            $string = print_r($obj, 1);
244
                    }
245
                }
246
            }
247
            if (! $string) {
248
                $string = "NOT SET";
249
                $style = "deleted";
250
            }
251
            DB::alteration_message("<i>$name</i>: <u><strong>".$string."</strong></u> ... <br /><sup>".$descriptionArray[$name]."</sup><hr />", $style);
252
        }
253
        echo "<h1>Change Settings</h1>";
254
        $links = array(
255
            "resetmycountry/" => "Go back to the standard country",
256
        );
257
        $links["/dev/tasks/TEST_GEOIP_COUNTRY_CODE_BY_NAME"] = "test GEOIP function";
258
        foreach ($links as $link => $desc) {
259
            DB::alteration_message("<a href=\"".$this->Link($link)."\">".$desc."</a>");
260
        }
261
        return $this->index();
262
    }
263
264
265
    /**
266
     *
267
     * @return String
268
     */
269
    public function MyCurrency()
270
    {
271
        return ShoppingCart::current_order()->CurrencyUsed()->Name;
272
    }
273
274
    /**
275
     *
276
     * @return String
277
     */
278
    public function MyCountryCode()
279
    {
280
        return EcommerceCountry::get_country();
281
    }
282
283
    /**
284
     *
285
     * @return String
286
     */
287
    public function MyBackupCountryCode()
288
    {
289
        return CountryPrice_EcommerceCountry::get_backup_country()->Code;
290
    }
291
292
    /**
293
     * @return String
294
     */
295
    public function MyCountryTitle()
296
    {
297
        $code = EcommerceCountry::get_country();
298
        return EcommerceCountry::find_title($code);
299
    }
300
301
    public function MyDefaultDistributor()
302
    {
303
        return Distributor::get_default_distributor();
304
    }
305
306
307
308
    /**
309
     * returns note about Delivery Cost
310
     * Needs to be in model so we can access from the order in the template.
311
     * @return Varchar Field
312
     */
313 View Code Duplication
    public function MyDeliveryCostNote()
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...
314
    {
315
        $note = EcommerceCountry::get_country_object()->DeliveryCostNote;
316
        if (!$note) {
317
            if ($distributor = $this->MyDistributor()) {
318
                $note = $distributor->DeliveryCostNote;
0 ignored issues
show
Documentation introduced by
The property DeliveryCostNote does not exist on object<Distributor>. 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...
319
            }
320
            if (!$note) {
321
                $note = CountryPrice_EcommerceCountry::get_backup_country()->DeliveryCostNote;
322
            }
323
        }
324
        return DBField::create_field("Varchar", $note);
325
    }
326
327
    /**
328
     * returns note about Shipping Estimation Time
329
     * Needs to be in model so we can access from the order in the template.
330
     * @return Varchar Field
331
     */
332 View Code Duplication
    public function MyShippingDeliveryInfo()
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...
333
    {
334
        $info = EcommerceCountry::get_country_object()->ShippingEstimation;
335
        if (!$info) {
336
            if ($distributor = $this->MyDistributor()) {
337
                $info = $distributor->ShippingEstimation;
0 ignored issues
show
Documentation introduced by
The property ShippingEstimation does not exist on object<Distributor>. 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...
338
            }
339
            if (!$info) {
340
                $info = CountryPrice_EcommerceCountry::get_backup_country()->ShippingEstimation;
341
            }
342
        }
343
        return DBField::create_field("Varchar", $info);
344
    }
345
346
    /**
347
     * returns the Distributor for the user
348
     * Needs to be in model so we can access from the order in the template.
349
     * @return Varchar Field
0 ignored issues
show
Documentation introduced by
Should the return type not be Distributor?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
350
     */
351
    public function MyDistributor()
352
    {
353
        $code = EcommerceCountry::get_country();
354
        $distributor = Distributor::get_one_for_country($code);
355
        return $distributor;
356
    }
357
358
    /**
359
     * returns note about shipping return information
360
     * Needs to be in model so we can access from the order in the template.
361
     * @return Varchar Field
362
     */
363 View Code Duplication
    public function MyShippingReturnInfo()
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...
364
    {
365
        $info = EcommerceCountry::get_country_object()->ReturnInformation;
366
        if (!$info) {
367
            if ($distributor = $this->MyDistributor()) {
368
                $info = $distributor->ReturnInformation;
0 ignored issues
show
Documentation introduced by
The property ReturnInformation does not exist on object<Distributor>. 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...
369
            }
370
            if (!$info) {
371
                $info = CountryPrice_EcommerceCountry::get_backup_country()->ReturnInformation;
372
            }
373
        }
374
        return DBField::create_field("Varchar", $info);
375
    }
376
377
378
    /**
379
     * returns note to show when product / variation is not available in the specific country
380
     * Needs to be in model so we can access from the order in the template.
381
     * @return Varchar Field
382
     */
383
    public function MyProductNotAvailableNote()
384
    {
385
        $note = EcommerceCountry::get_country_object()->ProductNotAvailableNote;
386
        if (!$note) {
387
            if ($distributor = $this->MyDistributor()) {
388
                $note = $distributor->ProductNotAvailableNote;
0 ignored issues
show
Documentation introduced by
The property ProductNotAvailableNote does not exist on object<Distributor>. 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...
389
            }
390
            if (!$note) {
391
                $note = CountryPrice_EcommerceCountry::get_backup_country()->ProductNotAvailableNote;
392
            }
393
            if (!$note) {
394
                $note = $this->EcomConfig()->NotForSaleMessage;
0 ignored issues
show
Bug introduced by
The method EcomConfig() does not exist on CountryPrice_TestController. Did you maybe mean config()?

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...
395
            }
396
        }
397
        return DBField::create_field("HTMLText", $note);
398
    }
399
400
401
    /**
402
     * provides information on where to buy a product
403
     * if it can not be bought online...
404
     * @return StockistCountryPage | Null
405
     */
406
    public function MyStockistCountryPage($attempts = 0)
407
    {
408
        $exit = false;
409
        switch ($attempts) {
410
            case 0:
411
                $countryCode = EcommerceCountry::get_country();
412
                break;
413
            case 1:
414
                $countryCode = CountryPrice_EcommerceCountry::get_distributor_country();
415
                break;
416
            case 2:
417
                 $countryCode = EcommerceConfig::get('EcommerceCountry', 'default_country_code');
418
                break;
419
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
420
            default:
421
                $countryCode = "NZ";
422
                $exit = true;
423
        }
424
        if ($countryCode) {
425
            $countryStockistPage = StockistCountryPage::get()->filter(array("Country" => $countryCode))->first();
426
            if (!$countryStockistPage) {
427
                if ($countryObject = DataObject::get_one("EcommerceCountry", "Code = '$countryCode'")) {
428
                    $rows = DB::query(
429
                        "
430
                        SELECT \"StockistCountryPageID\"
431
                        FROM \"StockistCountryPage_AdditionalCountries\"
432
                        WHERE \"EcommerceCountryID\" = ".$countryObject->ID
433
                    );
434
                    if ($rows) {
435
                        foreach ($rows as $row) {
436
                            $countryStockistPage = StockistCountryPage::get()->byID($rows["StockistCountryPageID"]);
437
                            if ($countryStockistPage) {
438
                                break;
439
                            }
440
                        }
441
                    }
442
                }
443
            }
444
            if ($countryStockistPage || $exit) {
445
                return $countryStockistPage;
446
            }
447
        }
448
        $attempts++;
449
        return $this->MyStockistCountryPage($attempts);
450
    }
451
452
    /**
453
     *
454
     * @return EcommerceCountry
455
     */
456
    public function MyDistributorCountry()
457
    {
458
        return CountryPrice_EcommerceCountry::get_distributor_country();
459
        return $countryObject;
0 ignored issues
show
Unused Code introduced by
return $countryObject; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
460
    }
461
462
    /**
463
     *
464
     * @return StockistSearchPage
465
     */
466
    public function StockistSearchPage()
467
    {
468
        return StockistSearchPage::get()->filter(array("ClassName" => "StockistSearchPage"))->first();
469
    }
470
471
472
    public function MyStockistSearchPage()
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...
473
    {
474
        $country = EcommerceCountry::get_country();
475
        $countryPage = StockistCountryPage::get()->filter(array("Country" => $country))->First();
476
        $sql = "
477
            SELECT StockistCountryPageID
478
            FROM StockistCountryPage_AdditionalCountries
479
            WHERE EcommerceCountryID = '".EcommerceCountry::get_country_id($country)."'";
480
        $idForPage = DB::query($sql)->value();
481
        if ($idForPage) {
482
            StockistCountryPage::get()->byID($idForPage);
483
        }
484
        if (!$countryPage) {
485
            if (!$countryPage) {
486
                $countryPage = $this->StockistSearchPage();
487
            }
488
        }
489
        return $countryPage;
490
    }
491
492
    /**
493
     *
494
     * returns the DistributorFAQPage ONLY if Specific Content has been entered
495
     * for the country.
496
     * @return DistributorFAQPage
497
     */
498
    public function MyCountryFAQPage()
499
    {
500
        $country = $this->MyDistributorCountry();
501
        if ($country && $country->FAQContent) {
502
            return DistributorFAQPage::get()->First();
503
        }
504
    }
505
}
506