Complex classes like MapsDisplayMapRenderer 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 MapsDisplayMapRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MapsDisplayMapRenderer { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @since 2.0 |
||
| 17 | * |
||
| 18 | * @var iMappingService |
||
| 19 | */ |
||
| 20 | protected $service; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor. |
||
| 24 | * |
||
| 25 | * @param iMappingService $service |
||
| 26 | */ |
||
| 27 | public function __construct( iMappingService $service ) { |
||
| 28 | $this->service = $service; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns the HTML to display the map. |
||
| 33 | * |
||
| 34 | * @since 2.0 |
||
| 35 | * |
||
| 36 | * @param array $params |
||
| 37 | * @param Parser $parser |
||
| 38 | * @param string $mapName |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | protected function getMapHTML( array $params, Parser $parser, $mapName ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns a PHP object to encode to JSON with the map data. |
||
| 61 | * |
||
| 62 | * @since 2.0 |
||
| 63 | * |
||
| 64 | * @param array $params |
||
| 65 | * @param Parser $parser |
||
| 66 | * |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | protected function getJSONObject( array $params, Parser $parser ) { |
||
|
|
|||
| 70 | return $params; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Handles the request from the parser hook by doing the work that's common for all |
||
| 75 | * mapping services, calling the specific methods and finally returning the resulting output. |
||
| 76 | * |
||
| 77 | * @param array $params |
||
| 78 | * @param Parser $parser |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public final function renderMap( array $params, Parser $parser ) { |
||
| 83 | $this->handleMarkerData( $params, $parser ); |
||
| 84 | |||
| 85 | $mapName = $this->service->getMapId(); |
||
| 86 | |||
| 87 | $output = $this->getMapHTML( $params, $parser, $mapName ); |
||
| 88 | |||
| 89 | $configVars = Skin::makeVariablesScript( $this->service->getConfigVariables() ); |
||
| 90 | |||
| 91 | $this->service->addDependencies( $parser ); |
||
| 92 | $parser->getOutput()->addHeadItem( $configVars ); |
||
| 93 | |||
| 94 | return $output; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Converts the data in the coordinates parameter to JSON-ready objects. |
||
| 99 | * These get stored in the locations parameter, and the coordinates on gets deleted. |
||
| 100 | * |
||
| 101 | * FIXME: complexity |
||
| 102 | * |
||
| 103 | * @since 1.0 |
||
| 104 | * |
||
| 105 | * @param array &$params |
||
| 106 | * @param Parser $parser |
||
| 107 | */ |
||
| 108 | protected function handleMarkerData( array &$params, Parser $parser ) { |
||
| 109 | if ( is_object( $params['centre'] ) ) { |
||
| 110 | $params['centre'] = $params['centre']->getJSONObject(); |
||
| 111 | } |
||
| 112 | |||
| 113 | $parserClone = clone $parser; |
||
| 114 | |||
| 115 | if ( is_object( $params['wmsoverlay'] ) ) { |
||
| 116 | $params['wmsoverlay'] = $params['wmsoverlay']->getJSONObject(); |
||
| 117 | } |
||
| 118 | |||
| 119 | $iconUrl = MapsMapper::getFileUrl( $params['icon'] ); |
||
| 120 | $visitedIconUrl = MapsMapper::getFileUrl( $params['visitedicon'] ); |
||
| 121 | $params['locations'] = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var Location $location |
||
| 125 | */ |
||
| 126 | foreach ( $params['coordinates'] as $location ) { |
||
| 127 | $jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '',$visitedIconUrl); |
||
| 128 | |||
| 129 | $jsonObj['title'] = $parserClone->parse( $jsonObj['title'], $parserClone->getTitle(), new ParserOptions() )->getText(); |
||
| 130 | $jsonObj['text'] = $parserClone->parse( $jsonObj['text'], $parserClone->getTitle(), new ParserOptions() )->getText(); |
||
| 131 | $jsonObj['inlineLabel'] = strip_tags($parserClone->parse( $jsonObj['inlineLabel'], $parserClone->getTitle(), new ParserOptions() )->getText(),'<a><img>'); |
||
| 132 | |||
| 133 | $hasTitleAndtext = $jsonObj['title'] !== '' && $jsonObj['text'] !== ''; |
||
| 134 | $jsonObj['text'] = ( $hasTitleAndtext ? '<b>' . $jsonObj['title'] . '</b><hr />' : $jsonObj['title'] ) . $jsonObj['text']; |
||
| 135 | $jsonObj['title'] = strip_tags( $jsonObj['title'] ); |
||
| 136 | |||
| 137 | $params['locations'][] = $jsonObj; |
||
| 138 | } |
||
| 139 | |||
| 140 | unset( $params['coordinates'] ); |
||
| 141 | |||
| 142 | $this->handleShapeData( $params, $parserClone ); |
||
| 143 | |||
| 144 | if ( $params['mappingservice'] === 'openlayers' ) { |
||
| 145 | $params['layers'] = self::evilOpenLayersHack( $params['layers'] ); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | protected function handleShapeData( array &$params, Parser $parserClone ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * FIXME |
||
| 178 | * |
||
| 179 | * Temporary hack until the mapping service handling gets a proper refactor |
||
| 180 | * This kind of JS construction is also rather evil and should not be done at this point |
||
| 181 | * |
||
| 182 | * @since 3.0 |
||
| 183 | * @deprecated |
||
| 184 | * |
||
| 185 | * @param string[] $layers |
||
| 186 | * |
||
| 187 | * @return string[] |
||
| 188 | */ |
||
| 189 | public static function evilOpenLayersHack( $layers ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * FIXME |
||
| 293 | * @see evilOpenLayersHack |
||
| 294 | */ |
||
| 295 | private static function getLayerDependencies( array $layerNames ) { |
||
| 311 | |||
| 312 | } |
||
| 313 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.