| Conditions | 17 |
| Paths | 1122 |
| Total Lines | 86 |
| Code Lines | 56 |
| Lines | 15 |
| Ratio | 17.44 % |
| 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 |
||
| 69 | public function validate($string, $config, $context) |
||
| 70 | { |
||
| 71 | $string = $this->parseCDATA($string); |
||
| 72 | $bits = explode(' ', $string); |
||
| 73 | |||
| 74 | $keywords = array(); |
||
| 75 | $keywords['h'] = false; // left, right |
||
| 76 | $keywords['v'] = false; // top, bottom |
||
| 77 | $keywords['ch'] = false; // center (first word) |
||
| 78 | $keywords['cv'] = false; // center (second word) |
||
| 79 | $measures = array(); |
||
| 80 | |||
| 81 | $i = 0; |
||
| 82 | |||
| 83 | $lookup = array( |
||
| 84 | 'top' => 'v', |
||
| 85 | 'bottom' => 'v', |
||
| 86 | 'left' => 'h', |
||
| 87 | 'right' => 'h', |
||
| 88 | 'center' => 'c' |
||
| 89 | ); |
||
| 90 | |||
| 91 | foreach ($bits as $bit) { |
||
| 92 | if ($bit === '') { |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | // test for keyword |
||
| 97 | $lbit = ctype_lower($bit) ? $bit : strtolower($bit); |
||
| 98 | if (isset($lookup[$lbit])) { |
||
| 99 | $status = $lookup[$lbit]; |
||
| 100 | if ($status == 'c') { |
||
| 101 | if ($i == 0) { |
||
| 102 | $status = 'ch'; |
||
| 103 | } else { |
||
| 104 | $status = 'cv'; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | $keywords[$status] = $lbit; |
||
| 108 | $i++; |
||
| 109 | } |
||
| 110 | |||
| 111 | // test for length |
||
| 112 | $r = $this->length->validate($bit, $config, $context); |
||
| 113 | if ($r !== false) { |
||
| 114 | $measures[] = $r; |
||
| 115 | $i++; |
||
| 116 | } |
||
| 117 | |||
| 118 | // test for percentage |
||
| 119 | $r = $this->percentage->validate($bit, $config, $context); |
||
| 120 | if ($r !== false) { |
||
| 121 | $measures[] = $r; |
||
| 122 | $i++; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!$i) { |
||
| 127 | return false; |
||
| 128 | } // no valid values were caught |
||
| 129 | |||
| 130 | $ret = array(); |
||
| 131 | |||
| 132 | // first keyword |
||
| 133 | View Code Duplication | if ($keywords['h']) { |
|
| 134 | $ret[] = $keywords['h']; |
||
| 135 | } elseif ($keywords['ch']) { |
||
| 136 | $ret[] = $keywords['ch']; |
||
| 137 | $keywords['cv'] = false; // prevent re-use: center = center center |
||
| 138 | } elseif (count($measures)) { |
||
| 139 | $ret[] = array_shift($measures); |
||
| 140 | } |
||
| 141 | |||
| 142 | View Code Duplication | if ($keywords['v']) { |
|
| 143 | $ret[] = $keywords['v']; |
||
| 144 | } elseif ($keywords['cv']) { |
||
| 145 | $ret[] = $keywords['cv']; |
||
| 146 | } elseif (count($measures)) { |
||
| 147 | $ret[] = array_shift($measures); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (empty($ret)) { |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | return implode(' ', $ret); |
||
| 154 | } |
||
| 155 | } |
||
| 158 |
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.