Complex classes like Response often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Response |
||
14 | { |
||
15 | use Injector, CommonUtils; |
||
16 | |||
17 | /** HTTPバージョン */ |
||
18 | const HTTP_VERSION = '1.1'; |
||
19 | /** 文字コード */ |
||
20 | private $charset = 'UTF-8'; |
||
21 | /** Cache-Control */ |
||
22 | private $cacheControl = 'no-cache'; |
||
23 | /** Pragma */ |
||
24 | private $pragma = 'no-cache'; |
||
25 | /** Mime type */ |
||
26 | private $mimeType = 'text/html'; |
||
27 | /** ロケーション */ |
||
28 | private $location; |
||
29 | /** Access-Control-Allow-Origin */ |
||
30 | private $accessControlAllowOrigin = []; |
||
31 | /** X-Frame-Options */ |
||
32 | private $xframeOptions = 'SAMEORIGIN'; |
||
33 | /** X-XSS-Protection */ |
||
34 | private $xxssProtection = '1; mode=block'; |
||
35 | /** ステータスコード */ |
||
36 | private $statusCode = 200; |
||
37 | /** レスポンスボディ */ |
||
38 | private $body; |
||
39 | /** レスポンスファイル */ |
||
40 | private $file; |
||
41 | /** Content-Length */ |
||
42 | private $contentLength; |
||
43 | /** Content-Disposition */ |
||
44 | private $contentDisposition; |
||
45 | /** Content-Transfer-Encoding */ |
||
46 | private $contentTransferEncoding; |
||
47 | /** Expires */ |
||
48 | private $expires; |
||
49 | |||
50 | /** |
||
51 | * デストラクタ |
||
52 | */ |
||
53 | public function __destruct() |
||
57 | |||
58 | /** |
||
59 | * 文字コードを設定 |
||
60 | * @param String 文字コード |
||
61 | */ |
||
62 | public function setCharset($charset) |
||
66 | |||
67 | /** |
||
68 | * Cache-Controlを設定 |
||
69 | * @param String Cache-Control |
||
70 | */ |
||
71 | public function setCacheControl($cacheControl) |
||
75 | |||
76 | /** |
||
77 | * Pragmaを設定 |
||
78 | * @param String Pragma |
||
79 | */ |
||
80 | public function setPragma($pragma) |
||
84 | |||
85 | /** |
||
86 | * MimeTypeを設定 |
||
87 | * ファイルタイプにより指定 |
||
88 | * @param String ファイルタイプ |
||
89 | */ |
||
90 | public function setType($fileType) |
||
99 | |||
100 | /** |
||
101 | * MimeTypeを設定 |
||
102 | * MimeTypeを直接指定 |
||
103 | * @param String MimeType |
||
104 | */ |
||
105 | public function setMimeType($mimeType) |
||
109 | |||
110 | /** |
||
111 | * リダイレクトロケーションを設定 |
||
112 | * @param String ロケーションパス |
||
113 | */ |
||
114 | public function setLocation($location) |
||
118 | |||
119 | /** |
||
120 | * Access-Control-Allow-Originを設定 |
||
121 | * 複数指定する場合は、引数に列挙する |
||
122 | * @param String URLまたはワイルドカード |
||
123 | */ |
||
124 | public function setAccessControlAllowOrigin() |
||
131 | |||
132 | /** |
||
133 | * X-Frame-Optionsを設定 |
||
134 | * @param String SAMEORIGINまたはDENY |
||
135 | */ |
||
136 | public function setXFrameOptions($xframeOptions) |
||
140 | |||
141 | /** |
||
142 | * X-XSS-Protectionを設定 |
||
143 | * @param String XSSフィルタ設定(0:無効、1:有効) |
||
144 | */ |
||
145 | public function setXXssProtection($xxssProtection) |
||
149 | |||
150 | /** |
||
151 | * ステータスコードを設定 |
||
152 | * @param Integer ステータスコード |
||
153 | */ |
||
154 | public function setStatusCode($statusCode) |
||
165 | |||
166 | /** |
||
167 | * Content-Lengthを設定 |
||
168 | * @param Integer Content-Length |
||
169 | */ |
||
170 | public function setContentLength($contentLength) |
||
174 | |||
175 | /** |
||
176 | * Content-Dispositionを設定 |
||
177 | * @param String ファイル名 |
||
178 | */ |
||
179 | public function setContentDisposition($filename) |
||
185 | |||
186 | /** |
||
187 | * Content-Transfer-Encodingを設定 |
||
188 | * @param String エンコーディング方法 |
||
189 | */ |
||
190 | public function setContentTransferEncoding($contentTransferEncoding) |
||
194 | |||
195 | /** |
||
196 | * Expiresを設定 |
||
197 | * @param Integer 有効期限 |
||
198 | */ |
||
199 | public function setExpires($expires) |
||
203 | |||
204 | /** |
||
205 | * レスポンスボディを設定 |
||
206 | * @param String レスポンスボディ |
||
207 | */ |
||
208 | public function setBody($body) |
||
212 | |||
213 | /** |
||
214 | * レスポンスファイルを設定 |
||
215 | * @param String レスポンスファイル |
||
216 | */ |
||
217 | public function setFile($file) |
||
221 | |||
222 | /** |
||
223 | * レスポンスを送出する |
||
224 | */ |
||
225 | public function send() |
||
230 | |||
231 | /** |
||
232 | * レスポンスヘッダを送出する |
||
233 | */ |
||
234 | public function header() |
||
300 | |||
301 | /** |
||
302 | * レスポンスボディを送出する |
||
303 | */ |
||
304 | public function body() |
||
316 | |||
317 | /** |
||
318 | * Mime-Type |
||
319 | */ |
||
320 | protected $mime = [ |
||
321 | 'txt' => 'text/plain', |
||
322 | 'jpeg' => 'image/jpeg', |
||
323 | 'jpg' => 'image/jpeg', |
||
324 | 'gif' => 'image/gif', |
||
325 | 'png' => 'image/png', |
||
326 | 'tiff' => 'image/tiff', |
||
327 | 'tif' => 'image/tiff', |
||
328 | 'bmp' => 'image/bmp', |
||
329 | 'ico' => 'image/x-icon', |
||
330 | 'svg' => 'image/svg+xml', |
||
331 | 'xml' => 'application/xml', |
||
332 | 'xsl' => 'application/xml', |
||
333 | 'rss' => 'application/rss+xml', |
||
334 | 'rdf' => 'application/rdf+xml', |
||
335 | 'atom' => 'application/atom+xml', |
||
336 | 'zip' => 'application/zip', |
||
337 | 'html' => 'text/html', |
||
338 | 'htm' => 'text/html', |
||
339 | 'css' => 'text/css', |
||
340 | 'csv' => 'text/csv', |
||
341 | 'tsv' => 'text/tab-separated-values', |
||
342 | 'js' => 'text/javascript', |
||
343 | 'jsonp' => 'text/javascript', |
||
344 | 'json' => 'application/json', |
||
345 | 'pdf' => 'application/pdf', |
||
346 | 'file' => 'application/octet-stream' |
||
347 | ]; |
||
348 | |||
349 | /** |
||
350 | * Status |
||
351 | */ |
||
352 | protected $status = [ |
||
353 | '100' => 'Continue', |
||
354 | '101' => 'Switching Protocols', |
||
355 | '102' => 'Processing', |
||
356 | '200' => 'OK', |
||
357 | '201' => 'Created', |
||
358 | '202' => 'Accepted', |
||
359 | '203' => 'Non-Authoritative Information', |
||
360 | '204' => 'No Content', |
||
361 | '205' => 'Reset Content', |
||
362 | '206' => 'Partial Content', |
||
363 | '207' => 'Multi-Status', |
||
364 | '208' => 'Already Reported', |
||
365 | '226' => 'IM Used', |
||
366 | '300' => 'Multiple Choices', |
||
367 | '301' => 'Moved Permanently', |
||
368 | '302' => 'Found', |
||
369 | '303' => 'See Other', |
||
370 | '304' => 'Not Modified', |
||
371 | '305' => 'Use Proxy', |
||
372 | '307' => 'Temporary Redirect', |
||
373 | '400' => 'Bad Request', |
||
374 | '401' => 'Unauthorized', |
||
375 | '402' => 'Payment Required', |
||
376 | '403' => 'Forbidden', |
||
377 | '404' => 'Not Found', |
||
378 | '405' => 'Method Not Allowed', |
||
379 | '406' => 'Not Acceptable', |
||
380 | '407' => 'Proxy Authentication Required', |
||
381 | '408' => 'Request Timeout', |
||
382 | '409' => 'Conflict', |
||
383 | '410' => 'Gone', |
||
384 | '411' => 'Length Required', |
||
385 | '412' => 'Precondition Failed', |
||
386 | '413' => 'Request Entity Too Large', |
||
387 | '414' => 'Request-URI Too Long', |
||
388 | '415' => 'Unsupported Media Type', |
||
389 | '416' => 'Requested Range Not Satisfiable', |
||
390 | '417' => 'Expectation Failed', |
||
391 | '418' => "I'm a teapot", |
||
392 | '422' => 'Unprocessable Entity', |
||
393 | '423' => 'Locked', |
||
394 | '424' => 'Failed Dependency', |
||
395 | '426' => 'Upgrade Required', |
||
396 | '500' => 'Internal Server Error', |
||
397 | '501' => 'Not Implemented', |
||
398 | '502' => 'Bad Gateway', |
||
399 | '503' => 'Service Unavailable', |
||
400 | '504' => 'Gateway Timeout', |
||
401 | '505' => 'HTTP Version Not Supported', |
||
402 | '506' => 'Variant Also Negotiates', |
||
403 | '507' => 'Insufficient Storage', |
||
404 | '508' => 'Loop Detected', |
||
405 | '510' => 'Not Extended', |
||
406 | ]; |
||
407 | |||
408 | /** |
||
409 | * 301 alias |
||
410 | * @param String redirect url |
||
411 | */ |
||
412 | public function movePermanently($url) |
||
418 | |||
419 | /** |
||
420 | * 400 alias |
||
421 | */ |
||
422 | public function badRequest() |
||
426 | |||
427 | /** |
||
428 | * 401 alias |
||
429 | */ |
||
430 | public function unauthorized() |
||
434 | |||
435 | /** |
||
436 | * 403 alias |
||
437 | */ |
||
438 | public function forbidden() |
||
442 | |||
443 | /** |
||
444 | * 404 alias |
||
445 | */ |
||
446 | public function notFound() |
||
450 | |||
451 | /** |
||
452 | * 405 alias |
||
453 | */ |
||
454 | public function methodNotAllowed() |
||
458 | |||
459 | /** |
||
460 | * 422 alias |
||
461 | */ |
||
462 | public function unprocessableEntity() |
||
466 | |||
467 | /** |
||
468 | * 500 alias |
||
469 | */ |
||
470 | public function internalServerError() |
||
474 | |||
475 | /** |
||
476 | * 静的ファイルを表示 |
||
477 | * @param String ファイル名 |
||
478 | */ |
||
479 | public function displayFile($filename) |
||
486 | |||
487 | /** |
||
488 | * ファイルをダウンロード |
||
489 | * @param String ファイル名 |
||
490 | * @param String ユーザエージェント |
||
491 | */ |
||
492 | public function downloadFile($filename, $ua) |
||
506 | |||
507 | /** |
||
508 | * 指定したステータスコードのページに遷移 |
||
509 | * @param Integer ステータスコード |
||
510 | */ |
||
511 | public function move($statusCode) |
||
523 | |||
524 | /** |
||
525 | * レスポンス送出を開始する |
||
526 | */ |
||
527 | public function start() |
||
532 | |||
533 | /** |
||
534 | * レスポンスを送出して終了する |
||
535 | */ |
||
536 | public function end() |
||
568 | |||
569 | /** |
||
570 | * レスポンス送出せず終了する |
||
571 | */ |
||
572 | public function clean() |
||
576 | |||
577 | /** |
||
578 | * HTMLテンプレート |
||
579 | * @param String 表示内容 |
||
580 | * @return String HTML |
||
581 | */ |
||
582 | private function bodyTemplate($content) |
||
598 | } |
||
599 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.