Completed
Push — fix-hhvm-build ( fd57d7 )
by Erin
02:12
created

Formatter::objectToString()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 12
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 24
rs 6.7272
1
<?php
2
3
namespace Peridot\Leo\Formatter;
4
5
use Exception;
6
use Peridot\Leo\Matcher\Match;
7
use Peridot\Leo\Matcher\Template\TemplateInterface;
8
use Throwable;
9
10
/**
11
 * Class Formatter is the core FormatterInterface for Leo.
12
 *
13
 * @package Peridot\Leo\Formatter
14
 */
15
class Formatter implements FormatterInterface
16
{
17
    /**
18
     * @var Match
19
     */
20
    protected $match;
21
22
    public function __construct()
23
    {
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @return Match
30
     */
31
    public function getMatch()
32
    {
33
        return $this->match;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @param  Match $match
40
     * @return $this
41
     */
42
    public function setMatch(Match $match)
43
    {
44
        $this->match = $match;
45
46
        return $this;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @param  TemplateInterface $template
53
     * @return mixed|string
54
     */
55
    public function getMessage(TemplateInterface $template)
56
    {
57
        $vars = $this->getTemplateVars($template);
58
59
        $tpl = $this->match->isNegated()
60
            ? $template->getNegatedTemplate()
61
            : $template->getDefaultTemplate();
62
63
        foreach ($vars as $name => $value) {
64
            $tpl = str_replace('{{' . $name . '}}', $this->objectToString($value), $tpl);
65
        }
66
67
        return $tpl;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @param  mixed  $obj
74
     * @return string
75
     */
76
    public function objectToString($obj)
77
    {
78
        if ($obj === false) {
79
            return 'false';
80
        }
81
82
        if ($obj === true) {
83
            return 'true';
84
        }
85
86
        if ($obj === null) {
87
            return 'null';
88
        }
89
90
        if (is_string($obj)) {
91
            return '"' . $obj . '"';
92
        }
93
94
        if ($obj instanceof Throwable || $obj instanceof Exception) {
95
            return get_class($obj) . ' Exception("' . $obj->getMessage() . '")';
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in Exception, but not in Throwable.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
96
        }
97
98
        return rtrim(print_r($obj, true));
99
    }
100
101
    /**
102
     * Applies match results to other template variables.
103
     *
104
     * @param  TemplateInterface $template
105
     * @return array
106
     */
107
    protected function getTemplateVars(TemplateInterface $template)
108
    {
109
        $vars = [
110
            'expected' => $this->match->getExpected(),
111
            'actual' => $this->match->getActual(),
112
        ];
113
114
        if ($tplVars = $template->getTemplateVars()) {
115
            $vars = array_merge($vars, $tplVars);
116
        }
117
118
        return $vars;
119
    }
120
}
121