Passed
Branch master (d63766)
by Ryuichi
02:15
created

ApplicationException::__construct()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 5.0909

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 12
nop 3
dl 20
loc 20
ccs 11
cts 13
cp 0.8462
crap 5.0909
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Exception;
3
4
/**
5
 * ApplicationException
6
 * @author Ryuichi TANAKA.
7
 * @since 2014/05/05
8
 * @version 0.4
9
 */
10 View Code Duplication
class ApplicationException extends \LogicException
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @var string エラーメッセージ
14
     */
15
    private $errorMessage;
16
17
    /**
18
     * constructor
19
     * @param string $message エラーメッセージ
20
     * @param int $code ステータスコード
21
     * @param Exception $exception 例外オブジェクト
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
22
     */
23 4
    public function __construct($message, $code = 500, $exception = null)
24
    {
25 4
        parent::__construct($message, $code);
26
27 4
        if ($exception === null) {
28
            $exception = $this;
29
        }
30
31 4
        if (!empty($message)) {
32
            $message .= " ";
33
        }
34
35 4
        $this->errorMessage = get_class($exception) . " is thrown: " . $message . $exception->getFile() . "(" . $exception->getLine() . ")";
36 4
        $stacktraceList = explode("#", $exception->getTraceAsString());
37 4
        foreach ($stacktraceList as $stacktraceLine) {
38 4
            if ($stacktraceLine === "") {
39 4
                continue;
40
            }
41 4
            $this->errorMessage .= PHP_EOL;
42 4
            $this->errorMessage .= "\t#" . trim($stacktraceLine);
43
        }
44 4
    }
45
46
    /**
47
     * エラーメッセージを返却する
48
     * @return string エラーメッセージ
49
     */
50 14
    public function getExceptionAsString(): string
51
    {
52 14
        return $this->errorMessage;
53
    }
54
}
55