EcommerceTaskDebugCart::debug_object()   C
last analyzed

Complexity

Conditions 12
Paths 8

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
dl 0
loc 53
rs 6.9666
c 0
b 0
f 0
nc 8
nop 1

How to fix   Long Method    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
4
class EcommerceTaskDebugCart extends BuildTask
5
{
6
    protected $title = 'Debug your cart';
7
8
    protected $description = 'Check all the values in your cart to find any potential errors.';
9
10
    public function run($request)
11
    {
12
        $order = ShoppingCart::current_order();
13
        self::debug_object($order);
14
    }
15
16
    public static function debug_object($obj)
17
    {
18
        $html = '
19
			<h2>'.$obj->ClassName.'</h2><ul>';
20
        $fields = Config::inst()->get($obj->ClassName, 'db', Config::INHERITED);
21
22
        //db
23
        if (count($fields)) {
24
            foreach ($fields as  $key => $type) {
0 ignored issues
show
Bug introduced by
The expression $fields of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
25
                $value = self::cleanup_value($type, $obj->$key);
26
                $html .= "<li><b>$key ($type):</b> ".$value.'</li>';
27
            }
28
        }
29
30
        //casted variables
31
        $fields = Config::inst()->get($obj->ClassName, 'casting', Config::FIRST_SET);
32
        if (count($fields)) {
33
            foreach ($fields as  $key => $type) {
0 ignored issues
show
Bug introduced by
The expression $fields of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
34
                $method = $key;
35
                if ($obj->hasMethod($method)) {
36
                    $value = $obj->$method();
37
                } else {
38
                    $method = 'get'.$key;
39
                    if ($obj->hasMethod($method)) {
40
                        $value = $obj->$method();
41
                    } else {
42
                        $value = $obj->$key;
43
                    }
44
                }
45
                $value = self::cleanup_value($type, $value);
46
                $html .= "<li><b>$key ($type):</b> ".$value.'</li>';
47
            }
48
        }
49
50
        //has_one
51
        $fields = Config::inst()->get($obj->ClassName, 'has_one', Config::FIRST_SET);
52
        if (count($fields)) {
53
            foreach ($fields as  $key => $type) {
0 ignored issues
show
Bug introduced by
The expression $fields of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
54
                $value = '';
55
                $field = $key.'ID';
56
                if ($object = $obj->$key()) {
57
                    if ($object && $object->exists()) {
58
                        $value = ', '.$object->getTitle();
59
                    }
60
                }
61
                $html .= "<li><b>$key ($type):</b> ".$obj->$field.$value.' </li>';
62
            }
63
        }
64
        //to do: has_many and many_many
65
        $html .= '</ul>';
66
67
        return $html;
68
    }
69
70
    private static function cleanup_value($type, $value)
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...
71
    {
72
        switch ($type) {
73
            case 'HTMLText':
74
                $value = (substr(strip_tags($value), 0, 100));
75
                break;
76
            case 'Boolean':
77
                $value = $value ? 'YES' : 'NO';
78
                break;
79
            default:
80
                $value = $value;
0 ignored issues
show
Bug introduced by
Why assign $value to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
81
                break;
82
        }
83
84
        return $value;
85
    }
86
}
87