HasModule::module()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WPDFI\Traits;
4
5
/**
6
 * Trait for all classes which have sub-modules
7
 *
8
 * @author Duc Bui Quang <[email protected]>
9
 * @since 1.0.0
10
 */
11
12
Trait HasModule
13
{
14
	/**
15
	 * Sub-modules of this plugin
16
	 *
17
	 * @since 	1.0.0
18
	 * @var		array
19
	 */
20
	protected $modules;
21
22
	/**
23
	 * Load neccessary sub-modules for the this class.
24
	 * 
25
	 * @since 1.0.0
26
	 * @return \WPDFI\Traits\HasModule
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasModule is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
27
	 */
28
	abstract public function loadModules();
0 ignored issues
show
Coding Style introduced by
The function name loadModules is in camel caps, but expected load_modules instead as per the coding standard.
Loading history...
29
30
	/**
31
	 * Magic method to get properties
32
	 *
33
	 * @since  1.0.0
34
	 *
35
	 * @param  string $name
36
	 * @return mixed
37
	 */
38
	public function __get($name)
39
	{
40
		switch($name) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
41
			// Load sub-module first
42
			case isset($this->modules[$name]):
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
43
				return $this->modules[$name];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
44
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
45
			default:
46
				throw new \Exception(__("This property does not exists."));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal This property does not exists. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
47
				break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
		}
49
	}
50
51
	/**
52
	 * Trigger hook actions of all sub-modules
53
	 *
54
	 * @since 1.0.0 
55
	 * @return void
56
	 */
57
	public function moduleHooks() {
0 ignored issues
show
Coding Style introduced by
The function name moduleHooks is in camel caps, but expected module_hooks instead as per the coding standard.
Loading history...
58
		foreach($this->modules as $module) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
59
			if(method_exists($module, 'hooks')) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
60
				$module->hooks();
61
			}
62
		}
63
	}
64
	
65
	/**
66
	 * Load a sub-module or add a new sub-module to the vatweets().
67
	 * 
68
	 * @param	string	$name
69
	 * @param	mixed	$handle
70
	 * @since 	1.0.0
71
	 * @return  mixed $handle
72
	 */
73
	public function module($name, $handle = null) {
74
		if(!isset($this->modules[$name]))
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
75
			$this->modules[$name] = $handle;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
76
		return $this->modules[$name];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
77
	}
78
}