Conditions | 19 |
Paths | 608 |
Total Lines | 101 |
Code Lines | 47 |
Lines | 3 |
Ratio | 2.97 % |
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 |
||
49 | public function getComponents($device_id=null,$options=array()) { |
||
50 | // Define our results array, this will be set even if no rows are returned. |
||
51 | $RESULT = array(); |
||
52 | $PARAM = array(); |
||
53 | |||
54 | // Our base SQL Query, with no options. |
||
55 | $SQL = "SELECT `C`.`id`,`C`.`device_id`,`C`.`type`,`C`.`label`,`C`.`status`,`C`.`disabled`,`C`.`ignore`,`C`.`error`,`CP`.`attribute`,`CP`.`value` FROM `component` as `C` LEFT JOIN `component_prefs` as `CP` on `C`.`id`=`CP`.`component` WHERE "; |
||
56 | |||
57 | // Device_id is shorthand for filter C.device_id = $device_id. |
||
58 | if (!is_null($device_id)) { |
||
59 | $options['filter']['device_id'] = array('=', $device_id); |
||
60 | } |
||
61 | |||
62 | // Type is shorthand for filter type = $type. |
||
63 | View Code Duplication | if (isset($options['type'])) { |
|
|
|||
64 | $options['filter']['type'] = array('=', $options['type']); |
||
65 | } |
||
66 | |||
67 | // filter field => array(operator,value) |
||
68 | // Filters results based on the field, operator and value |
||
69 | $COUNT = 0; |
||
70 | if (isset($options['filter'])) { |
||
71 | $COUNT++; |
||
72 | $validFields = array('device_id','type','id','label','status','disabled','ignore','error'); |
||
73 | $SQL .= " ( "; |
||
74 | foreach ($options['filter'] as $field => $array) { |
||
75 | // Only add valid fields to the query |
||
76 | if (in_array($field,$validFields)) { |
||
77 | if ($array[0] == 'LIKE') { |
||
78 | $SQL .= "`".$field."` LIKE ? AND "; |
||
79 | $array[1] = "%".$array[1]."%"; |
||
80 | } |
||
81 | else { |
||
82 | // Equals operator is the default |
||
83 | $SQL .= "`".$field."` = ? AND "; |
||
84 | } |
||
85 | array_push($PARAM,$array[1]); |
||
86 | } |
||
87 | } |
||
88 | // Strip the last " AND " before closing the bracket. |
||
89 | $SQL = substr($SQL,0,-5)." )"; |
||
90 | } |
||
91 | |||
92 | if ($COUNT == 0) { |
||
93 | // Strip the " WHERE " that we didn't use. |
||
94 | $SQL = substr($SQL,0,-7); |
||
95 | } |
||
96 | |||
97 | // sort column direction |
||
98 | // Add SQL sorting to the results |
||
99 | if (isset($options['sort'])) { |
||
100 | $SQL .= " ORDER BY ".$options['sort']; |
||
101 | } |
||
102 | |||
103 | // Get our component records using our built SQL. |
||
104 | $COMPONENTS = dbFetchRows($SQL, $PARAM); |
||
105 | |||
106 | // if we have no components we need to return nothing |
||
107 | if (count($COMPONENTS) == 0) { |
||
108 | return $RESULT; |
||
109 | } |
||
110 | |||
111 | // Add the AVP's to the array. |
||
112 | foreach ($COMPONENTS as $COMPONENT) { |
||
113 | if ($COMPONENT['attribute'] != "") { |
||
114 | // if this component has attributes, set them in the array. |
||
115 | $RESULT[$COMPONENT['device_id']][$COMPONENT['id']][$COMPONENT['attribute']] = $COMPONENT['value']; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Populate our reserved fields into the Array, these cant be used as user attributes. |
||
120 | foreach ($COMPONENTS as $COMPONENT) { |
||
121 | foreach ($this->reserved as $k => $v) { |
||
122 | $RESULT[$COMPONENT['device_id']][$COMPONENT['id']][$k] = $COMPONENT[$k]; |
||
123 | } |
||
124 | |||
125 | // Sort each component array so the attributes are in order. |
||
126 | ksort($RESULT[$RESULT[$COMPONENT['device_id']][$COMPONENT['id']]]); |
||
127 | ksort($RESULT[$RESULT[$COMPONENT['device_id']]]); |
||
128 | } |
||
129 | |||
130 | // limit array(start,count) |
||
131 | if (isset($options['limit'])) { |
||
132 | $TEMP = array(); |
||
133 | $COUNT = 0; |
||
134 | // k = device_id, v = array of components for that device_id |
||
135 | foreach ($RESULT as $k => $v) { |
||
136 | // k1 = component id, v1 = component array |
||
137 | foreach ($v as $k1 => $v1) { |
||
138 | if ( ($COUNT >= $options['limit'][0]) && ($COUNT < $options['limit'][0]+$options['limit'][1])) { |
||
139 | $TEMP[$k][$k1] = $v1; |
||
140 | } |
||
141 | // We are counting components. |
||
142 | $COUNT++; |
||
143 | } |
||
144 | } |
||
145 | $RESULT = $TEMP; |
||
146 | } |
||
147 | |||
148 | return $RESULT; |
||
149 | } |
||
150 | |||
259 | } |
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.