Conditions | 11 |
Paths | 8 |
Total Lines | 45 |
Code Lines | 19 |
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 |
||
61 | private static function evaluate_requirement( $object, $field, $requirement ) { |
||
62 | |||
63 | // Test for callables first. |
||
64 | if ( is_callable( $requirement ) ) { |
||
65 | return call_user_func_array( $requirement, array( $field, $object ) ); |
||
66 | } |
||
67 | |||
68 | // Look for comparison array. |
||
69 | if ( is_array( $requirement ) && isset( $requirement['operator'], $requirement['value'], $requirement['setting'] ) ) { |
||
70 | |||
71 | if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) { |
||
72 | if ( false === strpos( $requirement['setting'], '[' ) ) { |
||
73 | $requirement['setting'] = $field['option_name'] . '[' . $requirement['setting'] . ']'; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $current_setting = $object->manager->get_setting( $requirement['setting'] ); |
||
78 | |||
79 | /** |
||
80 | * Depending on the 'operator' argument we use, |
||
81 | * we'll need to perform the appropriate comparison |
||
82 | * and figure out if the control will be shown or not. |
||
83 | */ |
||
84 | $show = self::compare( $requirement['value'], $current_setting->value(), $requirement['operator'] ); |
||
85 | |||
86 | } else { |
||
87 | if ( ! is_array( $requirement ) ) { |
||
88 | return true; |
||
89 | } |
||
90 | |||
91 | if ( is_array( $requirement ) ) { |
||
92 | // Handles "OR" functionality. |
||
93 | $show = false; |
||
94 | foreach ( $requirement as $sub_requirement ) { |
||
95 | $show = self::evaluate_requirement( $object, $field, $sub_requirement ); |
||
96 | // No need to go on if one sub_requirement returns true. |
||
97 | if ( $show ) { |
||
98 | return true; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $show; |
||
|
|||
105 | } |
||
106 | |||
182 |
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: