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

flat::__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 flat {
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 = 'flat';
23
      $this->title = OSCOM::getDef('module_shipping_flat_text_title');
24
      $this->description = OSCOM::getDef('module_shipping_flat_text_description');
25
      $this->sort_order = defined('MODULE_SHIPPING_FLAT_SORT_ORDER') ? (int)MODULE_SHIPPING_FLAT_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_FLAT_TAX_CLASS') ? MODULE_SHIPPING_FLAT_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_FLAT_STATUS') && (MODULE_SHIPPING_FLAT_STATUS == 'True') ? true : false);
29
30
      if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_FLAT_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_FLAT_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;
52
53
      $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...
54
                            'module' => OSCOM::getDef('module_shipping_flat_text_title'),
55
                            'methods' => array(array('id' => $this->code,
56
                                                     'title' => OSCOM::getDef('module_shipping_flat_text_way'),
57
                                                     'cost' => MODULE_SHIPPING_FLAT_COST)));
58
59 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...
60
        $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
61
      }
62
63 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...
64
65
      return $this->quotes;
66
    }
67
68
    function check() {
69
      return defined('MODULE_SHIPPING_FLAT_STATUS');
70
    }
71
72 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...
73
      $OSCOM_Db = Registry::get('Db');
74
75
      $OSCOM_Db->save('configuration', [
76
        'configuration_title' => 'Enable Flat Shipping',
77
        'configuration_key' => 'MODULE_SHIPPING_FLAT_STATUS',
78
        'configuration_value' => 'True',
79
        'configuration_description' => 'Do you want to offer flat rate shipping?',
80
        'configuration_group_id' => '6',
81
        'sort_order' => '1',
82
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
83
        'date_added' => 'now()'
84
      ]);
85
86
      $OSCOM_Db->save('configuration', [
87
        'configuration_title' => 'Shipping Cost',
88
        'configuration_key' => 'MODULE_SHIPPING_FLAT_COST',
89
        'configuration_value' => '5.00',
90
        'configuration_description' => 'The shipping cost for all orders using this shipping method.',
91
        'configuration_group_id' => '6',
92
        'sort_order' => '1',
93
        'date_added' => 'now()'
94
      ]);
95
96
      $OSCOM_Db->save('configuration', [
97
        'configuration_title' => 'Tax Class',
98
        'configuration_key' => 'MODULE_SHIPPING_FLAT_TAX_CLASS',
99
        'configuration_value' => '0',
100
        'configuration_description' => 'Use the following tax class on the shipping fee.',
101
        'configuration_group_id' => '6',
102
        'sort_order' => '1',
103
        'use_function' => 'tep_get_tax_class_title',
104
        'set_function' => 'tep_cfg_pull_down_tax_classes(',
105
        'date_added' => 'now()'
106
      ]);
107
108
      $OSCOM_Db->save('configuration', [
109
        'configuration_title' => 'Shipping Zone',
110
        'configuration_key' => 'MODULE_SHIPPING_FLAT_ZONE',
111
        'configuration_value' => '0',
112
        'configuration_description' => 'If a zone is selected, only enable this shipping method for that zone.',
113
        'configuration_group_id' => '6',
114
        'sort_order' => '1',
115
        'use_function' => 'tep_get_zone_class_title',
116
        'set_function' => 'tep_cfg_pull_down_zone_classes(',
117
        'date_added' => 'now()'
118
      ]);
119
120
      $OSCOM_Db->save('configuration', [
121
        'configuration_title' => 'Sort Order',
122
        'configuration_key' => 'MODULE_SHIPPING_FLAT_SORT_ORDER',
123
        'configuration_value' => '0',
124
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
125
        'configuration_group_id' => '6',
126
        'sort_order' => '0',
127
        'date_added' => 'now()'
128
      ]);
129
    }
130
131
    function remove() {
132
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
133
    }
134
135
    function keys() {
136
      return array('MODULE_SHIPPING_FLAT_STATUS', 'MODULE_SHIPPING_FLAT_COST', 'MODULE_SHIPPING_FLAT_TAX_CLASS', 'MODULE_SHIPPING_FLAT_ZONE', 'MODULE_SHIPPING_FLAT_SORT_ORDER');
137
    }
138
  }
139
?>
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...
140