Completed
Pull Request — master (#22)
by Karsten
18:28
created

SemanticNotifications::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 11.

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.

Loading history...
2
3
use SMW\Notifications\HookRegistry;
4
5
/**
6
 * @see https://github.com/SemanticMediaWiki/SemanticNotifications/
7
 *
8
 * @defgroup SemanticNotifications Semantic Notifications
9
 */
10
if ( !defined( 'MEDIAWIKI' ) ) {
11
	die( 'This file is part of the Semantic Notifications extension, it is not a valid entry point.' );
12
}
13
14
if ( defined( 'SMW_NOTIFICATIONS_VERSION' ) ) {
15
	// Do not initialize more than once.
16
	return 1;
17
}
18
19
SemanticNotifications::load();
20
21
/**
22
 * @codeCoverageIgnore
23
 */
24
class SemanticNotifications {
0 ignored issues
show
Coding Style Compatibility introduced by
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.

Loading history...
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @note It is expected that this function is loaded before LocalSettings.php
30
	 * to ensure that settings and global functions are available by the time
31
	 * the extension is activated.
32
	 */
33
	public static function load() {
0 ignored issues
show
Coding Style introduced by
load 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);
    }
}
Loading history...
34
35
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
36
			include_once __DIR__ . '/vendor/autoload.php';
37
		}
38
39
		$GLOBALS['snogChangeNotificationDetectionPropertyExemptionList'] = array(
40
			'_MDAT',
41
			'_REDI'
42
		);
43
44
		// In case extension.json is being used, the the succeeding steps are
45
		// expected to be handled by the ExtensionRegistry
46
		self::initExtension();
47
48
		$GLOBALS['wgExtensionFunctions'][] = function() {
49
			self::onExtensionFunction();
50
		};
51
	}
52
53
	/**
54
	 * @since 1.0
55
	 */
56
	public static function initExtension() {
0 ignored issues
show
Coding Style introduced by
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);
    }
}
Loading history...
57
58
		define( 'SMW_NOTIFICATIONS_VERSION', '1.0.0-alpha' );
59
60
		// Register extension info
61
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
62
			'path'           => __FILE__,
63
			'name'           => 'Semantic Notifications',
64
			'author'         => array( 'James Hong Kong' ),
65
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticNotifications/',
66
			'descriptionmsg' => 'smw-notifications-desc',
67
			'version'        => SMW_NOTIFICATIONS_VERSION,
68
			'license-name'   => 'GPL-2.0-or-later'
69
		);
70
71
		// Register message files
72
		$GLOBALS['wgMessagesDirs']['SemanticNotifications'] = __DIR__ . '/i18n';
73
74
		self::onBeforeCreateEchoEvent();
75
	}
76
77
	/**
78
	 * Register the hook before the execution of ExtensionFunction
79
	 *
80
	 * @since 1.0
81
	 */
82
	public static function onBeforeCreateEchoEvent() {
0 ignored issues
show
Coding Style introduced by
onBeforeCreateEchoEvent 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);
    }
}
Loading history...
83
		$GLOBALS['wgHooks']['BeforeCreateEchoEvent'][] = "\SMW\Notifications\EchoNotificationsManager::initNotificationsDefinitions";
84
	}
85
86
	/**
87
	 * @since 1.0
88
	 */
89
	public static function doCheckRequirements() {
0 ignored issues
show
Coding Style introduced by
doCheckRequirements 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);
    }
}
Loading history...
90
91
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) {
92
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticNotifications/">Semantic Notifications</a> is only compatible with MediaWiki 1.27 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() 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...
93
		}
94
95
		// There is no good way to detect whether Echo is available or not without
96
		// making a class_exists, what should I say ...
97
		if ( !isset( $GLOBALS['wgMessagesDirs']['Echo'] ) ) {
98
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticNotifications/">Semantic Notifications</a> requires the Echo extension, please enable or install <a href="https://www.mediawiki.org/wiki/Extension:Echo">Echo(Notifications)</a> first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() 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...
99
		}
100
101
		if ( !defined( 'SMW_VERSION' ) ) {
102
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticNotifications/">Semantic Notifications</a> requires the Semantic MediaWiki extension, please enable or install <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a> first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() 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...
103
		}
104
	}
105
106
	/**
107
	 * @since 1.0
108
	 */
109
	public static function onExtensionFunction() {
110
111
		// Check requirements after LocalSetting.php has been processed
112
		self::doCheckRequirements();
113
114
		$hookRegistry = new HookRegistry();
115
		$hookRegistry->register();
116
	}
117
118
	/**
119
	 * @since 1.0
120
	 *
121
	 * @return string|null
122
	 */
123
	public static function getVersion() {
124
		return SMW_NOTIFICATIONS_VERSION;
125
	}
126
127
}
128