Passed
Push — master ( 3409e2...7cafe6 )
by Alain
36:07 queued 01:47
created

CheckNeedTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 23
ccs 0
cts 7
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A is_needed() 0 12 3
1
<?php
2
/**
3
 * Check Need Trait.
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
/**
15
 * Check Need Trait.
16
 *
17
 * Provides the `is_needed()` method.
18
 *
19
 * @since   0.1.0
20
 *
21
 * @package BrightNucleus\Shortcode
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
trait CheckNeedTrait {
25
26
	/**
27
	 * Check whether an element is needed.
28
	 *
29
	 * @since 0.2.0
30
	 *
31
	 * @param mixed $context Data about the context in which the call is made.
32
	 * @return boolean Whether the element is needed or not.
33
	 */
34
	protected function is_needed( $context = null ) {
35
36
		$is_needed = $this->hasConfigKey( 'is_needed' )
0 ignored issues
show
Bug introduced by
It seems like hasConfigKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
			? $this->getConfigKey( 'is_needed' )
0 ignored issues
show
Bug introduced by
It seems like getConfigKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
			: true;
39
40
		if ( is_callable( $is_needed ) ) {
41
			return $is_needed( $context );
42
		}
43
44
		return (bool) $is_needed;
45
	}
46
}
47