Completed
Push — build-options ( 5749a9 )
by Arnaud
04:10
created

Build::processCommand()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 6.6545
c 0
b 0
f 0
cc 10
nc 192
nop 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
/*
3
 * This file is part of the PHPoole package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PHPoole\Command;
12
13
use PHPoole\PHPoole;
14
15
class Build extends AbstractCommand
16
{
17
    /**
18
     * @var bool
19
     */
20
    protected $drafts;
21
    /**
22
     * @var string
23
     */
24
    protected $baseurl;
25
    /**
26
     * @var bool
27
     */
28
    protected $quiet;
29
    /**
30
     * @var bool
31
     */
32
    protected $remove;
33
    /**
34
     * @var bool
35
     */
36
    protected $dryrun;
37
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
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