Completed
Push — master ( bb7259...dc298a )
by Sam
03:00
created

YamlReader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 41
ccs 7
cts 9
cp 0.7778
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A readConfiguration() 0 16 3
1
<?php
2
3
namespace Jalle19\StatusManager\Configuration\Reader;
4
5
use Jalle19\StatusManager\Exception\InvalidConfigurationException;
6
use Symfony\Component\Yaml\Exception\ParseException;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Class YamlReader
11
 * @package   Jalle19\StatusManager\Configuration\Reader
12
 * @copyright Copyright &copy; Sam Stenvall 2016-
13
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
14
 */
15
class YamlReader implements ReaderInterface
16
{
17
18
	/**
19
	 * @var string
20
	 */
21
	private $_file;
22
23
24
	/**
25
	 * YamlReader constructor.
26
	 *
27
	 * @param string $file
28
	 */
29 1
	public function __construct($file)
30
	{
31 1
		$this->_file = $file;
32 1
	}
33
34
35
	/**
36
	 * @inheritdoc
37
	 */
38 1
	public function readConfiguration()
39
	{
40
		// Check that the configuration file is readable
41 1
		if (!is_readable($this->_file))
42 1
			throw new InvalidConfigurationException('The configuration file does not exist or is not readable');
43
44
		// Parse the configuration file
45
		try
46
		{
47 1
			return Yaml::parse(file_get_contents($this->_file));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Componen...ontents($this->_file)); (array|string|stdClass) is incompatible with the return type declared by the interface Jalle19\StatusManager\Co...face::readConfiguration 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...
48
		}
49
		catch (ParseException $e)
50
		{
51
			throw new InvalidConfigurationException('Failed to parse the specified configuration file: ' . $e->getMessage());
52
		}
53
	}
54
55
}
56