| Conditions | 9 |
| Paths | 121 |
| Total Lines | 63 |
| 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->watch = $this->getRoute()->getMatchedParam('watch', false); |
||
| 41 | $this->browser = $this->getRoute()->getMatchedParam('browser', false); |
||
| 42 | $this->fileSystem = new Filesystem(); |
||
| 43 | |||
| 44 | $this->setUpServer(); |
||
| 45 | $command = sprintf( |
||
| 46 | 'php -S %s:%d -t %s %s', |
||
| 47 | 'localhost', |
||
| 48 | '8000', |
||
| 49 | $this->getPath().'/'.$this->getPHPoole()->getConfig()->get('output.dir'), |
||
| 50 | sprintf('%s/.phpoole/router.php', $this->getPath()) |
||
| 51 | ); |
||
| 52 | |||
| 53 | $this->wlAnnonce(sprintf('Starting server (http://%s:%d)...', 'localhost', '8000')); |
||
| 54 | $process = new Process($command); |
||
| 55 | if (!$process->isStarted()) { |
||
| 56 | // write changes cache |
||
| 57 | if ($this->watch) { |
||
| 58 | $finder = new Finder(); |
||
| 59 | $finder->files() |
||
| 60 | ->name('*.md') |
||
| 61 | ->name('*.twig') |
||
| 62 | ->in([ |
||
| 63 | $this->getPath().'/'.$this->getPHPoole()->getConfig()->get('content.dir'), |
||
| 64 | $this->getPath().'/'.$this->getPHPoole()->getConfig()->get('layouts.dir'), |
||
| 65 | ]); |
||
| 66 | if (is_dir($this->getPath().'/'.$this->getPHPoole()->getConfig()->get('themes.dir'))) { |
||
| 67 | $finder->in($this->getPath().'/'.$this->getPHPoole()->getConfig()->get('themes.dir')); |
||
| 68 | } |
||
| 69 | $hashContent = new Crc32ContentHash(); |
||
| 70 | $resourceCache = new ResourceCacheMemory(); |
||
| 71 | $resourceWatcher = new ResourceWatcher($resourceCache, $finder, $hashContent); |
||
| 72 | $resourceWatcher->initialize(); |
||
| 73 | $this->fileSystem->dumpFile($this->getPath().'/.phpoole/watch.flag', ''); |
||
| 74 | } |
||
| 75 | // start server |
||
| 76 | try { |
||
| 77 | $process->start(); |
||
| 78 | if ($this->browser) { |
||
| 79 | Plateform::openBrowser('http://localhost:8000'); |
||
| 80 | } |
||
| 81 | while ($process->isRunning()) { |
||
| 82 | // watch changes? |
||
| 83 | if ($this->watch) { |
||
| 84 | $result = $resourceWatcher->findChanges(); |
||
|
|
|||
| 85 | if ($result->hasChanges()) { |
||
| 86 | // re-generate |
||
| 87 | $this->wlAlert('Changes detected!'); |
||
| 88 | $callable = new Build($this->getRoute(), $this->getConsole()); |
||
| 89 | $callable($this->getRoute(), $this->getConsole()); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | usleep(1000000); // 1 s |
||
| 93 | } |
||
| 94 | } catch (ProcessFailedException $e) { |
||
| 95 | $this->tearDownServer(); |
||
| 96 | echo $e->getMessage(); |
||
| 97 | exit(2); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 136 |
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: