|
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; |
|
|
|
|
|
|
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
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.