GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 8854b7...a3174d )
by François
02:32
created

YamlFile::readConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 * Copyright 2016 François Kooman <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace fkooman\RemoteStorage\Config;
20
21
use RuntimeException;
22
use Symfony\Component\Yaml\Dumper;
23
use Symfony\Component\Yaml\Yaml;
24
25
class YamlFile implements ReaderInterface, WriterInterface
26
{
27
    /** @var array */
28
    private $configFile;
29
30
    public function __construct($configFile)
31
    {
32
        if (!is_array($configFile)) {
33
            $configFile = [$configFile];
34
        }
35
36
        $this->configFile = $configFile;
37
    }
38
39
    public function readConfig()
40
    {
41
        foreach ($this->configFile as $configFile) {
42
            $fileContent = @file_get_contents($configFile);
43
            if (false !== $fileContent) {
44
                return Yaml::parse($fileContent);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Componen...l::parse($fileContent); (string|array|stdClass) is incompatible with the return type declared by the interface fkooman\RemoteStorage\Co...erInterface::readConfig of type array.

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...
45
            }
46
        }
47
48
        throw new RuntimeException(sprintf('unable to read configuration file(s) "%s"', implode(',', $this->configFile)));
49
    }
50
51
    public function writeConfig(array $config)
52
    {
53
        $dumper = new Dumper();
54
        $yamlStr = $dumper->dump($config, 3);
55
        foreach ($this->configFile as $configFile) {
56
            if (false !== @file_put_contents($configFile, $yamlStr)) {
57
                return;
58
            }
59
        }
60
61
        throw new RuntimeException(sprintf('unable to write configuration file(s) "%s"', implode(',', $this->configFile)));
62
    }
63
}
64