| Conditions | 19 |
| Paths | 24 |
| Total Lines | 99 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public function manipulate( Parameter &$parameter, array &$parameters ) { |
||
| 45 | global $egMapsOLLayerGroups, $egMapsOLAvailableLayers; |
||
| 46 | |||
| 47 | $layerDefs = []; |
||
| 48 | $usedLayers = []; |
||
| 49 | |||
| 50 | foreach ( $parameter->getValue() as $layerOrGroup ) { |
||
| 51 | $lcLayerOrGroup = strtolower( $layerOrGroup ); |
||
| 52 | |||
| 53 | // Layer groups. Loop over all items and add them if not present yet. |
||
| 54 | if ( array_key_exists( $lcLayerOrGroup, $egMapsOLLayerGroups ) ) { |
||
| 55 | foreach ( $egMapsOLLayerGroups[$lcLayerOrGroup] as $layerName ) { |
||
| 56 | if ( !in_array( $layerName, $usedLayers ) ) { |
||
| 57 | if ( is_array( $egMapsOLAvailableLayers[$layerName] ) ) { |
||
| 58 | $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$layerName][0]; |
||
| 59 | } |
||
| 60 | else { |
||
| 61 | $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$layerName]; |
||
| 62 | } |
||
| 63 | $usedLayers[] = $layerName; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | // Single layers. Add them if not present yet. |
||
| 68 | elseif ( array_key_exists( $lcLayerOrGroup, $egMapsOLAvailableLayers ) ) { |
||
| 69 | if ( !in_array( $lcLayerOrGroup, $usedLayers ) ) { |
||
| 70 | if ( is_array( $egMapsOLAvailableLayers[$lcLayerOrGroup] ) ) { |
||
| 71 | $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$lcLayerOrGroup][0]; |
||
| 72 | } |
||
| 73 | else { |
||
| 74 | $layerDefs[] = 'new ' . $egMapsOLAvailableLayers[$lcLayerOrGroup]; |
||
| 75 | } |
||
| 76 | |||
| 77 | $usedLayers[] = $lcLayerOrGroup; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | // Image layers. Check validity and add if not present yet. |
||
| 81 | else { |
||
| 82 | $layerParts = explode( $this->groupNameSep, $layerOrGroup, 2 ); |
||
| 83 | $layerGroup = $layerParts[0]; |
||
| 84 | $layerName = count( $layerParts ) > 1 ? $layerParts[1] : null; |
||
| 85 | |||
| 86 | $title = Title::newFromText( $layerGroup, Maps_NS_LAYER ); |
||
| 87 | |||
| 88 | if ( $title !== null && $title->getNamespace() == Maps_NS_LAYER ) { |
||
| 89 | /** |
||
| 90 | * TODO/FIXME: This shouldn't be here and using $wgParser, instead it should |
||
| 91 | * be somewhere around MapsBaseMap::renderMap. But since we do a lot more than |
||
| 92 | * 'parameter manipulation' in here, we already diminish the information needed |
||
| 93 | * for this which will never arrive there. Perhaps the whole |
||
| 94 | * MapsLayer::getJavaScriptDefinition() shouldn't be done here. |
||
| 95 | */ |
||
| 96 | global $wgParser; |
||
| 97 | // add dependency to the layer page so if the layer definition gets updated, |
||
| 98 | // the page where it is used will be updated as well: |
||
| 99 | $rev = Revision::newFromTitle( $title ); |
||
| 100 | $wgParser->getOutput()->addTemplate( $title, $title->getArticleID(), $rev->getId() ); |
||
| 101 | |||
| 102 | // if the whole layer group is not yet loaded into the map and the group exists: |
||
| 103 | if( ! in_array( $layerGroup, $usedLayers ) |
||
| 104 | && $title->exists() |
||
| 105 | ) { |
||
| 106 | $layerPage = new MapsLayerPage( $title ); |
||
|
|
|||
| 107 | |||
| 108 | if( $layerName !== null ) { |
||
| 109 | // load specific layer with name: |
||
| 110 | $layer = MapsLayers::loadLayer( $title, $layerName ); |
||
| 111 | $layers = new MapsLayerGroup( $layer ); |
||
| 112 | $usedLayer = $layerOrGroup; |
||
| 113 | } |
||
| 114 | else { |
||
| 115 | // load all layers from group: |
||
| 116 | $layers = MapsLayers::loadLayerGroup( $title ); |
||
| 117 | $usedLayer = $layerGroup; |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach( $layers->getLayers() as $layer ) { |
||
| 121 | if( ( // make sure named layer is only taken once (in case it was requested on its own before) |
||
| 122 | $layer->getName() === null |
||
| 123 | || ! in_array( $layerGroup . $this->groupNameSep . $layer->getName(), $usedLayers ) |
||
| 124 | ) |
||
| 125 | && $layer->isOk() |
||
| 126 | ) { |
||
| 127 | $layerDefs[] = $layer->getJavaScriptDefinition(); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | $usedLayers[] = $usedLayer; // have to add this after loop of course! |
||
| 131 | } |
||
| 132 | } |
||
| 133 | else { |
||
| 134 | wfWarn( "Invalid layer ($layerOrGroup) encountered after validation." ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $parameter->setValue( $layerDefs ); |
||
| 140 | |||
| 141 | MapsMappingServices::getServiceInstance( 'openlayers' )->addLayerDependencies( $this->getDependencies( $usedLayers ) ); |
||
| 142 | } |
||
| 143 | |||
| 171 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.