TravisYMLHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 95
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A saveTravis() 0 4 1
A loadTravis() 0 9 2
A checkForExistingInEnv() 0 13 3
A ensurePathExistsInYaml() 0 13 3
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\PHPTravisEnhancer\Helper;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class TravisYMLHelper
8
{
9
10
    /** @var string */
11
    private $travisFileName = '.travis.yml';
12
13
    /**
14
     * TravisYMLHelper constructor.
15
     *
16
     * @param string $travisFileName The name of the travis config file, by default .travis.yml, but injectable for
17
     * testing purposes
18
     */
19
    public function __construct(string $travisFileName = '.travis.yml')
20
    {
21
        $this->travisFileName = $travisFileName;
22
    }
23
24
25
    /**
26
     * Ensure that a hierarchy exists in a PHP array. Pass the hierarchy in the form matrix/include, this will populate
27
     * the path an empty leaf array
28
     *
29
     * @param array<string, string|array> $yamlAsArray YAML file converted into an array. Can of course be any
30
     *          associative array
31
     * @param string $path The hierarchy required to exist, in the form matrix/include (forward slash separated)
32
     */
33
    public function ensurePathExistsInYaml(array &$yamlAsArray, string $path): void
34
    {
35
        $pathParts = \explode('/', $path);
36
        $part = \array_shift($pathParts);
37
        if (!isset($yamlAsArray[$part])) {
38
            $yamlAsArray[$part] = [];
39
        }
40
        $remainingPath = \implode('/', $pathParts);
41
        if (\sizeof($pathParts) === 0) {
42
            return;
43
        }
44
45
        $this->ensurePathExistsInYaml($yamlAsArray[$part], $remainingPath);
0 ignored issues
show
Bug introduced by
It seems like $yamlAsArray[$part] can also be of type string; however, parameter $yamlAsArray of Suilven\PHPTravisEnhance...nsurePathExistsInYaml() does only seem to accept array, 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

45
        $this->ensurePathExistsInYaml(/** @scrutinizer ignore-type */ $yamlAsArray[$part], $remainingPath);
Loading history...
46
    }
47
48
49
    /**
50
     * Check for an existing entry of the bash variable in the Travis matrix
51
     *
52
     * @todo What is the correct annotation to prevent this error?
53
     * @psalm-suppress PossiblyInvalidArrayOffset
54
     * @param array<string, string|array> $yamlAsArray Yaml parsed into an array
55
     * @param string $flag a bash variable flag, such as DUPLICATE_CHECK
56
     * @return bool true if an existing environment setting exists for this variable
57
     */
58
    public function checkForExistingInEnv(array $yamlAsArray, string $flag): bool
59
    {
60
        $foundExisting = false;
61
        foreach ($yamlAsArray['matrix']['include'] as $entry) {
62
            $env = $entry['env'];
63
            if ($env !== ($flag . '=1')) {
64
                continue;
65
            }
66
67
            $foundExisting = true;
68
        }
69
70
        return $foundExisting;
71
    }
72
73
74
    /**
75
     * Load the Travis file, or default to an empty array if it does not exist
76
     *
77
     * @todo Change this behavior?
78
     * @return array<string, string|array> The Travis YAML file as an array (note Symfony returns mixed)
79
     */
80
    public function loadTravis(): array
81
    {
82
        $result = [];
83
        $path = \getcwd() . '/' . $this->travisFileName;
84
        if (\file_exists($this->travisFileName)) {
85
            $result = Yaml::parseFile($path);
86
        }
87
88
        return $result;
89
    }
90
91
92
    /**
93
     * Save a travis file, default .travis.yml, in the root of a project
94
     *
95
     * @todo How does one specifiy this as an associative array?
96
     * @param array<string, string|array> $yamlArray an array that ought to have been formed from a YAML file
97
     */
98
    public function saveTravis(array $yamlArray): void
99
    {
100
        $yaml = Yaml::dump($yamlArray, Yaml::DUMP_OBJECT_AS_MAP);
101
        \file_put_contents($this->travisFileName, $yaml);
102
    }
103
}
104