ExpectationFailed::trimString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
nc 2
cc 2
eloc 5
nop 2
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension;
4
5
abstract class ExpectationFailed extends \Exception
6
{
7
    abstract public function getContextText();
8
9
    /**
10
     * Returns exception message with additional context info.
11
     *
12
     * @return string
13
     */
14
    public function __toString()
15
    {
16
        try {
17
            $contextText = $this->pipeString($this->trimString($this->getContextText())."\n");
18
            $string = sprintf("%s\n\n%s", $this->getMessage(), $contextText);
19
        } catch (\Exception $e) {
20
            return $this->getMessage();
21
        }
22
23
        return $string;
24
    }
25
26
    /**
27
     * Prepends every line in a string with pipe (|).
28
     *
29
     * @param string $string
30
     *
31
     * @return string
32
     */
33
    protected function pipeString($string)
34
    {
35
        return '|  '.strtr($string, ["\n" => "\n|  "]);
36
    }
37
38
    /**
39
     * Trims string to specified number of chars.
40
     *
41
     * @param string $string response content
42
     * @param int    $count  trim count
43
     *
44
     * @return string
45
     */
46
    protected function trimString($string, $count = 1000)
47
    {
48
        $string = trim($string);
49
        if ($count < mb_strlen($string)) {
50
            return mb_substr($string, 0, $count - 3).'...';
51
        }
52
53
        return $string;
54
    }
55
}
56