shoppingCart   D
last analyzed

Complexity

Total Complexity 104

Size/Duplication

Total Lines 119
Duplicated Lines 8.4 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 119
rs 4.8717
c 0
b 0
f 0
wmc 104
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A shoppingCart() 0 6 1
C add_cart() 5 18 21
B update_quantity() 5 11 16
A cleanup() 0 7 4
A in_cart() 0 7 2
B calculate() 0 55 9
A show_total() 0 5 1

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 shoppingCart 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 shoppingCart, and based on these observations, apply Extract Interface, too.

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\Registry;
10
11
  class shoppingCart {
12
    var $db, $contents, $total, $weight;
13
14
    function shoppingCart() {
15
      $this->db = Registry::get('Db');
16
17
      $this->contents = array();
18
      $this->total = 0;
19
    }
20
21
    function add_cart($products_id, $qty = '', $attributes = '') {
22
      $products_id = tep_get_uprid($products_id, $attributes);
23
24
      if ($this->in_cart($products_id)) {
25
        $this->update_quantity($products_id, $qty, $attributes);
26
      } else {
27
        if ($qty == '') $qty = '1'; // if no quantity is supplied, then add '1' to the customers basket
28
29
        $this->contents[$products_id] = array('qty' => $qty);
30
31 View Code Duplication
        if (is_array($attributes)) {
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...
32
          foreach( $attributes as $option => $value ) {
33
            $this->contents[$products_id]['attributes'][$option] = $value;
34
          }
35
        }
36
      }
37
      $this->cleanup();
38
    }
39
40
    function update_quantity($products_id, $quantity = '', $attributes = '') {
41
      if ($quantity == '') return true; // nothing needs to be updated if theres no quantity, so we return true..
42
43
      $this->contents[$products_id] = array('qty' => $quantity);
44
45 View Code Duplication
      if (is_array($attributes)) {
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...
46
        foreach( $attributes as $option => $value ) {
47
          $this->contents[$products_id]['attributes'][$option] = $value;
48
        }
49
      }
50
    }
51
52
    function cleanup() {
53
      foreach( array_keys($this->contents) as $key ) {
54
        if ($this->contents[$key]['qty'] < 1) {
55
          unset($this->contents[$key]);
56
        }
57
      }
58
    }
59
60
    function in_cart($products_id) {
61
      if (isset($this->contents[$products_id])) {
62
        return true;
63
      } else {
64
        return false;
65
      }
66
    }
67
68
    function calculate() {
69
      $this->total = 0;
70
      $this->weight = 0;
71
      if (!is_array($this->contents)) return 0;
72
73
      foreach ( array_keys($this->contents) as $products_id ) {
74
        $qty = $this->contents[$products_id]['qty'];
75
76
// products price
77
        $Qproduct = $this->db->get('products', [
78
          'products_id',
79
          'products_price',
80
          'products_tax_class_id',
81
          'products_weight'
82
        ], [
83
          'products_id' => (int)tep_get_prid($products_id)
84
        ]);
85
86
        if ($Qproduct->fetch() !== false) {
87
          $prid = $Qproduct->valueInt('products_id');
88
          $products_tax = tep_get_tax_rate($Qproduct->valueInt('products_tax_class_id'));
89
          $products_price = $Qproduct->value('products_price');
90
          $products_weight = $Qproduct->value('products_weight');
91
92
          $Qspecials = $this->db->get('specials', 'specials_new_products_price', ['products_id' => $prid, 'status' => '1']);
93
94
          if ($Qspecials->fetch() !== false) {
95
            $products_price = $Qspecials->value('specials_new_products_price');
96
          }
97
98
          $this->total += tep_add_tax($products_price, $products_tax) * $qty;
99
          $this->weight += ($qty * $products_weight);
100
101
// attributes price
102
          if (isset($this->contents[$products_id]['attributes'])) {
103
            foreach ( $this->contents[$products_id]['attributes'] as $option => $value ) {
104
              $Qattribute = $this->db->get('products_attributes', [
105
                'options_values_price',
106
                'price_prefix'
107
              ], [
108
                'products_id' => $prid,
109
                'options_id' => (int)$option,
110
                'options_values_id' => (int)$value
111
              ]);
112
113
              if ($Qattribute->value('price_prefix') == '+') {
114
                $this->total += $qty * tep_add_tax($Qattribute->value('options_values_price'), $products_tax);
115
              } else {
116
                $this->total -= $qty * tep_add_tax($Qattribute->value('options_values_price'), $products_tax);
117
              }
118
            }
119
          }
120
        }
121
      }
122
    }
123
124
    function show_total() {
125
      $this->calculate();
126
127
      return $this->total;
128
    }
129
  }
130
?>
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...
131