Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

MapsImageLayer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 184
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameterDefinitions() 0 55 3
B getJavaScriptDefinition() 0 38 5
A register() 0 4 1
B getPropertyHtmlRepresentation() 0 28 5
A doPropertiesHtmlTransform() 0 18 1
1
<?php
2
3
/**
4
 * Class for describing image layers.
5
 *
6
 * @since 0.7.2
7
 * 
8
 * @file Maps_ImageLayer.php
9
 * @ingroup Maps
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 * @author Daniel Werner
14
 */
15
class MapsImageLayer extends MapsLayer {
16
17
	/**
18
	 * Registers the layer.
19
	 *
20
	 * @since 0.7.2
21
	 */
22
	public static function register() {
23
		MapsLayerTypes::registerLayerType( 'image', __CLASS__, 'openlayers' );
24
		return true;
25
	}
26
27
	/**
28
	 * @see MapsLayer::getParameterDefinitions
29
	 *
30
	 * @since 0.7.2
31
	 *
32
	 * @return array
33
	 */
34
	protected function getParameterDefinitions() {
35
		$params = parent::getParameterDefinitions();
36
37
		// map extent for extents bound object:
38
		$params['topextent'] = [
39
			'type' => 'float',
40
			'aliases' => [ 'upperbound', 'topbound' ],
41
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
42
		];
43
44
		$params['rightextent'] = [
45
			'type' => 'float',
46
			'aliases' => [ 'rightbound' ],
47
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
48
		];
49
50
		$params['bottomextent'] = [
51
			'type' => 'float',
52
			'aliases' => [ 'lowerbound', 'bottombound' ],
53
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
54
		];
55
56
		$params['leftextent'] = [
57
			'type' => 'float',
58
			'aliases' => [ 'leftbound' ],
59
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
60
		];
61
62
		// image-source information:
63
		$params['source'] = [
64
			// TODO-customMaps: addCriteria( new CriterionIsImage() )
65
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
66
			'post-format' => function( $source ) {
67
				$imageUrl = MapsMapper::getFileUrl( $source );
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
68
69
				global $egMapsAllowExternalImages;
70
				if( $imageUrl === '' && $egMapsAllowExternalImages ) {
71
					return $source;
72
				}
73
				return $imageUrl;
74
			}
75
		];
76
77
		$params['width'] = [
78
			'type' => 'float',
79
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
80
		];
81
82
		$params['height'] = [
83
			'type' => 'float',
84
			'message' => 'maps-displaymap-par-coordinates', // TODO-customMaps: create a message
85
		];
86
87
		return $params;
88
	}
89
90
	/**
91
	 * @see MapsLayer::getPropertyHtmlRepresentation
92
	 *
93
	 * @since 3.0
94
	 *
95
	 * @return array
96
	 */
97
	protected function getPropertyHtmlRepresentation( $name, &$parser ) {
98
		$value = $this->properties[ $name ];
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
100
		switch( $name ) {
101
			case 'source':
102
				$value = $this->originalPropertyValues['source']; // get original, non-modified value
103
104
				$title = Title::newFromText( $value, NS_FILE );
105
106
				// if title has invalid characters or doesn't exist and has url-style
107
				if( $title === null
108
					|| ( !$title->exists() && preg_match( '|^.+\://.+\..+$|', $value ) )
109
				) {
110
					// url link:
111
					$value = $parser->recursiveTagParse( "[$value $value]" );
112
				} else {
113
					// wikilink (can be red link to non-existant file):
114
					$imgName = $title->getPrefixedText();
115
					$value = $parser->recursiveTagParse( "[[$imgName|thumb|[[:$imgName]]|left]]" );
116
				}
117
				return $value; // html already
118
119
			default:
120
				// if we don't have any special handling here, leave it to base class:
121
				return parent::getPropertyHtmlRepresentation( $name, $parser );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::getProper...tation($name, $parser); (string) is incompatible with the return type documented by MapsImageLayer::getPropertyHtmlRepresentation of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
122
		}
123
		return htmlspecialchars( $value );;
0 ignored issues
show
Unused Code introduced by
return htmlspecialchars($value); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
124
	}
125
126
	/**
127
	 * @see MapsLayer::doPropertiesHtmlTransform
128
	 *
129
	 * @since 3.0
130
	 *
131
	 * @return array
132
	 */
133
	protected function doPropertiesHtmlTransform( &$properties ) {
134
		parent::doPropertiesHtmlTransform( $properties );
135
136
		$sp = '&#x202F;'; // non-breaking thin space
137
138
		// image-size:
139
		$properties['image-size'] = "<b>width:</b> {$properties['width']}{$sp}pixel, <b>height:</b> {$properties['height']}{$sp}pixel";
140
		unset( $properties['width'], $properties['height'] );
141
142
		// extent:
143
		$unit = $properties['units'];
144
		$properties['extent'] =
145
			"<b>left:</b> {$properties['leftextent']}{$sp}$unit, " .
146
			"<b>bottom:</b> {$properties['bottomextent']}{$sp}$unit, " .
147
			"<b>right:</b> {$properties['rightextent']}{$sp}$unit, " .
148
			"<b>top:</b> {$properties['topextent']}{$sp}$unit";
149
		unset( $properties['leftextent'], $properties['bottomextent'], $properties['rightextent'], $properties['topextent'] );
150
	}
151
152
	/**
153
	 * @see MapsLayer::getJavaScriptDefinition
154
	 * 
155
	 * @since 0.7.2
156
	 * 
157
	 * @return string
158
	 */
159
	public function getJavaScriptDefinition() {
160
		$this->validate();
161
162
		// do image layer options:
163
164
		$options = [
165
			'isImage' => true,
166
			'units' => $this->properties['units'],
167
		];
168
169
		if( $this->properties['zoomlevels'] !== false ) {
170
			$options['numZoomLevels'] = $this->properties['zoomlevels'];
171
		}
172
		if( $this->properties['minscale'] !== false ) {
173
			$options['minScale'] = $this->properties['minscale'];
174
		}
175
		if( $this->properties['maxscale'] !== false ) {
176
			$options['maxScale'] = $this->properties['maxscale'];
177
		}
178
		
179
		$options = Xml::encodeJsVar( (object)$options ); //js-encode all options );
180
181
		// for non-option params, get JavaScript-encoded config values:
182
		foreach( $this->properties as $name => $value ) {
183
			${ $name } = MapsMapper::encodeJsVar( $value );
184
		}
185
186
		return <<<JavaScript
187
new OpenLayers.Layer.Image(
188
	$label,
0 ignored issues
show
Bug introduced by
The variable $label does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
189
	$source,
0 ignored issues
show
Bug introduced by
The variable $source does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
190
	new OpenLayers.Bounds($leftextent, $bottomextent, $rightextent, $topextent),
0 ignored issues
show
Bug introduced by
The variable $leftextent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $bottomextent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $rightextent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $topextent does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
191
	new OpenLayers.Size($width, $height),
0 ignored issues
show
Bug introduced by
The variable $width does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $height does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
192
	$options
193
);
194
JavaScript;
195
		die();
0 ignored issues
show
Unused Code introduced by
die; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Coding Style Compatibility introduced by
The method getJavaScriptDefinition() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
196
	}
197
198
}
199