Completed
Push — scrutinizer-quality ( c3b7e2 )
by Erin
02:15
created

Formatter::objectToString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 10
rs 9.2
1
<?php
2
3
namespace Peridot\Leo\Formatter;
4
5
use Peridot\Leo\Matcher\Match;
6
use Peridot\Leo\Matcher\Template\TemplateInterface;
7
8
/**
9
 * Class Formatter is the core FormatterInterface for Leo.
10
 *
11
 * @package Peridot\Leo\Formatter
12
 */
13
class Formatter implements FormatterInterface
14
{
15
    /**
16
     * @var Match
17
     */
18
    protected $match;
19
20
    public function __construct()
21
    {
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @return Match
28
     */
29
    public function getMatch()
30
    {
31
        return $this->match;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @param  Match $match
38
     * @return $this
39
     */
40
    public function setMatch(Match $match)
41
    {
42
        $this->match = $match;
43
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @param  TemplateInterface $template
51
     * @return mixed|string
52
     */
53
    public function getMessage(TemplateInterface $template)
54
    {
55
        $vars = $this->getTemplateVars($template);
56
57
        $tpl = $this->match->isNegated()
58
            ? $template->getNegatedTemplate()
59
            : $template->getDefaultTemplate();
60
61
        foreach ($vars as $name => $value) {
62
            $tpl = str_replace('{{' . $name . '}}', $this->objectToString($value), $tpl);
63
        }
64
65
        return $tpl;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @param  mixed  $obj
72
     * @return string
73
     */
74
    public function objectToString($obj)
75
    {
76
        switch (gettype($obj)) {
77
            case 'boolean': return var_export($obj, true);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
78
            case 'NULL': return 'null';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
79
            case 'string': return '"' . $obj . '"';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
80
        }
81
82
        return rtrim(print_r($obj, true));
83
    }
84
85
    /**
86
     * Applies match results to other template variables.
87
     *
88
     * @param  TemplateInterface $template
89
     * @return array
90
     */
91
    protected function getTemplateVars(TemplateInterface $template)
92
    {
93
        $vars = [
94
            'expected' => $this->match->getExpected(),
95
            'actual' => $this->match->getActual(),
96
        ];
97
98
        if ($tplVars = $template->getTemplateVars()) {
99
            $vars = array_merge($vars, $tplVars);
100
        }
101
102
        return $vars;
103
    }
104
}
105