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