Completed
Push — master ( efd4b5...cdc15c )
by Greg
11s
created

Exec::setInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 121.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Robo\Task\Base;
3
4
use Robo\Common\ExecTrait;
5
use Robo\Contract\CommandInterface;
6
use Robo\Contract\PrintedInterface;
7
use Robo\Contract\SimulatedInterface;
8
use Robo\Task\BaseTask;
9
use Symfony\Component\Process\Process;
10
use Robo\Result;
11
12
/**
13
 * Executes shell script. Closes it when running in background mode.
14
 *
15
 * ``` php
16
 * <?php
17
 * $this->taskExec('compass')->arg('watch')->run();
18
 * // or use shortcut
19
 * $this->_exec('compass watch');
20
 *
21
 * $this->taskExec('compass watch')->background()->run();
22
 *
23
 * if ($this->taskExec('phpunit .')->run()->wasSuccessful()) {
24
 *  $this->say('tests passed');
25
 * }
26
 *
27
 * ?>
28
 * ```
29
 */
30
class Exec extends BaseTask implements CommandInterface, PrintedInterface, SimulatedInterface
31
{
32
    use \Robo\Common\CommandReceiver;
33
    use \Robo\Common\ExecOneCommand;
34
35
    /**
36
     * @var static[]
37
     */
38
    protected static $instances = [];
39
40
    /**
41
     * @var string|\Robo\Contract\CommandInterface
42
     */
43
    protected $command;
44
45
    /**
46
     * @param string|\Robo\Contract\CommandInterface $command
47
     */
48
    public function __construct($command)
49
    {
50
        $this->command = $this->receiveCommand($command);
51
    }
52
53
    /**
54
     *
55
     */
56
    public function __destruct()
57
    {
58
        $this->stop();
59
    }
60
61
    /**
62
     * Executes command in background mode (asynchronously)
63
     *
64
     * @return $this
65
     */
66
    public function background($arg = true)
67
    {
68
        self::$instances[] = $this;
69
        $this->background = $arg;
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function getCommandDescription()
77
    {
78
        return $this->getCommand();
79
    }
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getCommand()
84
    {
85
        return trim($this->command . $this->arguments);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function simulate($context)
92
    {
93
        $this->printAction($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by parameter $context on line 91 can also be of type null; however, Robo\Common\ExecTrait::printAction() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
    }
95
96
    public static function stopRunningJobs()
97
    {
98
        foreach (self::$instances as $instance) {
99
            if ($instance) {
100
                unset($instance);
101
            }
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 View Code Duplication
    public function run()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
    {
110
        $result_data = $this->execute(new Process($this->getCommand()));
111
        return new Result(
112
            $this,
113
            $result_data->getExitCode(),
114
            $result_data->getMessage(),
115
            $result_data->getData()
116
        );
117
    }
118
}
119
120
if (function_exists('pcntl_signal')) {
121
    pcntl_signal(SIGTERM, ['Robo\Task\Base\Exec', 'stopRunningJobs']);
122
}
123
124
register_shutdown_function(['Robo\Task\Base\Exec', 'stopRunningJobs']);
125