| Conditions | 6 |
| Paths | 10 |
| Total Lines | 67 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 22 | public static function convertHtmlToOpenXMLTag($value, $tag = 'b') |
||
| 23 | { |
||
| 24 | $value_array = []; |
||
| 25 | $run_again = false; |
||
| 26 | //this could be used instead if html was already escaped |
||
| 27 | /* |
||
| 28 | $bo = "<"; |
||
| 29 | $bc = ">"; |
||
| 30 | */ |
||
| 31 | $bo = '<'; |
||
| 32 | $bc = '>'; |
||
| 33 | |||
| 34 | //get first BOLD |
||
| 35 | $tag_open_values = explode($bo.$tag.$bc, $value, 2); |
||
| 36 | |||
| 37 | if (count($tag_open_values) > 1) { |
||
| 38 | //save everything before the bold and close it |
||
| 39 | $value_array[] = $tag_open_values[0]; |
||
| 40 | $value_array[] = '</w:t></w:r>'; |
||
| 41 | |||
| 42 | if($tag=="u") |
||
| 43 | { |
||
| 44 | $tag_ooxml = 'u w:val="single" '; |
||
| 45 | $loose_formatting = ""; |
||
| 46 | } |
||
| 47 | elseif($tag=="b") |
||
| 48 | { |
||
| 49 | $tag_ooxml = 'b '; |
||
| 50 | $loose_formatting = ""; |
||
| 51 | } |
||
| 52 | elseif($tag=="i") |
||
| 53 | { |
||
| 54 | $tag_ooxml = 'i '; |
||
| 55 | $loose_formatting = "<w:i w:val=\"0\"/>"; |
||
| 56 | } |
||
| 57 | |||
| 58 | //define styling parameters |
||
| 59 | $wrPr_open = strrpos($tag_open_values[0], '<w:rPr>'); |
||
| 60 | $wrPr_close = strrpos($tag_open_values[0], '</w:rPr>', $wrPr_open); |
||
| 61 | $neutral_style = '<w:r><w:rPr>'.substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7))).'</w:rPr><w:t xml:space="preserve">'; |
||
| 62 | $tagged_style = '<w:r><w:rPr><w:'.$tag_ooxml.'/>'.str_replace($loose_formatting,"",substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7)))).'</w:rPr><w:t xml:space="preserve">'; |
||
| 63 | |||
| 64 | //open new text run and make it bold, include previous styling |
||
| 65 | $value_array[] = $tagged_style; |
||
| 66 | //get everything before bold close and after |
||
| 67 | $tag_close_values = explode($bo.'/'.$tag.$bc, $tag_open_values[1], 2); |
||
| 68 | //add bold text |
||
| 69 | $value_array[] = $tag_close_values[0]; |
||
| 70 | //close bold run |
||
| 71 | $value_array[] = '</w:t></w:r>'; |
||
| 72 | //open run for after bold |
||
| 73 | $value_array[] = $neutral_style; |
||
| 74 | $value_array[] = $tag_close_values[1]; |
||
| 75 | |||
| 76 | $run_again = true; |
||
| 77 | } else { |
||
| 78 | $value_array[] = $tag_open_values[0]; |
||
| 79 | } |
||
| 80 | |||
| 81 | $value = implode('', $value_array); |
||
| 82 | |||
| 83 | if ($run_again) { |
||
| 84 | $value = self::convertHtmlToOpenXMLTag($value, $tag); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $value; |
||
| 88 | } |
||
| 89 | } |
||
| 90 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: