Conditions | 11 |
Paths | 30 |
Total Lines | 37 |
Code Lines | 22 |
Lines | 0 |
Ratio | 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 |
||
51 | public function run($request) { |
||
52 | |||
53 | if($request->getVar('urls') && is_array($request->getVar('urls'))) { |
||
54 | return $this->queueURLs($request->getVar('urls')); |
||
55 | } |
||
56 | if($request->getVar('urls')) { |
||
57 | return $this->queueURLs(explode(',', $request->getVar('urls'))); |
||
58 | } |
||
59 | |||
60 | // The following shenanigans are necessary because a simple Page::get() |
||
61 | // will run out of memory on large data sets. This will take the pages |
||
62 | // in chunks by running this script multiple times and setting $_GET['start']. |
||
63 | // Chunk size can be set via yml (SiteTreeFullBuildEngine.records_per_request). |
||
64 | // To disable this functionality, just set a large chunk size and pass start=0. |
||
65 | increase_time_limit_to(); |
||
66 | $self = get_class($this); |
||
67 | $verbose = isset($_GET['verbose']); |
||
68 | |||
69 | if (isset($_GET['start'])) { |
||
70 | $this->runFrom((int)$_GET['start']); |
||
71 | } else { |
||
72 | foreach(array('framework','sapphire') as $dirname) { |
||
73 | $script = sprintf("%s%s$dirname%scli-script.php", BASE_PATH, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR); |
||
74 | if (file_exists($script)) break; |
||
75 | } |
||
76 | |||
77 | $total = $this->getAllLivePages()->count(); |
||
78 | echo "Adding all pages to the queue. Total: $total\n\n"; |
||
79 | for ($offset = 0; $offset < $total; $offset += self::config()->records_per_request) { |
||
80 | echo "$offset.."; |
||
81 | $cmd = "php $script dev/tasks/$self start=$offset"; |
||
|
|||
82 | if($verbose) echo "\n Running '$cmd'\n"; |
||
83 | $res = $verbose ? passthru($cmd) : `$cmd`; |
||
84 | if($verbose) echo " ".preg_replace('/\r\n|\n/', '$0 ', $res)."\n"; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
165 |
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: