Passed
Pull Request — master (#5)
by Joao
01:44
created

_Lib::replaceVariables()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Builder;
4
5
use ByJG\Config\Exception\ConfigNotFoundException;
6
use ByJG\Config\Exception\EnvironmentException;
7
use ByJG\Config\Exception\KeyNotFoundException;
8
use Closure;
9
use Psr\SimpleCache\InvalidArgumentException;
10
11
class _Lib
12
{
13
    protected $container;
14
    protected $image;
15
    protected $workdir;
16
    protected $systemOs;
17
18
    public function __construct()
19
    {
20
        $this->workdir = realpath(__DIR__ . '/../..');
21
    }
22
23
    public function getSystemOs()
24
    {
25
        if (!$this->systemOs) {
26
            $this->systemOs = php_uname('s');
27
            if (preg_match('/[Dd]arwin/', $this->systemOs)) {
28
                $this->systemOs = 'Darwin';
29
            } elseif (preg_match('/[Ww]in/', $this->systemOs)) {
30
                $this->systemOs = 'Windows';
31
            }
32
        }
33
34
        return $this->systemOs;
35
    }
36
37
    public function fixDir($command)
38
    {
39
        if ($this->getSystemOs() === "Windows") {
40
            return str_replace('/', '\\', $command);
41
        }
42
        return $command;
43
    }
44
45
    /**
46
     * Execute the given command by displaying console output live to the user.
47
     *
48
     * @param  string|array $cmd :  command to be executed
49
     * @return array   exit_status  :  exit status of the executed command
50
     *                  output       :  console output of the executed command
51
     * @throws ConfigNotFoundException
52
     * @throws EnvironmentException
53
     * @throws KeyNotFoundException
54
     * @throws InvalidArgumentException
55
     */
56
    protected function liveExecuteCommand($cmd)
57
    {
58
        // while (@ ob_end_flush()); // end all output buffers if any
59
60
        if (is_array($cmd)) {
61
            foreach ($cmd as $item) {
62
                $this->liveExecuteCommand($item);
63
            }
64
            return null;
65
        }
66
67
        $cmd = $this->replaceVariables($cmd);
68
        echo "\n>> $cmd\n";
69
70
        $complement = " 2>&1 ; echo Exit status : $?";
71
        if ($this->getSystemOs() === "Windows") {
72
            $complement = ' & echo Exit status : %errorlevel%';
73
        }
74
        $proc = popen("$cmd $complement", 'r');
75
76
        $completeOutput = "";
77
78
        while (!feof($proc)) {
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        while (!feof(/** @scrutinizer ignore-type */ $proc)) {
Loading history...
79
            $liveOutput     = fread($proc, 4096);
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            $liveOutput     = fread(/** @scrutinizer ignore-type */ $proc, 4096);
Loading history...
80
            $completeOutput = $completeOutput . $liveOutput;
81
            echo "$liveOutput";
82
            @ flush();
0 ignored issues
show
Bug introduced by
Are you sure the usage of flush() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

82
            /** @scrutinizer ignore-unhandled */ @ flush();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
83
        }
84
85
        pclose($proc);
0 ignored issues
show
Bug introduced by
It seems like $proc can also be of type false; however, parameter $handle of pclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        pclose(/** @scrutinizer ignore-type */ $proc);
Loading history...
86
87
        // get exit status
88
        preg_match('/[0-9]+$/', $completeOutput, $matches);
89
90
        $exitStatus = intval($matches[0]);
91
        // if ($exitStatus !== 0) {
92
        //     exit($exitStatus);
93
        // }
94
95
        // return exit status and intended output
96
        return array (
97
            'exit_status'  => $exitStatus,
98
            'output'       => str_replace("Exit status : " . $matches[0], '', $completeOutput)
99
        );
100
    }
101
102
    /**
103
     * @param string|Closure $variableValue
104
     * @return mixed
105
     * @throws ConfigNotFoundException
106
     * @throws EnvironmentException
107
     * @throws KeyNotFoundException
108
     * @throws InvalidArgumentException
109
     */
110
    protected function replaceVariables($variableValue)
111
    {
112
        $args = [];
113
        if (preg_match_all("/%[\\w\\d]+%/", $variableValue, $args)) {
114
            foreach ($args[0] as $arg) {
115
                $variableValue = str_replace(
116
                    $arg,
117
                    Psr11::container()->get(substr($arg,1, -1)),
118
                    $variableValue
119
                );
120
            }
121
        };
122
123
        return $variableValue;
124
    }
125
}
126