| Conditions | 13 |
| Paths | 26 |
| Total Lines | 58 |
| Code Lines | 35 |
| 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 |
||
| 69 | protected static function add($funcType, $options) |
||
| 70 | { |
||
| 71 | $options = array_merge(self::DEFAULT_OPTIONS, $options); |
||
| 72 | |||
| 73 | if (!array_key_exists('name', $options)) { |
||
| 74 | trigger_error('Cannot add asset: Name not provided!', E_USER_WARNING); |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (!array_key_exists('path', $options)) { |
||
| 79 | trigger_error('Cannot add asset: Path not provided!', E_USER_WARNING); |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | $funcName = "wp_{$funcType}_{$options['type']}"; |
||
| 84 | $lastVar = $options['type'] === 'script' ? $options['inFooter'] : $options['media']; |
||
| 85 | |||
| 86 | // allow external urls |
||
| 87 | $path = $options['path']; |
||
| 88 | if (!(StringHelpers::startsWith('http://', $path)) |
||
| 89 | && !(StringHelpers::startsWith('https://', $path)) |
||
| 90 | && !(StringHelpers::startsWith('//', $path)) |
||
| 91 | ) { |
||
| 92 | $path = Asset::requireUrl($path); |
||
| 93 | } |
||
| 94 | |||
| 95 | // TODO: What if a script is registered twice? |
||
| 96 | if (true === self::$loadFromCdn |
||
| 97 | && !empty($options['cdn']) |
||
| 98 | && !empty($options['cdn']['check']) |
||
| 99 | && !empty($options['cdn']['url']) |
||
| 100 | ) { |
||
| 101 | $cdnCheck = $options['cdn']['check']; |
||
| 102 | $localPath = $path; |
||
| 103 | $path = $options['cdn']['url']; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (function_exists($funcName)) { |
||
| 107 | $funcName( |
||
| 108 | $options['name'], |
||
| 109 | $path, |
||
| 110 | $options['dependencies'], |
||
| 111 | $options['version'], |
||
| 112 | $lastVar |
||
| 113 | ); |
||
| 114 | |||
| 115 | if (isset($localPath)) { |
||
| 116 | wp_add_inline_script( |
||
| 117 | $options['name'], |
||
| 118 | "${cdnCheck}||document.write(\"<script src=\\\"${localPath}\\\"><\/script>\")" |
||
|
|
|||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | return true; |
||
| 123 | } |
||
| 124 | |||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 141 |
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: