| Conditions | 5 |
| Paths | 5 |
| Total Lines | 52 |
| Code Lines | 22 |
| 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 |
||
| 116 | function vnsprintf($format, $args) |
||
| 117 | { |
||
| 118 | // we need to find all of the named parameters to expand |
||
| 119 | $regex = "|(?<!%)(?:%)([^0-9].+)(?:\\\$)|U"; |
||
| 120 | |||
| 121 | $matches = []; |
||
| 122 | $matchCount = preg_match_all($regex, $format, $matches, PREG_OFFSET_CAPTURE); |
||
| 123 | if ($matchCount === 0) { |
||
| 124 | // nothing to do |
||
| 125 | return vsprintf($format, $args); |
||
| 126 | } |
||
| 127 | |||
| 128 | // if we get here, then we have some matches to expand |
||
| 129 | // we're going to add them to the end of this array |
||
| 130 | $messageData = $args; |
||
| 131 | |||
| 132 | // this will keep track of where we've added the data |
||
| 133 | $nextData = count($messageData); |
||
| 134 | $paramKeys = array_fill_keys(array_keys($args), -1); |
||
| 135 | |||
| 136 | // this will keep track of how we've shrunk the format string |
||
| 137 | $formatChange = 0; |
||
| 138 | |||
| 139 | // transform the named parameters into offset parameters |
||
| 140 | foreach ($matches[1] as $match) { |
||
| 141 | // what is the named parameter? |
||
| 142 | $paramName = $match[0]; |
||
| 143 | |||
| 144 | // make sure the named parameter exists in our original data |
||
| 145 | if (!isset($paramKeys[$paramName])) { |
||
| 146 | throw new InvalidArgumentException("vnsprintf: named format-string parameter " . $paramName . " is not provided in \$args array"); |
||
| 147 | } |
||
| 148 | // have we already assigned it a place in $messageData? |
||
| 149 | // in case a named parameter appears multiple times in the format string |
||
| 150 | if ($paramKeys[$paramName] === -1) { |
||
| 151 | // no - it needs a home |
||
| 152 | $messageData[$nextData] =& $args[$paramName]; |
||
| 153 | $paramKeys[$paramName] = $nextData; |
||
| 154 | $nextData++; |
||
| 155 | } |
||
| 156 | // convert the named parameter in the format string into a positional |
||
| 157 | // parameter into $messageData |
||
| 158 | $paramOffset = $paramKeys[$paramName] + 1; |
||
| 159 | $format = substr_replace($format, $paramOffset, $formatChange + $match[1], strlen($paramName)); |
||
| 160 | |||
| 161 | // how much did we shrink / grow the format string by? |
||
| 162 | $formatChange = $formatChange - strlen($paramName) + strlen($paramOffset); |
||
| 163 | } |
||
| 164 | |||
| 165 | // all done |
||
| 166 | return vsprintf($format, $messageData); |
||
| 167 | } |
||
| 168 | |||
| 170 |