RunCommandLineMethodOnModule::CheckCommandExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
abstract class RunCommandLineMethodOnModule extends Object
4
{
5
6
      /**
7
       * root dir for module
8
       * e.g. /var/www/modules/mymodule
9
       * no final slash
10
       *
11
       * @var string
12
       */
13
    protected $rootDirForModule = '';
14
15
    /**
16
     *
17
     * @var string
18
     */
19
    protected $commands = [];
20
21
    public function setRootDirForModule($rootDirForModule)
22
    {
23
        $this->$rootDirForModule = $rootDirForModule;
24
    }
25
26
    /**
27
     *
28
     *
29
     * @param string
30
     */
31
    public function setCommand(array $commands)
32
    {
33
        $this->commands = $commands;
0 ignored issues
show
Documentation Bug introduced by
It seems like $commands of type array is incompatible with the declared type string of property $commands.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
35
        return $this;
36
    }
37
38
    /**
39
     *
40
     *
41
     * @param string
42
     */
43
    public function addCommands(string $command)
44
    {
45
        $this->commands[] = $command;
46
47
        return $this;
48
    }
49
50
    public function __construct($rootDirForModule = '')
51
    {
52
        $this->rootDirForModule = $rootDirForModule;
53
    }
54
55
    public function run()
56
    {
57
        if (! $this->rootDirForModule) {
58
            user_error('no root dir for module has been set');
59
        }
60
        if (! count($this->commands)) {
0 ignored issues
show
Bug introduced by
$this->commands of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        if (! count(/** @scrutinizer ignore-type */ $this->commands)) {
Loading history...
61
            user_error('command not set');
62
        }
63
        $this->runCommand();
64
    }
65
66
    /**
67
     * runs a command from the root dir or the module
68
     */
69
    protected function runCommand()
70
    {
71
        foreach($this->commands as $command) {
0 ignored issues
show
Bug introduced by
The expression $this->commands of type string is not traversable.
Loading history...
72
            GeneralMethods::output_to_screen('Running ' . $command);
73
            return exec(
74
              ' cd '.$this->rootDirForModule.';
75
                '.$command.'
76
                '
77
            );
78
        }
79
    }
80
81
    public static function CheckCommandExists($cmd)
82
    {
83
        return !empty(shell_exec("which $cmd"));
84
    }
85
}
86