Completed
Push — master ( 0c7c3e...261f42 )
by mw
18:20
created

SemanticFormsSelect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onExtensionFunction() 0 10 3
A getVersion() 0 3 1
B initExtension() 0 31 1
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 28 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 SFS\HookRegistry;
4
5
/**
6
 * @see https://github.com/SemanticMediaWiki/SemanticFormsSelect/
7
 *
8
 * @defgroup SemanticFormsSelect Semantic Forms Select
9
 */
10
if ( !defined( 'MEDIAWIKI' ) ) {
11
	die( 'Not an entry point.' );
12
}
13
14
if ( defined( 'SFS_VERSION' ) ) {
15
	// Do not initialize more than once.
16
	return 1;
17
}
18
19
SemanticFormsSelect::initExtension();
20
21
$GLOBALS['wgExtensionFunctions'][] = function() {
22
	SemanticFormsSelect::onExtensionFunction();
23
};
24
25
/**
26
 * @codeCoverageIgnore
27
 */
28
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...
29
30
	/**
31
	 * @since 1.0
32
	 */
33
	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...
34
35
		define( 'SFS_VERSION', '1.4.0-alpha' );
36
37
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
38
			'path' => __FILE__,
39
			'name' => 'Semantic Forms Select',
40
			'author' =>array( 'Jason Zhang', 'Toni Hermoso Pulido', '...' ),
41
			'url' => 'https://www.mediawiki.org/wiki/Extension:SemanticFormsSelect',
42
			'description' => 'Allows to generate a select field in a semantic form whose values are retrieved from a query',
43
			'version'  => SFS_VERSION,
44
			'license-name'   => 'GPL-2.0+',
45
		);
46
47
		// Api modules
48
		$GLOBALS['wgAPIModules']['sformsselect'] = 'SFS\ApiSemanticFormsSelect';
49
50
		$GLOBALS['wgScriptSelectCount'] = 0;
51
		$GLOBALS['wgSF_Select_debug'] = 0;
52
53
		// Register resource files
54
		$GLOBALS['wgResourceModules']['ext.sf_select.scriptselect'] = array(
55
			'localBasePath' => __DIR__ ,
56
			'remoteExtPath' => 'SemanticFormsSelect',
57
			'position' => 'bottom',
58
			'scripts' => array( 'res/scriptSelect.js' ),
59
			'dependencies' => array(
60
				'ext.semanticforms.main'
61
			)
62
		);
63
	}
64
65
	/**
66
	 * @since 1.0
67
	 */
68
	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...
69
70
		if ( !defined( 'SF_VERSION' ) ) {
71
			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:SemanticForms">Semantic 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...
72
		}
73
74
		if ( isset( $GLOBALS['sfgFormPrinter'] )) {
75
			$GLOBALS['sfgFormPrinter']->setInputTypeHook( 'SF_Select', '\SFS\SemanticFormsSelect::init', array() );
76
		}
77
	}
78
79
	/**
80
	 * @since 1.0
81
	 *
82
	 * @return string|null
83
	 */
84
	public static function getVersion() {
85
		return SFS_VERSION;
86
	}
87
88
}
89