Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

OutputContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A OutputContainer::getFileNames() 0 4 1
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Process\ProcessResultInterface;
6
7
/**
8
 * Class OutputContainer.
9
 */
10
class OutputContainer extends AbstractOutputContainer implements OutputContainerInterface
11
{
12
    /** @var string[] */
13
    protected $outputBuffer;
14
15
    /**
16
     * @param ProcessResultInterface $process
17
     * @param string $message
18
     */
19 19
    public function addToOutputBuffer(ProcessResultInterface $process, $message)
20
    {
21 19
        $this->outputBuffer[$process->getFilename()][] = $message;
22 19
    }
23
24
    /**
25
     * @return string[]
26
     */
27 10
    public function getFileNames()
28
    {
29 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...
30
    }
31
32
    /**
33
     * @return string[][]
34
     */
35 14
    public function getOutputBuffer()
36
    {
37 14
        return $this->outputBuffer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->outputBuffer; (string[]) is incompatible with the return type declared by the interface Paraunit\Printer\OutputC...erface::getOutputBuffer 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...
38
    }
39
40
    /**
41
     * @return int
42
     */
43 9
    public function countFiles()
44
    {
45 9
        return count($this->outputBuffer);
46
    }
47
48
    /**
49
     * @return int
50
     */
51
    public function countMessages()
52
    {
53
        $messageCount = 0;
54
        foreach ($this->outputBuffer as $fileName => $fileMessages) {
55
            $messageCount += count($fileMessages);
56
        }
57
58
        return $messageCount;
59
    }
60
}
61