Conditions | 12 |
Paths | 133 |
Total Lines | 45 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Tests | 26 |
CRAP Score | 12.1591 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | 1 | function smarty_function_sugarvar($params, &$smarty) |
|
70 | { |
||
71 | 4 | if(empty($params['key'])) { |
|
72 | $smarty->trigger_error("sugarvar: missing 'key' parameter"); |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | 4 | $object = (empty($params['objectName']))?$smarty->get_template_vars('parentFieldArray'): $params['objectName']; |
|
77 | 4 | $displayParams = $smarty->get_template_vars('displayParams'); |
|
78 | |||
79 | |||
80 | 4 | if(empty($params['memberName'])){ |
|
81 | 4 | $member = $smarty->get_template_vars('vardef'); |
|
82 | 4 | $member = $member['name']; |
|
83 | }else{ |
||
84 | 4 | $members = explode('.', $params['memberName']); |
|
85 | 4 | $member = $smarty->get_template_vars($members[0]); |
|
86 | 4 | for($i = 1; $i < count($members); $i++){ |
|
|
|||
87 | 4 | $member = $member[$members[$i]]; |
|
88 | } |
||
89 | } |
||
90 | |||
91 | 4 | $_contents = '$'. $object . '.' . $member . '.' . $params['key']; |
|
92 | 4 | if(empty($params['stringFormat']) && empty($params['string'])) { |
|
93 | 4 | $_contents = '{' . $_contents; |
|
94 | 4 | if(!empty($displayParams['htmlescape'])){ |
|
95 | 1 | $_contents .= '|escape:\'html\''; |
|
96 | } |
||
97 | 4 | if(!empty($params['htmlentitydecode'])){ |
|
98 | 1 | $_contents .= '|escape:\'html_entity_decode\''; |
|
99 | } |
||
100 | 4 | if(!empty($displayParams['strip_tags'])){ |
|
101 | $_contents .= '|strip_tags'; |
||
102 | } |
||
103 | 4 | if(!empty($displayParams['url2html'])){ |
|
104 | 1 | $_contents .= '|url2html'; |
|
105 | } |
||
106 | 4 | if(!empty($displayParams['nl2br'])){ |
|
107 | 1 | $_contents .= '|nl2br'; |
|
108 | } |
||
109 | |||
110 | 4 | $_contents .= '}'; |
|
111 | } |
||
112 | 4 | return $_contents; |
|
113 | 1 | } |
|
114 | ?> |
||
115 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: