Completed
Push — master ( 28790c...69b7b6 )
by mw
02:44
created

SemanticBreadcrumbLinks::onExtensionFunction()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 7.8084
c 0
b 0
f 0
cc 7
nc 6
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 26 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 Semantic Breadcrumb Links extension. It is not a valid entry point.' );
14
}
15
16
if ( defined( 'SBL_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticBreadcrumbLinks::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
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...
27
28
	/**
29
	 * @since 1.3
30
	 */
31
	public static function load() {
32
33
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
34
			include_once __DIR__ . '/vendor/autoload.php';
35
		}
36
37
		// Load DefaultSettings
38
		require_once __DIR__ . '/DefaultSettings.php';
39
	}
40
41
	/**
42
	 * @since 1.3
43
	 */
44
	public static function initExtension( $credits = [] ) {
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...
45
46
		// See https://phabricator.wikimedia.org/T151136
47
		define( 'SBL_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
48
		define( 'SBL_PROP_PARENTPAGE', 'Has parent page' );
49
50
		// Register message files
51
		$GLOBALS['wgMessagesDirs']['SemanticBreadcrumbLinks'] = __DIR__ . '/i18n';
52
		$GLOBALS['wgExtensionMessagesFiles']['SemanticBreadcrumbLinksMagic'] = __DIR__ . '/i18n/SemanticBreadcrumbLinks.magic.php';
53
54
		// Register resource files
55
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks.styles'] = [
56
			'styles'  => 'res/sbl.styles.css',
57
			'localBasePath' => __DIR__ ,
58
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
59
			'position' => 'top',
60
			'group'    => 'ext.smw',
61
			'targets' => [
62
				'mobile',
63
				'desktop'
64
			]
65
		];
66
67
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks'] = [
68
			'scripts' => 'res/sbl.tooltip.js',
69
			'localBasePath' => __DIR__ ,
70
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
71
			'position' => 'top',
72
			'group'    => 'ext.smw',
73
			'dependencies'  => [
74
				'ext.semanticbreadcrumblinks.styles',
75
				'onoi.qtip'
76
			],
77
			'targets' => [
78
				'mobile',
79
				'desktop'
80
			]
81
		];
82
	}
83
84
	/**
85
	 * @since 1.3
86
	 */
87
	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...
88
89
		if ( !defined( 'SMW_VERSION' ) ) {
90
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
91
				die( "\nThe 'Semantic Breadcrumb Links' 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...
92
			} else {
93
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">Semantic Breadcrumb Links</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...
94
			}
95
		}
96
97
		// Default values are defined at this point to ensure
98
		// NS contants are specified prior
99
		$defaultPropertySearchPatternByNamespace = [
100
			NS_CATEGORY => [
101
				'_SUBC',
102
				'_SUBC',
103
				'_SUBC'
104
			],
105
			SMW_NS_PROPERTY => [
106
				'_SUBP',
107
				'_SUBP',
108
				'_SUBP'
109
			],
110
			NS_MAIN => [
111
				SBL_PROP_PARENTPAGE,
112
				SBL_PROP_PARENTPAGE,
113
				SBL_PROP_PARENTPAGE
114
			],
115
			NS_HELP => [
116
				SBL_PROP_PARENTPAGE,
117
				SBL_PROP_PARENTPAGE,
118
				SBL_PROP_PARENTPAGE
119
			]
120
		];
121
122
		// Cover legacy settings
123
		$deprecationNotices = [];
124
125
		if ( isset( $GLOBALS['egSBLBreadcrumbTrailStyleClass'] ) ) {
126
			$GLOBALS['sblgBreadcrumbTrailStyleClass'] = $GLOBALS['egSBLBreadcrumbTrailStyleClass'];
127
			$deprecationNotices['replacement']['egSBLBreadcrumbTrailStyleClass'] = 'sblgBreadcrumbTrailStyleClass';
128
		}
129
130
		if ( $deprecationNotices !== [] && !isset( $GLOBALS['smwgDeprecationNotices']['sbl'] ) ) {
131
			$GLOBALS['smwgDeprecationNotices']['sbl'] = [ 'replacement' => $deprecationNotices['replacement'] ];
132
		}
133
134
		$configuration = [
135
			'hideSubpageParent' => $GLOBALS['egSBLPageTitleToHideSubpageParent'],
136
			'breadcrumbTrailStyleClass' => $GLOBALS['sblgBreadcrumbTrailStyleClass'],
137
			'breadcrumbDividerStyleClass' => $GLOBALS['egSBLBreadcrumbDividerStyleClass'],
138
			'tryToFindClosestDescendant' => $GLOBALS['egSBLTryToFindClosestDescendant'],
139
			'useSubpageFinderFallback' => $GLOBALS['egSBLUseSubpageFinderFallback'],
140
			'enabledSubpageParentAnnotation' => $GLOBALS['egSBLEnabledSubpageParentAnnotation'],
141
			'disableTranslationSubpageAnnotation' => $GLOBALS['egSBLDisableTranslationSubpageAnnotation'],
142
			'wgNamespacesWithSubpages' => $GLOBALS['wgNamespacesWithSubpages'],
143
			'propertySearchPatternByNamespace' => $GLOBALS['egSBLPropertySearchPatternByNamespace'] + $defaultPropertySearchPatternByNamespace
144
		];
145
146
		$hookRegistry = new HookRegistry(
147
			ApplicationFactory::getInstance()->getStore(),
148
			new Options( $configuration )
149
		);
150
151
		$hookRegistry->register();
152
	}
153
154
	/**
155
	 * @since 1.3
156
	 *
157
	 * @return string|null
158
	 */
159
	public static function getVersion() {
160
		return SBL_VERSION;
161
	}
162
163
}
164