Completed
Push — master ( 37262a...492eab )
by mw
02:09
created

SemanticBreadcrumbLinks::initExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 1
eloc 34
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 34 and the first side effect is on line 13.

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 SBL\HookRegistry;
4
use SBL\Options;
5
use SMW\ApplicationFactory;
6
7
/**
8
 * @see https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/
9
 *
10
 * @defgroup SBL Semantic Breadcrumb Links
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the SemanticBreadcrumbLinks extension, it is not a valid entry point.' );
14
}
15
16
if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) {
17
	die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">SemanticBreadcrumbLinks</a> is only compatible with MediaWiki 1.23 or above. You need to upgrade MediaWiki first.' );
18
}
19
20
if ( defined( 'SBL_VERSION' ) ) {
21
	// Do not initialize more than once.
22
	return 1;
23
}
24
25
SemanticBreadcrumbLinks::initExtension();
26
27
$GLOBALS['wgExtensionFunctions'][] = function() {
28
	SemanticBreadcrumbLinks::onExtensionFunction();
29
};
30
31
/**
32
 * @codeCoverageIgnore
33
 */
34
class SemanticBreadcrumbLinks {
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...
35
36
	/**
37
	 * @since 1.3
38
	 */
39
	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...
40
41
		// Load DefaultSettings
42
		require_once __DIR__ . '/DefaultSettings.php';
43
44
		define( 'SBL_VERSION', '1.3.0' );
45
46
		define( 'SBL_PROP_PARENTPAGE', 'Has parent page' );
47
48
		// Register the extension
49
		$GLOBALS['wgExtensionCredits']['semantic'][ ] = array(
50
			'path'           => __FILE__,
51
			'name'           => 'Semantic Breadcrumb Links',
52
			'author'         => array( 'James Hong Kong' ),
53
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/',
54
			'descriptionmsg' => 'sbl-desc',
55
			'version'        => SBL_VERSION,
56
			'license-name'   => 'GPL-2.0+',
57
		);
58
59
		// Register message files
60
		$GLOBALS['wgMessagesDirs']['SemanticBreadcrumbLinks'] = __DIR__ . '/i18n';
61
62
		// Register resource files
63
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks.styles'] = array(
64
			'styles'  => 'res/sbl.styles.css',
65
			'localBasePath' => __DIR__ ,
66
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
67
			'position' => 'top',
68
			'group'    => 'ext.smw',
69
			'targets' => array(
70
				'mobile',
71
				'desktop'
72
			)
73
		);
74
75
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks'] = array(
76
			'scripts' => 'res/sbl.tooltip.js',
77
			'localBasePath' => __DIR__ ,
78
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
79
			'position' => 'top',
80
			'group'    => 'ext.smw',
81
			'dependencies'  => array(
82
				'ext.semanticbreadcrumblinks.styles',
83
				'onoi.qtip'
84
			),
85
			'targets' => array(
86
				'mobile',
87
				'desktop'
88
			)
89
		);
90
	}
91
92
	/**
93
	 * @since 1.3
94
	 */
95
	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...
96
97
		// Default values are defined at this point to ensure
98
		// NS contants are specified prior
99
		$defaultPropertySearchPatternByNamespace = array(
100
			NS_CATEGORY => array(
101
				'_SUBC',
102
				'_SUBC',
103
				'_SUBC'
104
			),
105
			SMW_NS_PROPERTY => array(
106
				'_SUBP',
107
				'_SUBP',
108
				'_SUBP'
109
			),
110
			NS_MAIN => array(
111
				SBL_PROP_PARENTPAGE,
112
				SBL_PROP_PARENTPAGE,
113
				SBL_PROP_PARENTPAGE
114
			),
115
			NS_HELP => array(
116
				SBL_PROP_PARENTPAGE,
117
				SBL_PROP_PARENTPAGE,
118
				SBL_PROP_PARENTPAGE
119
			)
120
		);
121
122
		$configuration = array(
123
			'hideSubpageParent' => $GLOBALS['egSBLPageTitleToHideSubpageParent'],
124
			'breadcrumbTrailStyleClass' => $GLOBALS['egSBLBreadcrumbTrailStyleClass'],
125
			'breadcrumbDividerStyleClass' => $GLOBALS['egSBLBreadcrumbDividerStyleClass'],
126
			'tryToFindClosestDescendant' => $GLOBALS['egSBLTryToFindClosestDescendant'],
127
			'useSubpageFinderFallback' => $GLOBALS['egSBLUseSubpageFinderFallback'],
128
			'enabledSubpageParentAnnotation' => $GLOBALS['egSBLEnabledSubpageParentAnnotation'],
129
			'wgNamespacesWithSubpages' => $GLOBALS['wgNamespacesWithSubpages'],
130
			'propertySearchPatternByNamespace' => $GLOBALS['egSBLPropertySearchPatternByNamespace'] + $defaultPropertySearchPatternByNamespace
131
		);
132
133
		$hookRegistry = new HookRegistry(
134
			ApplicationFactory::getInstance()->getStore(),
135
			new Options( $configuration )
136
		);
137
138
		$hookRegistry->register();
139
	}
140
141
	/**
142
	 * @since 1.3
143
	 *
144
	 * @return string|null
145
	 */
146
	public static function getVersion() {
147
		return SBL_VERSION;
148
	}
149
150
}
151