Completed
Branch master (6407fd)
by Gordon
04:43 queued 02:45
created

AddDuplicationCheckTask::ensurePathExistsInYaml()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 13
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\PHPTravisEnhancer;
4
5
use Suilven\PHPTravisEnhancer\Helper\TravisYMLHelper;
6
7
class AddDuplicationCheckTask
8
{
9
10
    /**
11
     * Update Travis file to incorporate a check for duplicate code
12
     *
13
     * @param string $travisFile An injectable filename (for testing), leave blank for default of .travis.yml
14
     */
15
    public function run(string $travisFile = '.travis.yml'): void
16
    {
17
        $helper = new TravisYMLHelper($travisFile);
18
        $yamlAsArray = $helper->loadTravis();
19
20
        $this->ensurePathExistsInYaml($yamlAsArray, 'matrix/include');
21
22
        $foundExisting = false;
23
        foreach ($yamlAsArray['matrix']['include'] as $entry) {
24
            $env = $entry['env'];
25
            if ($env !== 'DUPLICATE_CODE_CHECK=1') {
26
                continue;
27
            }
28
29
            $foundExisting = true;
30
        }
31
32
        if ($foundExisting) {
33
            return;
34
        } else {
35
            // add a matrix entry
36
            $yamlAsArray['matrix']['include'][] = [
37
                'php' => 7.4,
38
                'env' => 'DUPLICATE_CODE_CHECK=1',
39
            ];
40
41
            // install jdscpd, node tool, for duplication detection
42
            $this->ensurePathExistsInYaml($yamlAsArray, 'before_script');
43
            $yamlAsArray['before_script'][] = 'if [[ $DUPLICATE_CODE_CHECK ]]; then sudo apt remove -y nodejs && curl '
44
                . '-sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh && sudo bash nodesource_setup.sh '
45
                . '&& sudo apt install -y build-essential nodejs && which npm && npm install [email protected]  ;fi';
46
47
            // run jscpd on src and tests dir
48
            $this->ensurePathExistsInYaml($yamlAsArray, 'script');
49
            $yamlAsArray['script'][] = 'if [[ $DUPLICATE_CODE_CHECK ]]; then node_modules/jscpd/bin/jscpd src && '
50
                . 'node_modules/jscpd/bin/jscpd tests ; fi';
51
        }
52
53
        $helper->saveTravis($yamlAsArray);
54
    }
55
56
57
    /**
58
     * Ensure that a hierarchy exists in a PHP array. Pass the hierarchy in the form matrix/include, this will populate
59
     * the path an empty leaf array
60
     *
61
     * @param array<string> $yamlAsArray YAML file converted into an array. Can of course be any associative array
62
     * @param string $path The hierarchy required to exist, in the form matrix/include (forward slash separated)
63
     */
64
    private function ensurePathExistsInYaml(array &$yamlAsArray, string $path): void
65
    {
66
        $pathParts = \explode('/', $path);
67
            $part = \array_shift($pathParts);
68
        if (!isset($yamlAsArray[$part])) {
69
            $yamlAsArray[$part] = [];
70
        }
71
            $remainingPath = \implode('/', $pathParts);
72
        if (\sizeof($pathParts) === 0) {
73
            return;
74
        }
75
76
        $this->ensurePathExistsInYaml($yamlAsArray[$part], $remainingPath);
0 ignored issues
show
Bug introduced by
$yamlAsArray[$part] of type string is incompatible with the type array expected by parameter $yamlAsArray of Suilven\PHPTravisEnhance...nsurePathExistsInYaml(). ( Ignorable by Annotation )

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

76
        $this->ensurePathExistsInYaml(/** @scrutinizer ignore-type */ $yamlAsArray[$part], $remainingPath);
Loading history...
77
    }
78
}
79