Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

OutputContainer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 86
ccs 18
cts 24
cp 0.75
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileNames() 0 5 1
A getOutputBuffer() 0 4 1
A getTag() 0 4 1
A getTitle() 0 4 1
A __construct() 0 6 1
A addToOutputBuffer() 0 4 1
A countFiles() 0 5 1
A countMessages() 0 9 2
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Process\ProcessResultInterface;
6
7
/**
8
 * Class OutputContainer.
9
 */
10
class OutputContainer implements OutputContainerInterface
11
{
12
    /** @var string[] */
13
    protected $outputBuffer;
14
15
    /** @var string */
16
    protected $tag;
17
18
    /** @var string */
19
    protected $title;
20
21
    /**
22
     * @param string $tag
23
     * @param string $title
24
     */
25 20
    public function __construct($tag, $title)
26
    {
27 20
        $this->tag = $tag;
28 20
        $this->title = $title;
29 20
        $this->outputBuffer = array();
30 20
    }
31
32
    /**
33
     * @param ProcessResultInterface $process
34
     * @param string $message
35
     */
36 18
    public function addToOutputBuffer(ProcessResultInterface $process, $message)
37
    {
38 18
        $this->outputBuffer[$process->getFilename()][] = $message;
39 18
    }
40
41
    /**
42
     * @return \string[]
43
     */
44 10
    public function getFileNames()
45
    {
46
        // TODO test
47 10
        return array_keys($this->outputBuffer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_keys($this->outputBuffer); (integer[]) is incompatible with the return type declared by the interface Paraunit\Printer\OutputC...Interface::getFileNames of type string[].

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
50
    /**
51
     * @return string[]
52
     */
53 11
    public function getOutputBuffer()
54
    {
55 11
        return $this->outputBuffer;
56
    }
57
58
    /**
59
     * @return int
60
     */
61 9
    public function countFiles()
62
    {
63
        // TODO test
64 9
        return count($this->outputBuffer);
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function countMessages()
71
    {
72
        $messageCount = 0;
73
        foreach ($this->outputBuffer as $fileName => $fileMessages) {
74
            $messageCount += count($fileMessages);
75
        }
76
77
        return $messageCount;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 8
    public function getTag()
84
    {
85 8
        return $this->tag;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 8
    public function getTitle()
92
    {
93 8
        return $this->title;
94
    }
95
}
96