Completed
Push — master ( d40614...680e53 )
by Sébastien
10s
created

ExpectationFailed   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getContextText() 0 1 ?
A __toString() 0 11 2
A pipeString() 0 4 1
A trimString() 0 9 2
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension;
4
5
abstract class ExpectationFailed extends \Exception
6
{
7
    abstract function getContextText();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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, array("\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