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 |
||
| 25 | class CKEditorRenderer implements CKEditorRendererInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var JsonBuilder |
||
| 29 | */ |
||
| 30 | private $jsonBuilder; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var RouterInterface |
||
| 34 | */ |
||
| 35 | private $router; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Packages |
||
| 39 | */ |
||
| 40 | private $assetsPackages; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var EngineInterface |
||
| 44 | */ |
||
| 45 | private $templating; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var RequestStack |
||
| 49 | */ |
||
| 50 | private $requestStack; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var null|string |
||
| 54 | */ |
||
| 55 | private $locale; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param JsonBuilder|ContainerInterface $containerOrJsonBuilder |
||
| 59 | * @param RouterInterface $router |
||
| 60 | * @param Packages $packages |
||
| 61 | * @param RequestStack $requestStack |
||
| 62 | * @param EngineInterface $templating |
||
| 63 | * @param null|string $locale |
||
| 64 | */ |
||
| 65 | 780 | public function __construct($containerOrJsonBuilder, RouterInterface $router = null, Packages $packages = null, RequestStack $requestStack = null, EngineInterface |
|
| 66 | $templating = null, $locale = null) |
||
| 67 | { |
||
| 68 | 780 | if ($containerOrJsonBuilder instanceof ContainerInterface) { |
|
| 69 | 10 | @trigger_error(sprintf('Passing a %s as %s first argument is deprecated since IvoryCKEditor 6.1, and will be removed in 7.0. Use %s instead.', ContainerInterface::class, __METHOD__, JsonBuilder::class), E_USER_DEPRECATED); |
|
|
|
|||
| 70 | 10 | $jsonBuilder = $containerOrJsonBuilder->get('ivory_ck_editor.renderer.json_builder'); |
|
| 71 | 10 | $router = $containerOrJsonBuilder->get('router'); |
|
| 72 | 10 | $packages = $containerOrJsonBuilder->get('assets.packages'); |
|
| 73 | 10 | $requestStack = $containerOrJsonBuilder->get('request_stack'); |
|
| 74 | 10 | $templating = $containerOrJsonBuilder->get('templating'); |
|
| 75 | 780 | } elseif ($containerOrJsonBuilder instanceof JsonBuilder) { |
|
| 76 | 780 | $jsonBuilder = $containerOrJsonBuilder; |
|
| 77 | 780 | if ($router === null) { |
|
| 78 | throw new \InvalidArgumentException(sprintf('%s 2nd argument must not be null when using %s as first argument', __METHOD__, JsonBuilder::class)); |
||
| 79 | 780 | } elseif ($packages === null) { |
|
| 80 | throw new \InvalidArgumentException(sprintf('%s 3rd argument must not be null when using %s as first argument', __METHOD__, JsonBuilder::class)); |
||
| 81 | 780 | } elseif ($requestStack === null) { |
|
| 82 | throw new \InvalidArgumentException(sprintf('%s 4th argument must not be null when using %s as first argument', __METHOD__, JsonBuilder::class)); |
||
| 83 | 780 | } elseif ($templating === null) { |
|
| 84 | 156 | throw new \InvalidArgumentException(sprintf('%s 5th argument must not be null when using %s as first argument', __METHOD__, JsonBuilder::class)); |
|
| 85 | } |
||
| 86 | 624 | } else { |
|
| 87 | throw new \InvalidArgumentException(sprintf('%s first argument must be an instance of %s or %s (%s given).', __METHOD__, ContainerInterface::class, JsonBuilder::class, is_object($containerOrJsonBuilder) ? get_class($containerOrJsonBuilder) : gettype($containerOrJsonBuilder))); |
||
| 88 | } |
||
| 89 | |||
| 90 | 780 | $this->jsonBuilder = $jsonBuilder; |
|
| 91 | 780 | $this->router = $router; |
|
| 92 | 780 | $this->assetsPackages = $packages; |
|
| 93 | 780 | $this->templating = $templating; |
|
| 94 | 780 | $this->requestStack = $requestStack; |
|
| 95 | 780 | $this->locale = $locale; |
|
| 96 | 780 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 100 | public function renderBasePath($basePath) |
|
| 102 | { |
||
| 103 | 100 | return $this->fixPath($basePath); |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | 90 | public function renderJsPath($jsPath) |
|
| 110 | { |
||
| 111 | 90 | return $this->fixPath($jsPath); |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | 610 | public function renderWidget($id, array $config, array $options = []) |
|
| 118 | { |
||
| 119 | 610 | $config = $this->fixConfigLanguage($config); |
|
| 120 | 610 | $config = $this->fixConfigContentsCss($config); |
|
| 121 | 610 | $config = $this->fixConfigFilebrowsers( |
|
| 122 | 610 | $config, |
|
| 123 | 610 | isset($options['filebrowsers']) ? $options['filebrowsers'] : [] |
|
| 124 | 488 | ); |
|
| 125 | |||
| 126 | 610 | $autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
|
| 127 | 494 | ? 'CKEDITOR.disableAutoInline = true;'."\n" |
|
| 128 | 610 | : null; |
|
| 129 | |||
| 130 | 610 | $builder = $this->jsonBuilder->reset()->setValues($config); |
|
| 131 | 610 | $this->fixConfigEscapedValues($builder, $config); |
|
| 132 | |||
| 133 | 610 | $widget = sprintf( |
|
| 134 | 610 | 'CKEDITOR.%s("%s", %s);', |
|
| 135 | 610 | isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
|
| 136 | 610 | $id, |
|
| 137 | 610 | $this->fixConfigConstants($builder->build()) |
|
| 138 | 488 | ); |
|
| 139 | |||
| 140 | 610 | if (isset($options['input_sync']) && $options['input_sync']) { |
|
| 141 | 30 | $variable = 'ivory_ckeditor_'.$id; |
|
| 142 | 30 | $widget = 'var '.$variable.' = '.$widget."\n"; |
|
| 143 | |||
| 144 | 30 | return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
|
| 145 | } |
||
| 146 | |||
| 147 | 580 | return $autoInline.$widget; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 110 | public function renderDestroy($id) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 40 | public function renderPlugin($name, array $plugin) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 30 | public function renderStylesSet($name, array $stylesSet) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | 60 | public function renderTemplate($name, array $template) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param array $config |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 610 | private function fixConfigLanguage(array $config) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $config |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | 610 | private function fixConfigContentsCss(array $config) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param array $config |
||
| 260 | * @param array $filebrowsers |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | 610 | private function fixConfigFilebrowsers(array $config, array $filebrowsers) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param JsonBuilder $builder |
||
| 305 | * @param array $config |
||
| 306 | */ |
||
| 307 | 610 | private function fixConfigEscapedValues(JsonBuilder $builder, array $config) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $json |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 610 | private function fixConfigConstants($json) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $path |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | 250 | private function fixPath($path) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return null|string |
||
| 359 | */ |
||
| 360 | 590 | private function getLanguage() |
|
| 369 | } |
||
| 370 |
If you suppress an error, we recommend checking for the error condition explicitly: