Completed
Push — master ( d6fd04...fc71a7 )
by Nikolay
10s
created

DefaultMessagePrinter::getMessagesString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Kolyunya\Codeception\Lib\MarkupValidator;
4
5
use Kolyunya\Codeception\Lib\Base\Component;
6
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorMessageInterface;
7
use Kolyunya\Codeception\Lib\MarkupValidator\MessagePrinterInterface;
8
9
/**
10
 * Default markup validator message printer.
11
 */
12
class DefaultMessagePrinter extends Component implements MessagePrinterInterface
13
{
14
    /**
15
     * Placeholder for unavailable message data.
16
     */
17
    const UNAVAILABLE_DATA_PLACEHOLDER = 'unavailable';
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function getMessageString(MarkupValidatorMessageInterface $message)
23
    {
24
        return vsprintf($this->getMessageStringTemplate(), array(
25
            $message->getType(),
26
            $message->getSummary() ?: self::UNAVAILABLE_DATA_PLACEHOLDER,
27
            $message->getDetails() ?: self::UNAVAILABLE_DATA_PLACEHOLDER,
28
            $message->getFirstLineNumber() ?: self::UNAVAILABLE_DATA_PLACEHOLDER,
29
            $message->getLastLineNumber() ?: self::UNAVAILABLE_DATA_PLACEHOLDER,
30
            $message->getMarkup() ?: self::UNAVAILABLE_DATA_PLACEHOLDER,
31
        ));
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function getMessagesString(array $messages)
38
    {
39
        $messagesStrings = array_map(array($this, 'getMessageString'), $messages);
40
        $messagesString = implode("\n", $messagesStrings);
41
42
        return $messagesString;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $messagesString; (string) is incompatible with the return type declared by the interface Kolyunya\Codeception\Lib...face::getMessagesString of type Kolyunya\Codeception\Lib...datorMessageInterface[].

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...
43
    }
44
45
    /**
46
     * Returns message string representation template.
47
     *
48
     * @return string Message string representation template.
49
     */
50
    protected function getMessageStringTemplate()
51
    {
52
        return
53
            <<<TXT
54
Markup validator message:
55
Type: %s
56
Summary: %s
57
Details: %s
58
First Line: %s
59
Last Line: %s
60
Markup: %s
61
62
TXT
63
        ;
64
    }
65
}
66