Passed
Push — master ( 6407fd...37d291 )
by Gordon
04:05 queued 01:59
created

TaskBase::run()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 36
rs 9.6666
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\PHPTravisEnhancer\Abstraction;
4
5
use Suilven\PHPTravisEnhancer\Helper\TravisYMLHelper;
6
use Suilven\PHPTravisEnhancer\IFace\Task;
7
8
abstract class TaskBase implements Task
9
{
10
    abstract public function getFlag(): string;
11
12
13
    /**
14
     * Update Travis file to incorporate a check for duplicate code
15
     *
16
     * @todo What is the correct annotation to prevent this error?
17
     * @psalm-suppress PossiblyInvalidArrayOffset
18
     * @psalm-suppress PossiblyNullOperand - nulls are checked for
19
     * @param string $travisFile An injectable filename (for testing), leave blank for default of .travis.yml
20
     */
21
    public function run(string $travisFile = '.travis.yml'): void
22
    {
23
        $helper = new TravisYMLHelper($travisFile);
24
25
        /** @var array<string, string|array> $yamlAsArray */
26
        $yamlAsArray = $helper->loadTravis();
27
28
        $helper->ensurePathExistsInYaml($yamlAsArray, 'matrix/include');
29
30
        $foundExisting = $helper->checkForExistingInEnv($yamlAsArray, $this->getFlag());
31
        if (!$foundExisting) {
32
            // add a matrix entry
33
            $yamlAsArray['matrix']['include'][] = [
34
                'php' => 7.4,
35
                'env' => $this->getFlag() . '=1',
36
            ];
37
38
            /** @var string $prefix the bash prefix to check for the flag being set */
39
            $prefix = 'if [[ $' . $this->getFlag() .' ]]; then ';
40
41
            $beforeScript = $this->getBeforeScript();
42
            if (isset($beforeScript)) {
43
                // install jdscpd, node tool, for duplication detection
44
                $helper->ensurePathExistsInYaml($yamlAsArray, 'before_script');
45
                $yamlAsArray['before_script'][] = $prefix . $beforeScript . '  ;fi';
46
            }
47
48
            $script = $this->getScript();
49
            if (isset($script)) {
50
                // run jscpd on src and tests dir
51
                $helper->ensurePathExistsInYaml($yamlAsArray, 'script');
52
                $yamlAsArray['script'][] = $prefix . $this->getScript() . ' ; fi';
53
            }
54
        }
55
56
        $helper->saveTravis($yamlAsArray);
57
    }
58
}
59