Conditions | 10 |
Paths | 12 |
Total Lines | 70 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
71 | protected function haltHttp(array $errorInfo = []): bool |
||
72 | { |
||
73 | switch ($errorInfo['code']) { |
||
74 | case 404: |
||
75 | $statusCode = 404; //not found |
||
76 | $title = 'Resource not found'; |
||
77 | break; |
||
78 | case 500: //application |
||
79 | case 0: //default |
||
80 | default: |
||
81 | $statusCode = 500; |
||
82 | $title = 'Boo boo'; |
||
83 | break; |
||
84 | } |
||
85 | |||
86 | $output = '<!doctype html> |
||
87 | <html> |
||
88 | <head> |
||
89 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||
90 | <title>Oops</title> |
||
91 | <style> |
||
92 | * {background: #f2dede; color: #a94442; overflow-wrap: break-word;} |
||
93 | .i {margin-left: auto; margin-right: auto; text-align: center; width: auto;} |
||
94 | small {font-size: 0.8em;} |
||
95 | </style> |
||
96 | </head> |
||
97 | <body><div class="i"><br>' . |
||
98 | "<h1>{$title}</h1>"; |
||
99 | // not using "Config" here because we just want to stop and display a message, not do more validation. |
||
100 | if (Value::DEVELOPMENT === ($_SERVER['APP_ENVIRONMENT'] ?? Value::DEVELOPMENT)) { |
||
101 | $output .= \sprintf( |
||
102 | '<p><i>%s</i></p><p>%s:%s</p>', |
||
103 | $errorInfo['message'], |
||
104 | \basename($errorInfo['file']), |
||
105 | $errorInfo['line'], |
||
106 | ); |
||
107 | if (!empty($errorInfo['trace'])) { |
||
108 | $output .= "<p>"; |
||
109 | $output .= "<small>"; |
||
110 | foreach ($errorInfo['trace'] as $item) { |
||
111 | if (!empty($item['class'])) { |
||
112 | $output .= \sprintf('%s%s', $item['class'], $item['type']); |
||
113 | $output .= ""; |
||
114 | } |
||
115 | if (!empty($item['function'])) { |
||
116 | $output .= \sprintf('%s', $item['function']); |
||
117 | $output .= ""; |
||
118 | } |
||
119 | if (!empty($item['file'])) { |
||
120 | $output .= \sprintf( |
||
121 | ' [%s:%s]', |
||
122 | \basename($item['file']), |
||
123 | $item['line'], |
||
124 | ); |
||
125 | $output .= " "; |
||
126 | } |
||
127 | $output .= "<br>"; |
||
128 | } |
||
129 | $output .= "</small></p>"; |
||
130 | } |
||
131 | } |
||
132 | $output .= '</div></body></html>'; |
||
133 | |||
134 | $response = new \WebServCo\Framework\Http\Response( |
||
135 | $output, |
||
136 | $statusCode, |
||
137 | ['Content-Type' => ['text/html']], |
||
138 | ); |
||
139 | $response->send(); |
||
140 | return true; |
||
141 | } |
||
161 |