Conditions | 19 |
Paths | 627 |
Total Lines | 104 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
82 | public function convert($length, $to_unit) |
||
83 | { |
||
84 | if (!$length->isValid()) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | $n = $length->getN(); |
||
89 | $unit = $length->getUnit(); |
||
90 | |||
91 | if ($n === '0' || $unit === false) { |
||
92 | return new HTMLPurifier_Length('0', false); |
||
93 | } |
||
94 | |||
95 | $state = $dest_state = false; |
||
96 | foreach (self::$units as $k => $x) { |
||
97 | if (isset($x[$unit])) { |
||
98 | $state = $k; |
||
99 | } |
||
100 | if (isset($x[$to_unit])) { |
||
101 | $dest_state = $k; |
||
102 | } |
||
103 | } |
||
104 | if (!$state || !$dest_state) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | // Some calculations about the initial precision of the number; |
||
109 | // this will be useful when we need to do final rounding. |
||
110 | $sigfigs = $this->getSigFigs($n); |
||
111 | if ($sigfigs < $this->outputPrecision) { |
||
112 | $sigfigs = $this->outputPrecision; |
||
113 | } |
||
114 | |||
115 | // BCMath's internal precision deals only with decimals. Use |
||
116 | // our default if the initial number has no decimals, or increase |
||
117 | // it by how ever many decimals, thus, the number of guard digits |
||
118 | // will always be greater than or equal to internalPrecision. |
||
119 | $log = (int)floor(log(abs($n), 10)); |
||
120 | $cp = ($log < 0) ? $this->internalPrecision - $log : $this->internalPrecision; // internal precision |
||
121 | |||
122 | for ($i = 0; $i < 2; $i++) { |
||
123 | |||
124 | // Determine what unit IN THIS SYSTEM we need to convert to |
||
125 | if ($dest_state === $state) { |
||
126 | // Simple conversion |
||
127 | $dest_unit = $to_unit; |
||
128 | } else { |
||
129 | // Convert to the smallest unit, pending a system shift |
||
130 | $dest_unit = self::$units[$state][$dest_state][0]; |
||
131 | } |
||
132 | |||
133 | // Do the conversion if necessary |
||
134 | if ($dest_unit !== $unit) { |
||
135 | $factor = $this->div(self::$units[$state][$unit], self::$units[$state][$dest_unit], $cp); |
||
136 | $n = $this->mul($n, $factor, $cp); |
||
137 | $unit = $dest_unit; |
||
138 | } |
||
139 | |||
140 | // Output was zero, so bail out early. Shouldn't ever happen. |
||
141 | if ($n === '') { |
||
142 | $n = '0'; |
||
143 | $unit = $to_unit; |
||
144 | break; |
||
145 | } |
||
146 | |||
147 | // It was a simple conversion, so bail out |
||
148 | if ($dest_state === $state) { |
||
149 | break; |
||
150 | } |
||
151 | |||
152 | if ($i !== 0) { |
||
153 | // Conversion failed! Apparently, the system we forwarded |
||
154 | // to didn't have this unit. This should never happen! |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | // Pre-condition: $i == 0 |
||
159 | |||
160 | // Perform conversion to next system of units |
||
161 | $n = $this->mul($n, self::$units[$state][$dest_state][1], $cp); |
||
162 | $unit = self::$units[$state][$dest_state][2]; |
||
163 | $state = $dest_state; |
||
164 | |||
165 | // One more loop around to convert the unit in the new system. |
||
166 | |||
167 | } |
||
168 | |||
169 | // Post-condition: $unit == $to_unit |
||
170 | if ($unit !== $to_unit) { |
||
171 | return false; |
||
172 | } |
||
173 | |||
174 | // Useful for debugging: |
||
175 | //echo "<pre>n"; |
||
176 | //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n"; |
||
177 | |||
178 | $n = $this->round($n, $sigfigs); |
||
179 | if (strpos($n, '.') !== false) { |
||
180 | $n = rtrim($n, '0'); |
||
181 | } |
||
182 | $n = rtrim($n, '.'); |
||
183 | |||
184 | return new HTMLPurifier_Length($n, $unit); |
||
185 | } |
||
186 | |||
308 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.