Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

ShellTask::getDerivedFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Task;
13
14
use Symfony\Component\Process\Exception\ProcessFailedException;
15
use Symfony\Component\Process\Process;
16
17
use Webcreate\Conveyor\Task\Result\ExecuteResult;
18
use Webcreate\Util\Cli;
19
use Webcreate\Conveyor\IO\IOInterface;
20
use Webcreate\Conveyor\Repository\Version;
21
22
class ShellTask extends Task
23
{
24
    protected $cwd;
25
    protected $cli;
26
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
27
    protected $commandline;
28
29 2
    public function __construct($cwd, Cli $cli, IOInterface $io = null)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
30
    {
31 2
        $this->cli = $cli;
32 2
        $this->io  = $io;
33 2
        $this->cwd = $cwd;
34 2
    }
35
36
    /**
37
     * @todo improve output (also @see SshTask)
38
     *
39
     * @param $target
40
     * @param  Version                                                     $version
41
     * @return ExecuteResult
42
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
43
     */
44 2
    public function execute($target, Version $version = null)
45
    {
46 2
        $this->commandline = $this->options['command'];
47 2
        foreach ($this->commandline as $command) {
48 2
            $this->output(sprintf('Executing: <comment>%s</comment>', $command));
49
50 2
            $hasOutput = false;
51
52 2
            $self = $this;
53 View Code Duplication
            $outputter = function ($type, $buffer) use ($self, &$hasOutput) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                if (false === $self->io->isVerbose()) return;
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
55
56
                if (1 || 'out' === $type) {
57
                    if (!$hasOutput) {
58
                        $this->io->write('');
59
                        $this->io->write('');
60
                        $hasOutput = true;
61
                    }
62
63
                    $lines = explode("\n", $buffer);
64
                    foreach ($lines as $line) {
65
                        if ($output = trim($line, "\r\n")) {
66
                            $self->io->write(sprintf('> %s', $output));
67
                        }
68
                    }
69
                }
70 2
            };
71
72 2
            if ($this->cli->execute($command, $outputter, $this->cwd) <> 0) {
0 ignored issues
show
Documentation introduced by
$outputter is of type object<Closure>, but the function expects a object<Webcreate\Util\Closure>|string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like you are loosely comparing $this->cli->execute($com...$outputter, $this->cwd) of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
73 2
                throw new ProcessFailedException($this->cli->getProcess());
74
            }
75
//            elseif ($message = $this->cli->getErrorOutput()) {
76
//                $messages = explode("\n", $message);
77
//
78
//                $self->io->write(sprintf('<comment>%s</comment>', $messages));
79
//            }
80
        }
81
82 2
        return new ExecuteResult(
83 2
            $this->getDerivedFiles(),
84 2
            $this->getRemovedFiles()
85
        );
86
87
//        return $this->cli->getOutput();
88
    }
89
90
    public function simulate($target, Version $version)
91
    {
92
        $this->commandline = $this->options['command'];
93
        foreach ($this->commandline as $command) {
94
            $this->output(sprintf('Executing: <comment>%s</comment>', $command));
95
        }
96
    }
97
98 2
    protected function getDerivedFiles()
99
    {
100 2
        if (isset($this->options['creates'])) {
101
            return (array) $this->options['creates'];
102
        }
103
104 2
        return array();
105
    }
106
107 2
    protected function getRemovedFiles()
108
    {
109 2
        if (isset($this->options['removes'])) {
110
            return (array) $this->options['removes'];
111
        }
112
113 2
        return array();
114
    }
115
}
116