Complex classes like CKEditorRenderer 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 CKEditorRenderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CKEditorRenderer implements CKEditorRendererInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var JsonBuilder |
||
28 | */ |
||
29 | private $jsonBuilder; |
||
30 | |||
31 | /** |
||
32 | * @var RouterInterface |
||
33 | */ |
||
34 | private $router; |
||
35 | 790 | ||
36 | /** |
||
37 | 790 | * @var Packages |
|
38 | 790 | */ |
|
39 | private $packages; |
||
40 | |||
41 | /** |
||
42 | * @var EngineInterface |
||
43 | 100 | */ |
|
44 | private $templating; |
||
45 | 100 | ||
46 | /** |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $locale; |
||
50 | |||
51 | 90 | public function __construct( |
|
70 | 610 | ||
71 | /** |
||
72 | 610 | * {@inheritdoc} |
|
73 | 610 | */ |
|
74 | public function renderBasePath($basePath) |
||
75 | 610 | { |
|
76 | 610 | return $this->fixPath($basePath); |
|
77 | 610 | } |
|
78 | 610 | ||
79 | 610 | /** |
|
80 | 488 | * {@inheritdoc} |
|
81 | */ |
||
82 | 610 | public function renderJsPath($jsPath) |
|
83 | 30 | { |
|
84 | 30 | return $this->fixPath($jsPath); |
|
85 | } |
||
86 | 30 | ||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | 580 | */ |
|
90 | public function renderWidget($id, array $config, array $options = []) |
||
91 | { |
||
92 | $config = $this->fixConfigLanguage($config); |
||
93 | $config = $this->fixConfigContentsCss($config); |
||
94 | $config = $this->fixConfigFilebrowsers( |
||
95 | 110 | $config, |
|
96 | isset($options['filebrowsers']) ? $options['filebrowsers'] : [] |
||
97 | 110 | ); |
|
98 | |||
99 | 88 | $autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
|
100 | 88 | ? 'CKEDITOR.disableAutoInline = true;'."\n" |
|
101 | 110 | : null; |
|
102 | 22 | ||
103 | 88 | $builder = $this->jsonBuilder->reset()->setValues($config); |
|
104 | $this->fixConfigEscapedValues($builder, $config); |
||
105 | |||
106 | $widget = sprintf( |
||
107 | 'CKEDITOR.%s("%s", %s);', |
||
108 | isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
||
109 | 40 | $id, |
|
110 | $this->fixConfigConstants($builder->build()) |
||
111 | 40 | ); |
|
112 | 40 | ||
113 | 40 | if (isset($options['input_sync']) && $options['input_sync']) { |
|
114 | 40 | $variable = 'ivory_ckeditor_'.$id; |
|
115 | 40 | $widget = 'var '.$variable.' = '.$widget."\n"; |
|
116 | 32 | ||
117 | return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
||
118 | } |
||
119 | |||
120 | return $autoInline.$widget; |
||
121 | } |
||
122 | 30 | ||
123 | /** |
||
124 | 30 | * {@inheritdoc} |
|
125 | */ |
||
126 | 24 | public function renderDestroy($id) |
|
127 | 30 | { |
|
128 | 30 | return sprintf( |
|
129 | 30 | 'if (CKEDITOR.instances["%1$s"]) { '. |
|
130 | 24 | 'CKEDITOR.instances["%1$s"].destroy(true); '. |
|
131 | 'delete CKEDITOR.instances["%1$s"]; '. |
||
132 | '}', |
||
133 | $id |
||
134 | ); |
||
135 | } |
||
136 | 80 | ||
137 | /** |
||
138 | 80 | * {@inheritdoc} |
|
139 | 80 | */ |
|
140 | 64 | public function renderPlugin($name, array $plugin) |
|
141 | { |
||
142 | 80 | return sprintf( |
|
143 | 80 | 'CKEDITOR.plugins.addExternal("%s", "%s", "%s");', |
|
144 | 80 | $name, |
|
145 | 40 | $this->fixPath($plugin['path']), |
|
146 | 40 | $plugin['filename'] |
|
147 | 40 | ); |
|
148 | 32 | } |
|
149 | 32 | ||
150 | /** |
||
151 | 80 | * {@inheritdoc} |
|
152 | 80 | */ |
|
153 | 64 | public function renderStylesSet($name, array $stylesSet) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function renderTemplate($name, array $template) |
||
168 | 610 | { |
|
193 | 80 | ||
194 | 64 | /** |
|
195 | 64 | * @param array $config |
|
196 | * |
||
197 | 610 | * @return array |
|
198 | */ |
||
199 | private function fixConfigLanguage(array $config) |
||
211 | 488 | ||
212 | 488 | /** |
|
213 | 488 | * @param array $config |
|
214 | 488 | * |
|
215 | 488 | * @return array |
|
216 | 610 | */ |
|
217 | private function fixConfigContentsCss(array $config) |
||
230 | 140 | ||
231 | 140 | /** |
|
232 | 140 | * @param array $config |
|
233 | 112 | * @param array $filebrowsers |
|
234 | 112 | * |
|
235 | * @return array |
||
236 | 610 | */ |
|
237 | 610 | private function fixConfigFilebrowsers(array $config, array $filebrowsers) |
|
275 | |||
276 | 610 | /** |
|
277 | * @param JsonBuilder $builder |
||
278 | * @param array $config |
||
279 | */ |
||
280 | private function fixConfigEscapedValues(JsonBuilder $builder, array $config) |
||
299 | |||
300 | /** |
||
301 | * @param string $json |
||
302 | * |
||
303 | * @return string |
||
304 | 680 | */ |
|
305 | private function fixConfigConstants($json) |
||
309 | |||
310 | /** |
||
311 | * @param string $path |
||
312 | 590 | * |
|
313 | * @return string |
||
314 | 590 | */ |
|
315 | 20 | private function fixPath($path) |
|
329 | } |
||
330 |