Passed
Push — master ( cd28b2...551bb3 )
by Alain
04:04
created

ShortcodeUI::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * ShortcodeUI Class.
4
 *
5
 * @package   BrightNucleus\Shortcode
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Shortcode;
13
14
use Assert;
15
use BrightNucleus\Config\ConfigInterface;
16
use BrightNucleus\Config\ConfigTrait;
17
use BrightNucleus\Dependency\DependencyManagerInterface as DependencyManager;
18
use BrightNucleus\Exception\RuntimeException;
19
20
/**
21
 * Shortcode UI Class.
22
 *
23
 * @since   0.1.0
24
 *
25
 * @package BrightNucleus\Shortcode
26
 * @author  Alain Schlesser <[email protected]>
27
 */
28
class ShortcodeUI implements ShortcodeUIInterface {
29
30
	use ConfigTrait;
31
32
	/**
33
	 * Name of the shortcode handler.
34
	 *
35
	 * @since 0.1.0
36
	 *
37
	 * @var string
38
	 */
39
	protected $shortcode_tag;
40
41
	/**
42
	 * Dependencies to be enqueued.
43
	 *
44
	 * @since 0.1.0
45
	 *
46
	 * @var DependencyManagerInterface
47
	 */
48
	protected $dependencies;
49
50
	/**
51
	 * Instantiate Basic Shortcode UI.
52
	 *
53
	 * @since 1.0.
54
	 *
55
	 * @param string                 $shortcode_tag Tag of the Shortcode.
56
	 * @param ConfigInterface        $config        Configuration settings.
57
	 * @param DependencyManager|null $dependencies  Optional. Dependencies that
58
	 *                                              need to be enqueued.
59
	 * @throws RuntimeException If the config could not be processed.
60
	 */
61 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
		$shortcode_tag,
63
		ConfigInterface $config,
64
		DependencyManager $dependencies = null
65
	) {
66
		Assert\that( $shortcode_tag )->string()->notEmpty();
67
68
		$this->processConfig( $config );
69
70
		$this->shortcode_tag = $shortcode_tag;
71
		$this->dependencies  = $dependencies;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dependencies can also be of type object<BrightNucleus\Dep...ndencyManagerInterface>. However, the property $dependencies is declared as type object<BrightNucleus\Sho...ndencyManagerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72
	}
73
74
	/**
75
	 * Register the shortcode UI handler function with WordPress.
76
	 *
77
	 * @since 0.1.0
78
	 *
79
	 * @param mixed $context Data about the context in which the call is made.
80
	 * @return void
81
	 */
82
	public function register( $context = null ) {
83
		if ( ! $this->is_needed() ) {
84
			return;
85
		}
86
87
		\shortcode_ui_register_for_shortcode(
88
			$this->shortcode_tag,
89
			$this->config
90
		);
91
	}
92
93
	/**
94
	 * Check whether the shortcode UI is needed.
95
	 *
96
	 * @since 0.1.0
97
	 *
98
	 * @param mixed $context Data about the context in which the call is made.
99
	 * @return boolean Whether the shortcode UI is needed or not.
100
	 */
101 View Code Duplication
	protected function is_needed( $context = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
103
		$is_needed = $this->hasConfigKey( 'is_needed' )
104
			? $this->getConfigKey( 'is_needed' )
105
			: false;
106
107
		// Return true if a callable 'is_needed' evaluates to true.
108
		if ( is_callable( $is_needed ) ) {
109
			return $is_needed( $context );
110
		}
111
112
		return (bool) $is_needed;
113
	}
114
}
115