Completed
Push — master ( 2a4fe5...e752fc )
by mw
02:40
created

SemanticFormsSelect::initExtension()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 1
nop 1
1
<?php
2
3
use SFS\HookRegistry;
4
5
/**
6
 * @see https://github.com/SemanticMediaWiki/SemanticFormsSelect/
7
 *
8
 * @defgroup SemanticFormsSelect Semantic Forms Select
9
 * @codeCoverageIgnore
10
 */
11
class SemanticFormsSelect {
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...
12
13
	/**
14
	 * @since 1.0
15
	 */
16
	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...
17
18
		// See https://phabricator.wikimedia.org/T151136 (extension.json)
19
		define( 'SFS_VERSION', isset( $credits['version'] ) ? $credits['version'] : '2.2.0-alpha' );
20
21
		// Api modules
22
		$GLOBALS['wgAPIModules']['sformsselect'] = 'SFS\ApiSemanticFormsSelect';
23
24
		$GLOBALS['wgScriptSelectCount'] = 0;
25
		$GLOBALS['wgSF_Select_debug'] = 0;
26
27
		// Register resource files
28
		$GLOBALS['wgResourceModules']['ext.sf_select.scriptselect'] = array(
29
			'localBasePath' => __DIR__ ,
30
			'remoteExtPath' => 'SemanticFormsSelect',
31
			'position' => 'bottom',
32
			'scripts' => array(
33
				'res/scriptSelect.js'
34
			),
35
			'dependencies' => array(
36
				'ext.pageforms.main'
37
			)
38
		);
39
	}
40
41
	/**
42
	 * @since 1.0
43
	 */
44
	public static function onExtensionFunction() {
0 ignored issues
show
Coding Style introduced by
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);
    }
}
Loading history...
45
46
		if ( !defined( 'PF_VERSION' ) ) {
47
			die( '<b>Error:</b><a href="https://github.com/SemanticMediaWiki/SemanticFormsSelect/">Semantic Forms Select</a> requires the <a href="https://www.mediawiki.org/wiki/Extension:PageForms">Page Forms</a> extension. Please install and activate this extension first.' );
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...
48
		}
49
50
		if ( isset( $GLOBALS['wgPageFormsFormPrinter'] )) {
51
			$GLOBALS['wgPageFormsFormPrinter']->setInputTypeHook( 'SF_Select', '\SFS\SemanticFormsSelect::init', array() );
52
		}
53
	}
54
55
	/**
56
	 * @since 1.0
57
	 *
58
	 * @param string $dependency
59
	 *
60
	 * @return string|null
61
	 */
62
	public static function getVersion( $dependency = null ) {
63
64
		if ( $dependency === null && defined( 'SFS_VERSION' ) ) {
65
			return SFS_VERSION;
66
		}
67
68
		if ( $dependency === 'PageForms' && defined( 'PF_VERSION' ) ) {
69
			return PF_VERSION;
70
		}
71
72
		return null;
73
	}
74
75
}
76