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 | |||
| 36 | /** |
||
| 37 | * @var Packages |
||
| 38 | */ |
||
| 39 | private $packages; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EngineInterface |
||
| 43 | */ |
||
| 44 | private $templating; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|null |
||
| 48 | */ |
||
| 49 | private $locale; |
||
| 50 | |||
| 51 | 770 | public function __construct( |
|
| 52 | JsonBuilder $jsonBuilder, |
||
| 53 | RouterInterface $router, |
||
| 54 | RequestStack $requestStack, |
||
| 55 | Packages $packages = null, |
||
| 56 | EngineInterface $templating = null, |
||
| 57 | $locale = null |
||
| 58 | ) { |
||
| 59 | 770 | $this->jsonBuilder = $jsonBuilder; |
|
| 60 | 770 | $this->router = $router; |
|
| 61 | 770 | $this->packages = $packages; |
|
| 62 | 770 | $this->templating = $templating; |
|
| 63 | 770 | $requestLocale = $requestStack->getMasterRequest()->getLocale(); |
|
| 64 | 770 | $this->locale = $requestLocale ? $requestLocale : $locale; |
|
| 65 | 770 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 100 | public function renderBasePath($basePath) |
|
| 71 | { |
||
| 72 | 100 | return $this->fixPath($basePath); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 90 | public function renderJsPath($jsPath) |
|
| 79 | { |
||
| 80 | 90 | return $this->fixPath($jsPath); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 610 | public function renderWidget($id, array $config, array $options = []) |
|
| 87 | { |
||
| 88 | 610 | $config = $this->fixConfigLanguage($config); |
|
| 89 | 610 | $config = $this->fixConfigContentsCss($config); |
|
| 90 | 610 | $config = $this->fixConfigFilebrowsers( |
|
| 91 | 610 | $config, |
|
| 92 | 610 | isset($options['filebrowsers']) ? $options['filebrowsers'] : [] |
|
| 93 | 488 | ); |
|
| 94 | |||
| 95 | 610 | $autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
|
| 96 | 494 | ? 'CKEDITOR.disableAutoInline = true;'."\n" |
|
| 97 | 610 | : null; |
|
| 98 | |||
| 99 | 610 | $builder = $this->jsonBuilder->reset()->setValues($config); |
|
| 100 | 610 | $this->fixConfigEscapedValues($builder, $config); |
|
| 101 | |||
| 102 | 610 | $widget = sprintf( |
|
| 103 | 610 | 'CKEDITOR.%s("%s", %s);', |
|
| 104 | 610 | isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
|
| 105 | 610 | $id, |
|
| 106 | 610 | $this->fixConfigConstants($builder->build()) |
|
| 107 | 488 | ); |
|
| 108 | |||
| 109 | 610 | if (isset($options['input_sync']) && $options['input_sync']) { |
|
| 110 | 30 | $variable = 'ivory_ckeditor_'.$id; |
|
| 111 | 30 | $widget = 'var '.$variable.' = '.$widget."\n"; |
|
| 112 | |||
| 113 | 30 | return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
|
| 114 | } |
||
| 115 | |||
| 116 | 580 | return $autoInline.$widget; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 110 | public function renderDestroy($id) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 40 | public function renderPlugin($name, array $plugin) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 30 | public function renderStylesSet($name, array $stylesSet) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | 60 | public function renderTemplate($name, array $template) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $config |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 610 | private function fixConfigLanguage(array $config) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param array $config |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | 610 | private function fixConfigContentsCss(array $config) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param array $config |
||
| 229 | * @param array $filebrowsers |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | 610 | private function fixConfigFilebrowsers(array $config, array $filebrowsers) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param JsonBuilder $builder |
||
| 274 | * @param array $config |
||
| 275 | */ |
||
| 276 | 610 | private function fixConfigEscapedValues(JsonBuilder $builder, array $config) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $json |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | 610 | private function fixConfigConstants($json) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $path |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 250 | private function fixPath($path) |
|
| 325 | } |
||
| 326 |