Completed
Push — master ( ad90dd...5d8ebc )
by Jan Philipp
02:11
created

YamlConfigFileLoader::removeDistExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Config;
5
6
use Symfony\Component\Yaml\Parser;
7
8
/**
9
 * Load the config data from a yaml file
10
 */
11
class YamlConfigFileLoader implements ConfigLoader
12
{
13
    const KEY_HEADER = 'header';
14
15
    const KEY_DYNAMIC_VARIABLES = 'dynamic';
16
17
    const KEY_CONST_VARIABLES = 'const';
18
19
    const KEY_COMMAND_PATHS = 'paths';
20
21
    const KEY_ENVIRONMENTS = 'environments';
22
23
    const KEY_TEMPLATES = 'templates';
24
25
    /**
26
     * @var Parser
27
     */
28
    private $yamlReader;
29
30
    /**
31
     * @var ConfigBuilder
32
     */
33
    private $configBuilder;
34
35
    /**
36
     * @param Parser $yamlReader
37
     * @param ConfigBuilder $configBuilder
38
     */
39
    public function __construct(Parser $yamlReader, ConfigBuilder $configBuilder)
40
    {
41
        $this->yamlReader = $yamlReader;
42
        $this->configBuilder = $configBuilder;
43
    }
44
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function isSupported(string $file): bool
50
    {
51
        $file = $this->removeDistExtension($file);
52
        return pathinfo($file, PATHINFO_EXTENSION) === 'yaml' || pathinfo($file, PATHINFO_EXTENSION) === 'yml';
53
    }
54
55
    /**
56
     * @param string $file
57
     * @return string
58
     */
59
    private function removeDistExtension(string $file): string
60
    {
61
        $fileInfo = pathinfo($file);
62
        if ($fileInfo['extension'] === 'dist') {
63
            $file = $fileInfo['filename'];
64
        }
65
66
        return $file;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function load(string $file): Config
73
    {
74
        $contents = $this->loadFileContents($file);
75
        $rawConfigData = $this->parseFileContents($contents);
76
77
        $this->configBuilder->start();
78
79
        $this->configBuilder
80
            ->setHeader(
81
            $this->extractData(self::KEY_HEADER, $rawConfigData, '')
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        );
83
84
        $this->setConfigData($file, $rawConfigData);
85
86
        $environments = $this->extractData(self::KEY_ENVIRONMENTS, $rawConfigData, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        foreach ($environments as $name => $data) {
0 ignored issues
show
Bug introduced by
The expression $environments of type string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
88
            $this->configBuilder->start($name);
89
            $this->setConfigData($file, $data);
90
        }
91
92
        return $this->configBuilder
93
            ->create();
94
    }
95
96
    private function setConfigData(string $file, array $rawConfigData)
97
    {
98
        $this->configBuilder->setCommandPaths(
99
            $this->extractCommandPaths($file, $rawConfigData)
100
        );
101
102
        $this->configBuilder->setDynamicVariables(
103
            $this->extractData(self::KEY_DYNAMIC_VARIABLES, $rawConfigData, [])
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->extractData(self:...rawConfigData, array()) is of type string|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        );
105
106
        $this->configBuilder->setConstants(
107
            $this->extractData(self::KEY_CONST_VARIABLES, $rawConfigData, [])
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->extractData(self:...rawConfigData, array()) is of type string|null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
        );
109
110
        $this->configBuilder->setTemplates(
111
            array_map(function ($template) use ($file) {
112
                $template['source'] = $this->fixPath($template['source'], $file);
113
                $template['destination'] = $this->fixPath($template['destination'], $file);
114
115
                return $template;
116
            }, $this->extractData(self::KEY_TEMPLATES, $rawConfigData, []))
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
        );
118
    }
119
120
    /**
121
     * @param string $key
122
     * @param array $rawConfig
123
     * @param bool $default
124
     * @return string|null
125
     */
126
    private function extractData(string $key, array $rawConfig, $default = false)
127
    {
128
        if (!array_key_exists($key, $rawConfig)) {
129
            return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (boolean) is incompatible with the return type documented by Shopware\Psh\Config\Yaml...FileLoader::extractData of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
130
        }
131
132
        return $rawConfig[$key];
133
    }
134
135
    /**
136
     * @param string $file
137
     * @return string
138
     */
139
    private function loadFileContents(string $file): string
140
    {
141
        return file_get_contents($file);
142
    }
143
144
    /**
145
     * @param string $contents
146
     * @return array
147
     */
148
    private function parseFileContents(string $contents): array
149
    {
150
        return $this->yamlReader->parse($contents);
151
    }
152
153
    /**
154
     * @param string $file
155
     * @param $rawConfigData
156
     * @return array
157
     */
158
    private function extractCommandPaths(string $file, array $rawConfigData): array
159
    {
160
        $paths = $this->extractData(self::KEY_COMMAND_PATHS, $rawConfigData, []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
161
162
        return array_map(function ($path) use ($file) {
163
            return $this->fixPath($path, $file);
164
        }, $paths);
165
    }
166
167
    /**
168
     * @param string $absoluteOrRelativePath
169
     * @param string $baseFile
170
     * @return string
171
     */
172
    private function fixPath(string $absoluteOrRelativePath, string $baseFile): string
173
    {
174
        if (file_exists($absoluteOrRelativePath)) {
175
            return $absoluteOrRelativePath;
176
        }
177
178
        return pathinfo($baseFile, PATHINFO_DIRNAME) . '/' . $absoluteOrRelativePath;
179
    }
180
}
181