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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) |
|
|
|
|
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; |
|
|
|
|
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $value; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.