| Conditions | 16 |
| Paths | 1961 |
| Total Lines | 99 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 36 | function dol_convertToWord($num, $langs, $currency=false, $centimes=false) |
||
| 37 | { |
||
| 38 | global $conf; |
||
| 39 | |||
| 40 | $num = str_replace(array(',', ' '), '', trim($num)); |
||
| 41 | if(! $num) { |
||
| 42 | return false; |
||
| 43 | } |
||
| 44 | if($centimes && strlen($num) == 1) { |
||
| 45 | $num = $num*10; |
||
| 46 | } |
||
| 47 | $TNum = explode('.',$num); |
||
| 48 | $num = (int) $TNum[0]; |
||
| 49 | $words = array(); |
||
| 50 | $list1 = array( |
||
| 51 | '', |
||
| 52 | $langs->transnoentitiesnoconv('one'), |
||
| 53 | $langs->transnoentitiesnoconv('two'), |
||
| 54 | $langs->transnoentitiesnoconv('three'), |
||
| 55 | $langs->transnoentitiesnoconv('four'), |
||
| 56 | $langs->transnoentitiesnoconv('five'), |
||
| 57 | $langs->transnoentitiesnoconv('six'), |
||
| 58 | $langs->transnoentitiesnoconv('seven'), |
||
| 59 | $langs->transnoentitiesnoconv('eight'), |
||
| 60 | $langs->transnoentitiesnoconv('nine'), |
||
| 61 | $langs->transnoentitiesnoconv('ten'), |
||
| 62 | $langs->transnoentitiesnoconv('eleven'), |
||
| 63 | $langs->transnoentitiesnoconv('twelve'), |
||
| 64 | $langs->transnoentitiesnoconv('thirteen'), |
||
| 65 | $langs->transnoentitiesnoconv('fourteen'), |
||
| 66 | $langs->transnoentitiesnoconv('fifteen'), |
||
| 67 | $langs->transnoentitiesnoconv('sixteen'), |
||
| 68 | $langs->transnoentitiesnoconv('seventeen'), |
||
| 69 | $langs->transnoentitiesnoconv('eighteen'), |
||
| 70 | $langs->transnoentitiesnoconv('nineteen') |
||
| 71 | ); |
||
| 72 | $list2 = array( |
||
| 73 | '', |
||
| 74 | $langs->transnoentitiesnoconv('ten'), |
||
| 75 | $langs->transnoentitiesnoconv('twenty'), |
||
| 76 | $langs->transnoentitiesnoconv('thirty'), |
||
| 77 | $langs->transnoentitiesnoconv('forty'), |
||
| 78 | $langs->transnoentitiesnoconv('fifty'), |
||
| 79 | $langs->transnoentitiesnoconv('sixty'), |
||
| 80 | $langs->transnoentitiesnoconv('seventy'), |
||
| 81 | $langs->transnoentitiesnoconv('eighty'), |
||
| 82 | $langs->transnoentitiesnoconv('ninety'), |
||
| 83 | $langs->transnoentitiesnoconv('hundred') |
||
| 84 | ); |
||
| 85 | $list3 = array( |
||
| 86 | '', |
||
| 87 | $langs->transnoentitiesnoconv('thousand'), |
||
| 88 | $langs->transnoentitiesnoconv('million'), |
||
| 89 | $langs->transnoentitiesnoconv('billion'), |
||
| 90 | $langs->transnoentitiesnoconv('trillion'), |
||
| 91 | $langs->transnoentitiesnoconv('quadrillion') |
||
| 92 | ); |
||
| 93 | |||
| 94 | $num_length = strlen($num); |
||
| 95 | $levels = (int) (($num_length + 2) / 3); |
||
| 96 | $max_length = $levels * 3; |
||
| 97 | $num = substr('00' . $num, -$max_length); |
||
| 98 | $num_levels = str_split($num, 3); |
||
| 99 | for ($i = 0; $i < count($num_levels); $i++) { |
||
|
|
|||
| 100 | $levels--; |
||
| 101 | $hundreds = (int) ($num_levels[$i] / 100); |
||
| 102 | $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' '.$langs->transnoentities('hundred') . ( $hundreds == 1 ? '' : 's' ) . ' ': ''); |
||
| 103 | $tens = (int) ($num_levels[$i] % 100); |
||
| 104 | $singles = ''; |
||
| 105 | if ( $tens < 20 ) { |
||
| 106 | $tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' ); |
||
| 107 | } else { |
||
| 108 | $tens = (int) ($tens / 10); |
||
| 109 | $tens = ' ' . $list2[$tens] . ' '; |
||
| 110 | $singles = (int) ($num_levels[$i] % 10); |
||
| 111 | $singles = ' ' . $list1[$singles] . ' '; |
||
| 112 | } |
||
| 113 | $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' ); |
||
| 114 | } //end for loop |
||
| 115 | $commas = count($words); |
||
| 116 | if ($commas > 1) { |
||
| 117 | $commas = $commas - 1; |
||
| 118 | } |
||
| 119 | $concatWords = implode(' ', $words); |
||
| 120 | // Delete multi whitespaces |
||
| 121 | $concatWords = trim(preg_replace('/[ ]+/', ' ', $concatWords)); |
||
| 122 | |||
| 123 | if(!empty($currency)) { |
||
| 124 | $concatWords .= ' '.$currency; |
||
| 125 | } |
||
| 126 | |||
| 127 | // If we need to write cents call again this function for cents |
||
| 128 | if(!empty($TNum[1])) { |
||
| 129 | if(!empty($currency)) $concatWords .= ' '.$langs->transnoentities('and'); |
||
| 130 | $concatWords .= ' '.dol_convertToWord($TNum[1], $langs, $currency, true); |
||
| 131 | if(!empty($currency)) $concatWords .= ' '.$langs->transnoentities('centimes'); |
||
| 132 | } |
||
| 133 | return $concatWords; |
||
| 134 | } |
||
| 135 | |||
| 275 |
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: