Completed
Push — master ( cb6ae8...5ae7ff )
by Harald
03:13
created

table::__construct()   D

Complexity

Conditions 11
Paths 144

Size

Total Lines 31
Code Lines 22

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 144
nop 0
dl 31
loc 31
rs 4.9629
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
  use OSC\OM\HTML;
10
  use OSC\OM\OSCOM;
11
  use OSC\OM\Registry;
12
13
  class table {
14
    var $code, $title, $description, $icon, $enabled;
15
16
// class constructor
17 View Code Duplication
    function __construct() {
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...
18
      global $order;
19
20
      $OSCOM_Db = Registry::get('Db');
21
22
      $this->code = 'table';
23
      $this->title = OSCOM::getDef('module_shipping_table_text_title');
24
      $this->description = OSCOM::getDef('module_shipping_table_text_description');
25
      $this->sort_order = defined('MODULE_SHIPPING_TABLE_SORT_ORDER') ? (int)MODULE_SHIPPING_TABLE_SORT_ORDER : 0;
0 ignored issues
show
Bug introduced by
The property sort_order does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
      $this->icon = '';
27
      $this->tax_class = defined('MODULE_SHIPPING_TABLE_TAX_CLASS') ? MODULE_SHIPPING_TABLE_TAX_CLASS : 0;
0 ignored issues
show
Bug introduced by
The property tax_class does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
      $this->enabled = (defined('MODULE_SHIPPING_TABLE_STATUS') && (MODULE_SHIPPING_TABLE_STATUS == 'True') ? true : false);
29
30
      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_TABLE_ZONE > 0) ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
31
        $check_flag = false;
32
        $Qcheck = $OSCOM_Db->get('zones_to_geo_zones', 'zone_id', ['geo_zone_id' => MODULE_SHIPPING_TABLE_ZONE, 'zone_country_id' => $order->delivery['country']['id']], 'zone_id');
33
        while ($Qcheck->fetch()) {
34
          if ($Qcheck->valueInt('zone_id') < 1) {
35
            $check_flag = true;
36
            break;
37
          } elseif ($Qcheck->valueInt('zone_id') == $order->delivery['zone_id']) {
38
            $check_flag = true;
39
            break;
40
          }
41
        }
42
43
        if ($check_flag == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
44
          $this->enabled = false;
45
        }
46
      }
47
    }
48
49
// class methods
50
    function quote($method = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $method 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...
51
      global $order, $shipping_weight, $shipping_num_boxes;
52
53
      if (MODULE_SHIPPING_TABLE_MODE == 'price') {
54
        $order_total = $this->getShippableTotal();
55
      } else {
56
        $order_total = $shipping_weight;
57
      }
58
59
      $table_cost = preg_split("/[:,]/" , MODULE_SHIPPING_TABLE_COST);
60
      $size = sizeof($table_cost);
61
      for ($i=0, $n=$size; $i<$n; $i+=2) {
62
        if ($order_total <= $table_cost[$i]) {
63
          $shipping = $table_cost[$i+1];
64
          break;
65
        }
66
      }
67
68
      if (MODULE_SHIPPING_TABLE_MODE == 'weight') {
69
        $shipping = $shipping * $shipping_num_boxes;
0 ignored issues
show
Bug introduced by
The variable $shipping 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...
70
      }
71
72
      $this->quotes = array('id' => $this->code,
0 ignored issues
show
Bug introduced by
The property quotes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
                            'module' => OSCOM::getDef('module_shipping_table_text_title'),
74
                            'methods' => array(array('id' => $this->code,
75
                                                     'title' => OSCOM::getDef('module_shipping_table_text_way'),
76
                                                     'cost' => $shipping + MODULE_SHIPPING_TABLE_HANDLING)));
77
78 View Code Duplication
      if ($this->tax_class > 0) {
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...
79
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
80
      }
81
82 View Code Duplication
      if (tep_not_null($this->icon)) $this->quotes['icon'] = HTML::image($this->icon, $this->title);
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...
83
84
      return $this->quotes;
85
    }
86
87
    function check() {
88
      return defined('MODULE_SHIPPING_TABLE_STATUS');
89
    }
90
91 View Code Duplication
    function install() {
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...
92
      $OSCOM_Db = Registry::get('Db');
93
94
      $OSCOM_Db->save('configuration', [
95
        'configuration_title' => 'Enable Table Method',
96
        'configuration_key' => 'MODULE_SHIPPING_TABLE_STATUS',
97
        'configuration_value' => 'True',
98
        'configuration_description' => 'Do you want to offer table rate shipping?',
99
        'configuration_group_id' => '6',
100
        'sort_order' => '1',
101
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
102
        'date_added' => 'now()'
103
      ]);
104
105
      $OSCOM_Db->save('configuration', [
106
        'configuration_title' => 'Shipping Table',
107
        'configuration_key' => 'MODULE_SHIPPING_TABLE_COST',
108
        'configuration_value' => '25:8.50,50:5.50,10000:0.00',
109
        'configuration_description' => 'The shipping cost is based on the total cost or weight of items. Example: 25:8.50,50:5.50,etc.. Up to 25 charge 8.50, from there to 50 charge 5.50, etc',
110
        'configuration_group_id' => '6',
111
        'sort_order' => '1',
112
        'date_added' => 'now()'
113
      ]);
114
115
      $OSCOM_Db->save('configuration', [
116
        'configuration_title' => 'Table Method',
117
        'configuration_key' => 'MODULE_SHIPPING_TABLE_MODE',
118
        'configuration_value' => 'weight',
119
        'configuration_description' => 'The shipping cost is based on the order total or the total weight of the items ordered.',
120
        'configuration_group_id' => '6',
121
        'sort_order' => '1',
122
        'set_function' => 'tep_cfg_select_option(array(\'weight\', \'price\'), ',
123
        'date_added' => 'now()'
124
      ]);
125
126
      $OSCOM_Db->save('configuration', [
127
        'configuration_title' => 'Handling Fee',
128
        'configuration_key' => 'MODULE_SHIPPING_TABLE_HANDLING',
129
        'configuration_value' => '0',
130
        'configuration_description' => 'Handling fee for this shipping method.',
131
        'configuration_group_id' => '6',
132
        'sort_order' => '1',
133
        'date_added' => 'now()'
134
      ]);
135
136
      $OSCOM_Db->save('configuration', [
137
        'configuration_title' => 'Tax Class',
138
        'configuration_key' => 'MODULE_SHIPPING_TABLE_TAX_CLASS',
139
        'configuration_value' => '0',
140
        'configuration_description' => 'Use the following tax class on the shipping fee.',
141
        'configuration_group_id' => '6',
142
        'sort_order' => '1',
143
        'use_function' => 'tep_get_tax_class_title',
144
        'set_function' => 'tep_cfg_pull_down_tax_classes(',
145
        'date_added' => 'now()'
146
      ]);
147
148
      $OSCOM_Db->save('configuration', [
149
        'configuration_title' => 'Shipping Zone',
150
        'configuration_key' => 'MODULE_SHIPPING_TABLE_ZONE',
151
        'configuration_value' => '0',
152
        'configuration_description' => 'If a zone is selected, only enable this shipping method for that zone.',
153
        'configuration_group_id' => '6',
154
        'sort_order' => '1',
155
        'use_function' => 'tep_get_zone_class_title',
156
        'set_function' => 'tep_cfg_pull_down_zone_classes(',
157
        'date_added' => 'now()'
158
      ]);
159
160
      $OSCOM_Db->save('configuration', [
161
        'configuration_title' => 'Sort Order',
162
        'configuration_key' => 'MODULE_SHIPPING_TABLE_SORT_ORDER',
163
        'configuration_value' => '0',
164
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
165
        'configuration_group_id' => '6',
166
        'sort_order' => '0',
167
        'date_added' => 'now()'
168
      ]);
169
    }
170
171
    function remove() {
172
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
173
    }
174
175
    function keys() {
176
      return array('MODULE_SHIPPING_TABLE_STATUS', 'MODULE_SHIPPING_TABLE_COST', 'MODULE_SHIPPING_TABLE_MODE', 'MODULE_SHIPPING_TABLE_HANDLING', 'MODULE_SHIPPING_TABLE_TAX_CLASS', 'MODULE_SHIPPING_TABLE_ZONE', 'MODULE_SHIPPING_TABLE_SORT_ORDER');
177
    }
178
179
    function getShippableTotal() {
180
      global $order, $currencies;
181
182
      $OSCOM_Db = Registry::get('Db');
183
184
      $order_total = $_SESSION['cart']->show_total();
185
186
      if ($order->content_type == 'mixed') {
187
        $order_total = 0;
188
189
        for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
190
          $order_total += $currencies->calculate_price($order->products[$i]['final_price'], $order->products[$i]['tax'], $order->products[$i]['qty']);
191
192
          if (isset($order->products[$i]['attributes'])) {
193
            foreach ( $order->products[$i]['attributes'] as $option => $value ) {
194
              $Qcheck = $OSCOM_Db->prepare('select pa.products_id from :table_products_attributes pa, :table_products_attributes_download pad where pa.products_id = :products_id and pa.options_values_id = :options_values_id and pa.products_attributes_id = pad.products_attributes_id');
195
              $Qcheck->bindInt(':products_id', $order->products[$i]['id']);
196
              $Qcheck->bindInt(':options_values_id', $value['value_id']);
197
              $Qcheck->execute();
198
199
              if ($Qcheck->fetch() !== false) {
200
                $order_total -= $currencies->calculate_price($order->products[$i]['final_price'], $order->products[$i]['tax'], $order->products[$i]['qty']);
201
              }
202
            }
203
          }
204
        }
205
      }
206
207
      return $order_total;
208
    }
209
  }
210
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
211