Total Complexity | 6 |
Total Lines | 43 |
Duplicated Lines | 100 % |
Coverage | 87.5% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
10 | View Code Duplication | class ApplicationException extends \LogicException |
|
|
|||
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 例外オブジェクト |
||
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 |
|
53 | } |
||
54 | } |
||
55 |
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.