| Conditions | 10 |
| Paths | 192 |
| Total Lines | 48 |
| 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 |
||
| 38 | public function processCommand() |
||
| 39 | { |
||
| 40 | $this->drafts = $this->route->getMatchedParam('drafts', false); |
||
| 41 | $this->baseurl = $this->route->getMatchedParam('baseurl'); |
||
| 42 | $this->quiet = $this->route->getMatchedParam('quiet', false); |
||
| 43 | $this->remove = $this->getRoute()->getMatchedParam('remove', false); |
||
| 44 | $this->dryrun = $this->getRoute()->getMatchedParam('dry-run', false); |
||
| 45 | |||
| 46 | $options = []; |
||
| 47 | $messageOpt = ' ('; |
||
| 48 | |||
| 49 | if ($this->drafts) { |
||
| 50 | $options['drafts'] = true; |
||
| 51 | $messageOpt .= 'with drafts, '; |
||
| 52 | } |
||
| 53 | if ($this->baseurl) { |
||
| 54 | $config['site']['baseurl'] = $this->baseurl; |
||
|
|
|||
| 55 | } |
||
| 56 | if ($this->quiet) { |
||
| 57 | $options['verbosity'] = PHPoole::VERBOSITY_QUIET; |
||
| 58 | } |
||
| 59 | if ($this->remove) { |
||
| 60 | if ($this->fs->exists($this->getPath().'/'.$this->getPHPoole()->getConfig()->get('output.dir'))) { |
||
| 61 | $this->fs->remove($this->getPath().'/'.$this->getPHPoole()->getConfig()->get('output.dir')); |
||
| 62 | $this->wlDone('Output directory removed!'); |
||
| 63 | exit(0); |
||
| 64 | } |
||
| 65 | $this->wlError('Output directory not found!'); |
||
| 66 | exit(0); |
||
| 67 | } |
||
| 68 | if ($this->dryrun) { |
||
| 69 | $options['dry-run'] = true; |
||
| 70 | $messageOpt .= 'dry-run, '; |
||
| 71 | } |
||
| 72 | $messageOpt .= ')'; |
||
| 73 | |||
| 74 | try { |
||
| 75 | if (!$this->quiet) { |
||
| 76 | $this->wl(sprintf('Building website%s...', $messageOpt)); |
||
| 77 | } |
||
| 78 | $this->getPHPoole($options)->build($options); |
||
| 79 | if ($this->getRoute()->getName() == 'serve') { |
||
| 80 | $this->fs->dumpFile($this->getPath().'/.phpoole/changes.flag', ''); |
||
| 81 | } |
||
| 82 | } catch (\Exception $e) { |
||
| 83 | throw new \Exception(sprintf($e->getMessage())); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.