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 |
||
12 | class ErrorsDocument extends Document { |
||
13 | /** @var ErrorObject[] */ |
||
14 | protected $errors = []; |
||
15 | /** @var array */ |
||
16 | protected $httpStatusCodes; |
||
17 | /** @var array */ |
||
18 | protected static $defaults = [ |
||
19 | /** |
||
20 | * add the trace of exceptions when adding exceptions |
||
21 | * in some cases it might be handy to disable if traces are too big |
||
22 | */ |
||
23 | 'includeExceptionTrace' => true, |
||
24 | |||
25 | /** |
||
26 | * add previous exceptions as separate errors when adding exceptions |
||
27 | */ |
||
28 | 'includeExceptionPrevious' => true, |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @param ErrorObject $errorObject optional |
||
33 | */ |
||
34 | public function __construct(ErrorObject $errorObject=null) { |
||
41 | |||
42 | /** |
||
43 | * human api |
||
44 | */ |
||
45 | |||
46 | /** |
||
47 | * @param \Exception|\Throwable $exception |
||
48 | * @param array $options optional {@see ErrorsDocument::$defaults} |
||
49 | * @return ErrorsDocument |
||
50 | * |
||
51 | * @throws InputException if $exception is not \Exception or \Throwable |
||
52 | */ |
||
53 | public static function fromException($exception, array $options=[]) { |
||
65 | |||
66 | /** |
||
67 | * add an ErrorObject for the given $exception |
||
68 | * |
||
69 | * recursively adds multiple ErrorObjects if $exception carries a ->getPrevious() |
||
70 | * |
||
71 | * @param \Exception|\Throwable $exception |
||
72 | * @param array $options optional {@see ErrorsDocument::$defaults} |
||
73 | * |
||
74 | * @throws InputException if $exception is not \Exception or \Throwable |
||
75 | */ |
||
76 | public function addException($exception, array $options=[]) { |
||
93 | |||
94 | /** |
||
95 | * @param string|int $genericCode developer-friendly code of the generic type of error |
||
96 | * @param string $genericTitle human-friendly title of the generic type of error |
||
97 | * @param string $specificDetails optional, human-friendly explanation of the specific error |
||
98 | * @param string $specificAboutLink optional, human-friendly explanation of the specific error |
||
99 | * @param string $genericTypeLink optional, human-friendly explanation of the generic type of error |
||
100 | */ |
||
101 | public function add($genericCode, $genericTitle, $specificDetails=null, $specificAboutLink=null, $genericTypeLink=null) { |
||
106 | |||
107 | /** |
||
108 | * spec api |
||
109 | */ |
||
110 | |||
111 | /** |
||
112 | * @note also defines the http status code of the document if the ErrorObject has it defined |
||
113 | * |
||
114 | * @param ErrorObject $errorObject |
||
115 | */ |
||
116 | public function addErrorObject(ErrorObject $errorObject) { |
||
123 | |||
124 | /** |
||
125 | * DocumentInterface |
||
126 | */ |
||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | public function toArray() { |
||
145 | |||
146 | /** |
||
147 | * internal api |
||
148 | */ |
||
149 | |||
150 | /** |
||
151 | * @internal |
||
152 | * |
||
153 | * @param string|int $httpStatusCode |
||
154 | * @return int |
||
155 | */ |
||
156 | protected function determineHttpStatusCode($httpStatusCode) { |
||
189 | } |
||
190 |