Completed
Push — master ( dc1683...3e9aec )
by mw
07:38 queued 05:33
created

SemanticCompoundQueries   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 107
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 22 2
B initExtension() 0 26 1
A checkRequirements() 0 10 3
A onExtensionFunction() 0 17 1
A getVersion() 0 3 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 22 and the first side effect is on line 9.

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
/**
4
 * @see https://github.com/SemanticMediaWiki/SemanticCompoundQueries/
5
 *
6
 * @defgroup SemanticCompoundQueries SemanticCompoundQueries
7
 */
8
if ( !defined( 'MEDIAWIKI' ) ) {
9
	die( 'This file is part of the SemanticCompoundQueries extension, it is not a valid entry point.' );
10
}
11
12
if ( defined( 'SCQ_VERSION' ) ) {
13
	// Do not initialize more than once.
14
	return 1;
15
}
16
17
SemanticCompoundQueries::load();
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
class SemanticCompoundQueries {
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...
23
24
	/**
25
	 * @since 1.1
26
	 *
27
	 * @note It is expected that this function is loaded before LocalSettings.php
28
	 * to ensure that settings and global functions are available by the time
29
	 * the extension is activated.
30
	 */
31
	public static function load() {
0 ignored issues
show
Coding Style introduced by
load 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...
32
33
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
34
			include_once __DIR__ . '/vendor/autoload.php';
35
		}
36
37
		/**
38
		 * In case extension.json is being used, the succeeding steps are
39
		 * expected to be handled by the ExtensionRegistry aka extension.json
40
		 * ...
41
		 *
42
		 * 	"callback": "SemanticCompoundQueries::initExtension",
43
		 * 	"ExtensionFunctions": [
44
		 * 		"SemanticCompoundQueries::onExtensionFunction"
45
		 * 	],
46
		 */
47
		self::initExtension();
48
49
		$GLOBALS['wgExtensionFunctions'][] = function() {
50
			self::onExtensionFunction();
51
		};
52
	}
53
54
	/**
55
	 * @since 1.1
56
	 */
57
	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...
58
59
		define( 'SCQ_VERSION', '1.1.0-alpha' );
60
61
		// Register the extension
62
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
63
			'path' => __FILE__,
64
			'name' => 'Semantic Compound Queries',
65
			'version' => SCQ_VERSION,
66
			'author' => array(
67
				'[https://www.semantic-mediawiki.org/ Semantic MediaWiki project]',
68
				'Yaron Koren'
69
			),
70
			'url' => 'https://www.mediawiki.org/wiki/Extension:Semantic_Compound_Queries',
71
			'descriptionmsg' => 'semanticcompoundqueries-desc',
72
			'license-name' => 'GPL-2.0+'
73
		);
74
75
		// Register message files
76
		$GLOBALS['wgMessagesDirs']['SemanticCompoundQueries'] = __DIR__ . '/i18n';
77
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCompoundQueriesMagic'] = __DIR__ . '/i18n/SemanticCompoundQueries.i18n.magic.php';
78
79
		//$GLOBALS['wgAutoloadClasses']['SCQQueryProcessor'] = __DIR__ . '/SCQ_QueryProcessor.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
		//$GLOBALS['wgAutoloadClasses']['SCQQueryResult'] = __DIR__ . '/SCQ_QueryResult.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
		//$GLOBALS['wgAutoloadClasses']['SCQCompoundQueryApi'] = __DIR__ . '/SCQ_CompoundQueryApi.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
	}
83
84
	/**
85
	 * @since 1.1
86
	 */
87
	public static function checkRequirements() {
0 ignored issues
show
Coding Style introduced by
checkRequirements 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 ( version_compare( $GLOBALS[ 'wgVersion' ], '1.13', 'lt' ) ) {
90
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticCompoundQueries/">Semantic Compound Queries</a> is only compatible with MediaWiki 1.13 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkRequirements() 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...
91
		}
92
93
		if ( !defined( 'SMW_VERSION' ) ) {
94
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticCompoundQueries/">Semantic Compound Queries</a> requires the <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a> extension, please enable or install the extension first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkRequirements() 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...
95
		}
96
	}
97
98
	/**
99
	 * @since 1.1
100
	 */
101
	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...
102
103
		// Check requirements after LocalSetting.php has been processed, thid has
104
		// be done here to ensure SMW is loaded in case
105
		// wfLoadExtension( 'SemanticMediaWiki' ) is used
106
		self::checkRequirements();
107
108
		// wgAPIModules
109
		$GLOBALS['wgAPIModules']['compoundquery'] = 'SCQ\Api\CompoundQuery';
110
111
		$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function( Parser &$parser  ) {
112
			$parser->setFunctionHook( 'compound_query', array( '\SCQ\CompoundQueryProcessor', 'doCompoundQuery' ) );
113
114
			// always return true, in order not to stop MW's hook processing!
115
			return true;
116
		};
117
	}
118
119
	/**
120
	 * @since 1.1
121
	 *
122
	 * @return string|null
123
	 */
124
	public static function getVersion() {
125
		return SCQ_VERSION;
126
	}
127
128
}
129
130