These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | /** |
||
4 | * Initialization file for the Semantic Maps extension. |
||
5 | * |
||
6 | * @licence GNU GPL v2+ |
||
7 | * @author Jeroen De Dauw < [email protected] > |
||
8 | */ |
||
9 | |||
10 | if ( !defined( 'MEDIAWIKI' ) ) { |
||
11 | die( 'Not an entry point.' ); |
||
12 | } |
||
13 | |||
14 | if ( defined( 'SM_VERSION' ) ) { |
||
15 | // Do not initialize more than once. |
||
16 | return 1; |
||
17 | } |
||
18 | |||
19 | if ( version_compare( $GLOBALS['wgVersion'], '1.23c', '<' ) ) { |
||
20 | throw new Exception( |
||
21 | 'This version of Semantic Maps requires MediaWiki 1.23 or above; use Semantic Maps 3.3.x for older versions.' |
||
22 | . ' See https://github.com/SemanticMediaWiki/SemanticMaps/blob/master/INSTALL.md for more info.' |
||
23 | ); |
||
24 | } |
||
25 | |||
26 | if ( !defined( 'Maps_VERSION' ) && is_readable( __DIR__ . '/vendor/autoload.php' ) ) { |
||
27 | include_once( __DIR__ . '/vendor/autoload.php' ); |
||
28 | } |
||
29 | |||
30 | if ( !defined( 'Maps_VERSION' ) ) { |
||
31 | throw new Exception( 'You need to have Maps installed in order to use Semantic Maps' ); |
||
32 | } |
||
33 | |||
34 | SemanticMaps::initExtension(); |
||
35 | |||
36 | $GLOBALS['wgExtensionFunctions'][] = function() { |
||
37 | SemanticMaps::onExtensionFunction(); |
||
38 | }; |
||
39 | |||
40 | /** |
||
41 | * @codeCoverageIgnore |
||
42 | */ |
||
43 | class SemanticMaps { |
||
44 | |||
45 | /** |
||
46 | * @since 3.4 |
||
47 | */ |
||
48 | public static function initExtension() { |
||
0 ignored issues
–
show
initExtension uses the super-global variable $GLOBALS which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
49 | |||
50 | // Load DefaultSettings |
||
51 | require_once __DIR__ . '/DefaultSettings.php'; |
||
52 | |||
53 | define( 'SM_VERSION', '3.4.0-alpha' ); |
||
54 | |||
55 | $GLOBALS['wgExtensionCredits']['semantic'][] = [ |
||
56 | 'path' => __FILE__, |
||
57 | 'name' => 'Semantic Maps', |
||
58 | 'version' => SM_VERSION, |
||
59 | 'author' => [ |
||
60 | '[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]' |
||
61 | ], |
||
62 | 'url' => 'https://github.com/SemanticMediaWiki/SemanticMaps/blob/master/README.md#semantic-maps', |
||
63 | 'descriptionmsg' => 'semanticmaps-desc', |
||
64 | 'license-name' => 'GPL-2.0+' |
||
65 | ]; |
||
66 | |||
67 | include_once __DIR__ . '/src/queryprinters/SM_QueryPrinters.php'; |
||
68 | |||
69 | $moduleTemplate = [ |
||
70 | 'position' => 'bottom', |
||
71 | 'group' => 'ext.semanticmaps', |
||
72 | ]; |
||
73 | |||
74 | $GLOBALS['wgResourceModules']['ext.sm.forminputs'] = $moduleTemplate + [ |
||
75 | 'dependencies' => [ 'ext.maps.coord' ], |
||
76 | 'localBasePath' => __DIR__ . '/src/forminputs', |
||
77 | 'remoteExtPath' => 'SemanticMaps/src/forminputs', |
||
78 | 'scripts' => [ |
||
79 | 'jquery.mapforminput.js' |
||
80 | ], |
||
81 | 'messages' => [ |
||
82 | 'semanticmaps_enteraddresshere', |
||
83 | 'semanticmaps-updatemap', |
||
84 | 'semanticmaps_lookupcoordinates', |
||
85 | 'semanticmaps-forminput-remove', |
||
86 | 'semanticmaps-forminput-add', |
||
87 | 'semanticmaps-forminput-locations' |
||
88 | ] |
||
89 | ]; |
||
90 | |||
91 | $GLOBALS['wgResourceModules']['ext.sm.common'] = $moduleTemplate + [ |
||
92 | 'localBasePath' => __DIR__ . '/src', |
||
93 | 'remoteExtPath' => 'SemanticMaps/src', |
||
94 | 'scripts' => [ |
||
95 | 'ext.sm.common.js' |
||
96 | ] |
||
97 | ]; |
||
98 | |||
99 | include_once __DIR__ . '/src/services/GoogleMaps3/SM_GoogleMaps3.php'; |
||
100 | include_once __DIR__ . '/src/services/Leaflet/SM_Leaflet.php'; |
||
101 | include_once __DIR__ . '/src/services/OpenLayers/SM_OpenLaysers.php'; |
||
102 | |||
103 | // Internationalization |
||
104 | $GLOBALS['wgMessagesDirs']['SemanticMaps'] = __DIR__ . '/i18n'; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @since 3.4 |
||
109 | */ |
||
110 | public static function onExtensionFunction() { |
||
0 ignored issues
–
show
onExtensionFunction uses the super-global variable $GLOBALS which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
111 | |||
112 | // Hook for initializing the Geographical Data types. |
||
113 | $GLOBALS['wgHooks']['SMW::DataType::initTypes'][] = 'SemanticMapsHooks::initGeoDataTypes'; |
||
114 | |||
115 | // Hook for defining the default query printer for queries that ask for geographical coordinates. |
||
116 | $GLOBALS['wgHooks']['SMWResultFormat'][] = 'SemanticMapsHooks::addGeoCoordsDefaultFormat'; |
||
117 | |||
118 | // Hook for adding a Semantic Maps links to the Admin Links extension. |
||
119 | $GLOBALS['wgHooks']['AdminLinks'][] = 'SemanticMapsHooks::addToAdminLinks'; |
||
120 | |||
121 | $GLOBALS['wgHooks']['sfFormPrinterSetup'][] = 'SemanticMaps\FormInputsSetup::run'; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @since 3.4 |
||
126 | * |
||
127 | * @return string|null |
||
128 | */ |
||
129 | public static function getVersion() { |
||
130 | return SM_VERSION; |
||
131 | } |
||
132 | |||
133 | } |
||
134 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.