Completed
Push — master ( 99c0bb...0cd160 )
by Tobias
15:57 queued 14:35
created

SemanticScribunto::onExtensionFunction()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6346
c 0
b 0
f 0
cc 7
nc 5
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 15 and the first side effect is on line 10.

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\Scribunto\HookRegistry;
4
5
/**
6
 * @see https://github.com/SemanticMediaWiki/SemanticScribunto/
7
 *
8
 * @defgroup SemanticScribunto Semantic Scribunto
9
 */
10
SemanticScribunto::load();
11
12
/**
13
 * @codeCoverageIgnore
14
 */
15
class SemanticScribunto {
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...
16
17
	/**
18
	 * @since 1.0
19
	 *
20
	 * @note It is expected that this function is loaded before LocalSettings.php
21
	 * to ensure that settings and global functions are available by the time
22
	 * the extension is activated.
23
	 */
24
	public static function load() {
25
26
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
27
			include_once __DIR__ . '/vendor/autoload.php';
28
		}
29
	}
30
31
	/**
32
	 * @since 1.0
33
	 */
34
	public static function initExtension( $credits = array() ) {
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...
35
36
		// See https://phabricator.wikimedia.org/T151136
37
		define( 'SMW_SCRIBUNTO_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
38
39
		// Register message files
40
		$GLOBALS['wgMessagesDirs']['SemanticScribunto'] = __DIR__ . '/i18n';
41
	}
42
43
	/**
44
	 * @since 1.0
45
	 */
46
	public static function onExtensionFunction() {
47
48
		if ( !defined( 'SMW_VERSION' ) ) {
49
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
50
				die( "\nThe 'Semantic Scribunto' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
51
			} else {
52
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticScribunto/">Semantic Scribunto</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
53
			}
54
		}
55
56
		// Using the constant as indicator to avoid class_exists
57
		if ( !defined( 'CONTENT_MODEL_SCRIBUNTO' ) ) {
58
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
59
				die( "\nThe 'Semantic Scribunto' extension requires the 'Scribunto' extension to be installed and enabled.\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
60
			} else {
61
				die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticScribunto/">Semantic Scribunto</a> requires <a href="https://www.mediawiki.org/wiki/Extension:Scribunto">Scribunto</a>. Please install and enable the extension first.<br />' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
62
			}
63
		}
64
65
		$hookRegistry = new HookRegistry();
66
		$hookRegistry->register();
67
	}
68
69
	/**
70
	 * @since 1.0
71
	 *
72
	 * @param string|null $dependency
73
	 *
74
	 * @return string|null
75
	 */
76
	public static function getVersion( $dependency = null ) {
77
78
		if ( $dependency === null && defined( 'SMW_SCRIBUNTO_VERSION' ) ) {
79
			return SMW_SCRIBUNTO_VERSION;
80
		}
81
82
		if ( $dependency === 'SMW' && defined( 'SMW_VERSION' ) ) {
83
			return SMW_VERSION;
84
		}
85
86
		if ( $dependency === 'Lua' && method_exists( 'Scribunto_LuaStandaloneInterpreter', 'getLuaVersion' ) ) {
87
			return Scribunto_LuaStandaloneInterpreter::getLuaVersion( [ 'luaPath' => null ] );
88
		}
89
90
		return null;
91
	}
92
93
}
94