Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class View implements \IteratorAggregate |
||
14 | { |
||
15 | protected |
||
16 | /** @var ArrayObject */ $_storage |
||
17 | , /** @var string */ $_basePath |
||
18 | , /** @var string */ $_fileName |
||
19 | , /** @var string */ $_layoutFileName = '' |
||
20 | , /** @var string */ $_content = '' |
||
21 | ; |
||
22 | |||
23 | /** |
||
24 | * @param string $fileName 描画したいテンプレートのファイル名を指定します |
||
25 | * @param string $basePath テンプレートの探索基準パスです。相対パスも指定できます。指定しなければinclude_pathから探索します。 |
||
26 | * @param ArrayObject $arr view変数の引き継ぎ元です。内部用なので通常は使う必要はありません。 |
||
27 | */ |
||
28 | 12 | public function __construct($fileName, $basePath='', ArrayObject $arr=null) |
|
34 | |||
35 | /** |
||
36 | * このクラスはforeach可能です |
||
37 | * @return \ArrayIterator |
||
38 | */ |
||
39 | 1 | public function getIterator() |
|
43 | |||
44 | /** |
||
45 | * @param string|int $name |
||
46 | * @return mixed |
||
47 | */ |
||
48 | 4 | public function __get($name) |
|
52 | |||
53 | /** |
||
54 | * @param string|int $name |
||
55 | * @param mixed $value |
||
56 | */ |
||
57 | 4 | public function __set($name, $value) |
|
61 | |||
62 | /** |
||
63 | * @param string|int $name |
||
64 | * @return bool |
||
65 | */ |
||
66 | 1 | public function __isset($name) |
|
70 | |||
71 | /** |
||
72 | * 描画するスクリプトファイルのパスを返します。 |
||
73 | * @return string |
||
74 | */ |
||
75 | 4 | public function __toString() |
|
83 | |||
84 | /** |
||
85 | * セットされたview 変数を配列化して返します |
||
86 | * @return array |
||
87 | */ |
||
88 | 2 | public function toArray() |
|
92 | |||
93 | /** |
||
94 | * 配列で一気にview変数をセットします |
||
95 | * @param array|\Traversable $array |
||
96 | */ |
||
97 | 3 | public function assign($array) |
|
107 | |||
108 | /** |
||
109 | * @param string $name |
||
110 | * @param array $array |
||
111 | */ |
||
112 | 1 | public function append($name, $array) |
|
116 | |||
117 | /** |
||
118 | * @param string $name |
||
119 | * @param array $array |
||
120 | */ |
||
121 | 2 | public function prepend($name, $array) |
|
125 | |||
126 | /** |
||
127 | * @param string $name |
||
128 | * @param array $array |
||
129 | * @param bool $append |
||
130 | */ |
||
131 | 3 | private function _merge($name, array $array, $append=true) |
|
144 | |||
145 | /** |
||
146 | * テンプレートファイルを描画して文字列にして返します |
||
147 | * テンプレート内では$EでHTMLエスケープでき、 |
||
148 | * <?= $E('<script>') ?> と <?= htmlspecialchars('<script>') ?> が等価です。 |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 3 | public function render() |
|
172 | |||
173 | /** |
||
174 | * @internal |
||
175 | * @param string $content |
||
176 | */ |
||
177 | 2 | protected function setContent($content) |
|
181 | |||
182 | /** |
||
183 | * 子テンプレートの描画結果を文字列として返します |
||
184 | * @return string |
||
185 | */ |
||
186 | 2 | public function content() |
|
190 | |||
191 | /** |
||
192 | * 親となるレイアウトテンプレートのファイル名を指定します。 |
||
193 | * レイアウトは__construct()で指定した基準パスと同じパスから探索します。 |
||
194 | * @param string $layoutFileName |
||
195 | */ |
||
196 | 2 | public function setLayout($layoutFileName) |
|
200 | |||
201 | /** |
||
202 | * 現在セットされているレイアウトファイル名を返します |
||
203 | * @return string |
||
204 | */ |
||
205 | 1 | public function getLayout() |
|
209 | |||
210 | /** |
||
211 | * 指定したテンプレートファイルを描画します。 |
||
212 | * 変数は引き継がれます。 |
||
213 | * @param string $partialFileName |
||
214 | * @return string |
||
215 | */ |
||
216 | 1 | public function partial($partialFileName) |
|
225 | |||
226 | /** |
||
227 | * htmlspecialchars()のエイリアスです。 |
||
228 | * |
||
229 | * @param string $rawStr |
||
230 | * @param int $mode |
||
231 | * @param string $charset 文字コードです。指定しなければdefault_charsetの値を使用します。 |
||
232 | * @return string |
||
233 | */ |
||
234 | 2 | public static function h($rawStr, $mode=\ENT_QUOTES, $charset=null) |
|
242 | |||
243 | /** |
||
244 | * metaタグ群を出力するためのヘルパー関数です。 |
||
245 | * <?= self::meta($meta) ?> |
||
246 | * |
||
247 | * @example |
||
248 | * <pre> |
||
249 | * $meta = [ |
||
250 | * 'charset' => 'utf8', |
||
251 | * 'http-equiv' => [ |
||
252 | * 'X-UA-Compatible' => 'IE=edge', |
||
253 | * 'Content-Type' => 'text/html; charset=UTF-8', |
||
254 | * ], |
||
255 | * 'name' => [ |
||
256 | * 'keyword' => 'hoge,fuga,muga', |
||
257 | * 'description' => 'hogehoge', |
||
258 | * 'twitter:title' => 'Mountain sunset', |
||
259 | * ], |
||
260 | * 'property' => [ |
||
261 | * 'og:title' => 'foooooo', |
||
262 | * 'og:description' => 'hogehoge', |
||
263 | * 'og:image' => [ |
||
264 | * 'http://hoge.com/hoge1.jpg', |
||
265 | * 'http://hoge.com/hoge2.jpg', |
||
266 | * ], |
||
267 | * ], |
||
268 | * ]; |
||
269 | * </pre> |
||
270 | * |
||
271 | * @param array|Traversable $meta metaタグの定義。 |
||
272 | * @param boolean $isXhtml trueを指定すると、タグがXHTML形式になります。 |
||
273 | * @param boolean $escape falseを指定すると、HTMLエスケープを行わなくなります。 |
||
274 | * @return string |
||
275 | */ |
||
276 | 2 | public static function meta($meta, $isXhtml=false, $escape=true) |
|
309 | |||
310 | 1 | private static function _generateTag(array $def, $isXhtml=false, $escape=true) |
|
328 | } |
||
329 |