Completed
Pull Request — master (#82)
by Jan Philipp
01:31
created

BashScriptParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseContent() 0 13 2
A testScriptFileFitsRequirements() 0 10 3
A testContentContainsMarker() 0 6 2
A shouldWarnAboutBestPractice() 0 11 3
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\ScriptRuntime\ScriptLoader;
4
5
use Shopware\Psh\Listing\Script;
6
use Shopware\Psh\ScriptRuntime\BashCommand;
7
8
class BashScriptParser implements ScriptParser
9
{
10
    const TYPE_DIRECT_EXECUTE = '<PSH_EXECUTE_THROUGH_CMD>';
11
12
    /**
13
     * @todo add validation and notice
14
     */
15
    const SHOULD_BE_PRESENT = "set -euo pipefail";
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function parseContent(string $content, Script $script): array
21
    {
22
        $this->testContentContainsMarker($content);
23
        $this->testScriptFileFitsRequirements($script);
24
25
        $shouldWarn = $this->shouldWarnAboutBestPractice($content);
26
27
        if ($shouldWarn) {
28
            $warning = self::SHOULD_BE_PRESENT;
0 ignored issues
show
Unused Code introduced by
$warning is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
        }
30
31
        return [new BashCommand($script)];
32
    }
33
34
    /**
35
     * @param Script $script
36
     */
37
    private function testScriptFileFitsRequirements(Script $script)
38
    {
39
        if (!is_executable($script->getPath())) {
40
            throw new \RuntimeException('Bash scripts can only be executed if they are executable please execute <bold>chmod +x ' . $script->getPath() . '</bold>');
41
        }
42
43
        if (!is_writable($script->getDirectory())) {
44
            throw new \RuntimeException('Bash scripts can only be executed if they are in a writable directory please execute <bold>chmod +w ' . $script->getDirectory() . '</bold>');
45
        }
46
    }
47
48
    /**
49
     * @param string $content
50
     */
51
    private function testContentContainsMarker(string $content)
52
    {
53
        if (strpos($content, self::TYPE_DIRECT_EXECUTE) === false) {
54
            throw new ScriptNotSupportedByParser('Marker for execution missing');
55
        }
56
    }
57
58
    /**
59
     * @param string $content
60
     * @return bool
61
     */
62
    private function shouldWarnAboutBestPractice(string $content): bool
63
    {
64
        $firstLines = explode("\n", $content, 5);
65
        foreach ($firstLines as $line) {
66
            if ($line === self::SHOULD_BE_PRESENT) {
67
                return false;
68
            }
69
        }
70
71
        return true;
72
    }
73
}
74