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 | use WNBY\HookRegistry; |
||
4 | |||
5 | /** |
||
6 | * @see https://github.com/SemanticMediaWiki/WhatsNearby/ |
||
7 | * @link https://www.semantic-mediawiki.org/wiki/Extension:WhatsNearby |
||
8 | * |
||
9 | * @defgroup wnby WhatsNearby |
||
10 | */ |
||
11 | if ( !defined( 'MEDIAWIKI' ) ) { |
||
12 | die( 'This file is part of the WhatsNearby extension, it is not a valid entry point.' ); |
||
13 | } |
||
14 | |||
15 | if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) { |
||
16 | die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/WhatsNearby/">WhatsNearby</a> is only compatible with MediaWiki 1.23 or above. You need to upgrade MediaWiki first.' ); |
||
17 | } |
||
18 | |||
19 | // Do not initialize more than once. |
||
20 | if ( defined( 'WNBY_VERSION' ) ) { |
||
21 | return 1; |
||
22 | } |
||
23 | |||
24 | WhatsNearby::initExtension(); |
||
25 | |||
26 | $GLOBALS['wgExtensionFunctions'][] = function() { |
||
27 | WhatsNearby::onExtensionFunction(); |
||
28 | }; |
||
29 | |||
30 | /** |
||
31 | * @codeCoverageIgnore |
||
32 | */ |
||
33 | class WhatsNearby { |
||
0 ignored issues
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries. ![]() |
|||
34 | |||
35 | /** |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | 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);
}
}
![]() |
|||
39 | |||
40 | // Load DefaultSettings |
||
41 | require_once __DIR__ . '/DefaultSettings.php'; |
||
42 | |||
43 | define( 'WNBY_VERSION', '1.0.0-alpha' ); |
||
44 | |||
45 | $GLOBALS['wgExtensionCredits']['others'][] = array( |
||
46 | 'path' => __FILE__, |
||
47 | 'name' => 'Whats Nearby', |
||
48 | 'author' => array( |
||
49 | 'James Hong Kong' |
||
50 | ), |
||
51 | 'url' => 'https://www.semantic-mediawiki.org/wiki/Extension:WhatsNearby', |
||
52 | 'descriptionmsg' => 'wnby-desc', |
||
53 | 'version' => WNBY_VERSION, |
||
54 | 'license-name' => 'GPL-2.0-or-later' |
||
55 | ); |
||
56 | |||
57 | // Register message files |
||
58 | $GLOBALS['wgMessagesDirs']['WhatsNearby'] = __DIR__ . '/i18n'; |
||
59 | $GLOBALS['wgExtensionMessagesFiles']['WhatsNearbyMagic'] = __DIR__ . '/i18n/WhatsNearby.magic.php'; |
||
60 | |||
61 | $GLOBALS['wgResourceModules']['ext.whats.nearby.geoip'] = array( |
||
62 | 'localBasePath' => __DIR__ , |
||
63 | 'remoteExtPath' => 'WhatsNearby', |
||
64 | 'position' => 'bottom', |
||
65 | 'scripts' => array( |
||
66 | 'res/ext.whats.nearby.geoip.js' |
||
67 | ), |
||
68 | 'targets' => array( |
||
69 | 'mobile', |
||
70 | 'desktop' |
||
71 | ) |
||
72 | ); |
||
73 | |||
74 | $GLOBALS['wgResourceModules']['ext.whats.nearby'] = array( |
||
75 | 'localBasePath' => __DIR__ , |
||
76 | 'remoteExtPath' => 'WhatsNearby', |
||
77 | 'position' => 'bottom', |
||
78 | 'styles' => array( |
||
79 | 'res/ext.whats.nearby.css' |
||
80 | ), |
||
81 | 'scripts' => array( |
||
82 | 'res/ext.whats.nearby.js' |
||
83 | ), |
||
84 | 'dependencies' => array( |
||
85 | 'mediawiki.api', |
||
86 | 'mediawiki.api.parse', |
||
87 | 'ext.maps.services', |
||
88 | 'onoi.rangeslider', |
||
89 | 'onoi.blockUI', |
||
90 | 'onoi.md5', |
||
91 | 'onoi.blobstore' |
||
92 | ), |
||
93 | 'messages' => array( |
||
94 | 'wnby-geolocation-disabled', |
||
95 | 'wnby-geolocation-unsupported', |
||
96 | 'wnby-geolocation-unknown-error', |
||
97 | 'wnby-geolocation-timeout-error', |
||
98 | 'wnby-geolocation-position-unavailable', |
||
99 | 'wnby-geolocation-permission-denied', |
||
100 | 'wnby-no-fallback-location', |
||
101 | 'wnby-geolocation-geoip-fallback', |
||
102 | 'wnby-geolocation-geoip-no-fallback', |
||
103 | 'wnby-default-fallback-location', |
||
104 | 'wnby-invalid-coordinates-format', |
||
105 | 'wnby-localcache-use', |
||
106 | 'wnby-template-parameter-missing', |
||
107 | 'wnby-loading' |
||
108 | ), |
||
109 | 'targets' => array( |
||
110 | 'mobile', |
||
111 | 'desktop' |
||
112 | ) |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @since 1.0 |
||
118 | */ |
||
119 | public static function onExtensionFunction() { |
||
120 | $hookRegistry = new HookRegistry(); |
||
121 | $hookRegistry->register(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @since 1.0 |
||
126 | * |
||
127 | * @return string|null |
||
128 | */ |
||
129 | public static function getVersion() { |
||
130 | return WNBY_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.