Completed
Push — master ( 4f4d00...6903a5 )
by mw
03:06
created

SemanticFormsSelect::initExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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() {
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
		define( 'SFS_VERSION', '2.0.0-alpha' );
19
20
		// Api modules
21
		$GLOBALS['wgAPIModules']['sformsselect'] = 'SFS\ApiSemanticFormsSelect';
22
23
		$GLOBALS['wgScriptSelectCount'] = 0;
24
		$GLOBALS['wgSF_Select_debug'] = 0;
25
26
		// Register resource files
27
		$GLOBALS['wgResourceModules']['ext.sf_select.scriptselect'] = array(
28
			'localBasePath' => __DIR__ ,
29
			'remoteExtPath' => 'SemanticFormsSelect',
30
			'position' => 'bottom',
31
			'scripts' => array( 'res/scriptSelect.js' ),
32
			'dependencies' => array(
33
				'ext.pageforms.main'
34
			)
35
		);
36
	}
37
38
	/**
39
	 * @since 1.0
40
	 */
41
	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...
42
43
		if ( !defined( 'PF_VERSION' ) ) {
44
			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...
45
		}
46
47
		if ( isset( $GLOBALS['wgPageFormsFormPrinter'] )) {
48
			$GLOBALS['wgPageFormsFormPrinter']->setInputTypeHook( 'SF_Select', '\SFS\SemanticFormsSelect::init', array() );
49
		}
50
	}
51
52
	/**
53
	 * @since 1.0
54
	 *
55
	 * @return string|null
56
	 */
57
	public static function getVersion() {
58
		return is_string( SFS_VERSION ) ? SFS_VERSION : null;
59
	}
60
61
}
62