| Conditions | 8 |
| Paths | 13 |
| Total Lines | 69 |
| Code Lines | 28 |
| 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 |
||
| 23 | function wpbo_get_templates_list() { |
||
| 24 | |||
| 25 | /* Set the default templates directory */ |
||
| 26 | $directory = array( |
||
| 27 | 'path' => WPBO_PATH . 'templates', |
||
| 28 | 'url' => WPBO_URL . 'templates' |
||
| 29 | ); |
||
| 30 | |||
| 31 | /* Allow for extra directories */ |
||
| 32 | $dirs = apply_filters( 'wpbo_templates_dirs', array( $directory ) ); |
||
| 33 | $list = array(); |
||
| 34 | |||
| 35 | foreach( $dirs as $key => $dir ) { |
||
| 36 | |||
| 37 | $exceptions = array( '.', '..' ); |
||
| 38 | |||
| 39 | if( !isset( $_GET['test_template'] ) ) |
||
| 40 | $exceptions[] = 'template-test.php'; |
||
| 41 | |||
| 42 | /* Get file paths with trailing slashes */ |
||
| 43 | $path = trailingslashit( $dir['path'] ); |
||
| 44 | $url = trailingslashit( $dir['url'] ); |
||
| 45 | |||
| 46 | /* Scan the content */ |
||
| 47 | $templates = scandir( $path ); |
||
| 48 | |||
| 49 | foreach( $templates as $key => $template ) { |
||
| 50 | |||
| 51 | $images = array( 'png', 'jpg', 'jpeg', 'gif' ); // Allowed images types |
||
| 52 | |||
| 53 | /* Don't process the '.' and '..' */ |
||
| 54 | if( in_array( $template, $exceptions ) ) |
||
| 55 | continue; |
||
| 56 | |||
| 57 | /* Get file extension */ |
||
| 58 | $ext = pathinfo( $path . $template, PATHINFO_EXTENSION ); |
||
| 59 | |||
| 60 | /* Only check the php files */ |
||
| 61 | if( 'php' != $ext ) |
||
| 62 | continue; |
||
| 63 | |||
| 64 | /* Get template base name */ |
||
| 65 | $tpl = str_replace( ".$ext", '', $template ); |
||
| 66 | |||
| 67 | foreach( $images as $k => $type ) { |
||
| 68 | |||
| 69 | $imgfile = $tpl . '.' . $type; |
||
| 70 | |||
| 71 | if( file_exists( $path . $imgfile ) ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @todo need to get image URL |
||
| 75 | */ |
||
| 76 | $img = $url . $imgfile; |
||
| 77 | break; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | /* Add new template to the list */ |
||
| 84 | $list[$tpl] = $img; |
||
|
|
|||
| 85 | |||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $list; |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 161 | } |
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: