Passed
Branch develop (f3ed2c)
by Paul
02:52
created

Condition   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 2
A normalizeCondition() 0 17 4
A validateClassExists() 0 4 1
A validateDefined() 0 4 1
A validateFunctionExists() 0 4 1
A validateHook() 0 4 1
A validateIsFrontPage() 0 4 1
A validateIsHome() 0 4 1
A validateIsPageTemplate() 0 4 1
A validateIsPluginActive() 0 4 1
A validateIsPluginInactive() 0 4 1
A validateUnknown() 0 4 1
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
trait Condition
6
{
7
	protected static $conditions = [
8
		'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home',
9
		'is_page_template', 'is_plugin_active', 'is_plugin_inactive',
10
	];
11
12
	/**
13
	 * @return bool
14
	 */
15
	public function validate( array $conditions )
16
	{
17
		array_walk( $conditions, function( &$value, $key ) {
18
			$method = $this->app->buildMethodName( $key, 'validate' );
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
			$value = method_exists( $this, $method )
20
				? $this->$method( $value )
21
				: $this->validateUnknown( $key, $value );
22
		});
23
		return !in_array( false, $conditions );
24
	}
25
26
	/**
27
	 * @param mixed $conditions
28
	 * @return array
29
	 */
30
	protected function normalizeCondition( $conditions )
31
	{
32
		$conditions = $this->toArray( $conditions );
0 ignored issues
show
Bug introduced by
It seems like toArray() 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...
33
		if( count( array_filter( array_keys( $conditions ), 'is_string' )) == 0 ) {
34
			foreach( $conditions as $key ) {
35
				$conditions[str_replace( '!', '', $key )] = substr( $key, 0, 1 ) == '!' ? 0 : 1;
36
			}
37
			$conditions = array_filter( $conditions, function( $key ) {
38
				return !is_numeric( $key );
39
			}, ARRAY_FILTER_USE_KEY );
40
		}
41
		$hook = sprintf( 'pollux/%s/conditions', $this->getClassname() );
0 ignored issues
show
Bug introduced by
It seems like getClassname() 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...
42
		return array_intersect_key(
43
			$conditions,
44
			array_flip( apply_filters( $hook, static::$conditions ))
45
		);
46
	}
47
48
	/**
49
	 * @param string $value
50
	 * @return bool
51
	 */
52
	protected function validateClassExists( $value )
53
	{
54
		return class_exists( $value );
55
	}
56
57
	/**
58
	 * @param string $value
59
	 * @return bool
60
	 */
61
	protected function validateDefined( $value )
62
	{
63
		return defined( $value );
64
	}
65
66
	/**
67
	 * @param string $value
68
	 * @return bool
69
	 */
70
	protected function validateFunctionExists( $value )
71
	{
72
		return function_exists( $value );
73
	}
74
75
	/**
76
	 * @param string $value
77
	 * @return bool
78
	 */
79
	protected function validateHook( $value )
80
	{
81
		return apply_filters( $value, true );
82
	}
83
84
	/**
85
	 * @param bool $value
86
	 * @return bool
87
	 */
88
	protected function validateIsFrontPage( $value )
89
	{
90
		return $value == ( $this->getPostId() == get_option( 'page_on_front' ));
0 ignored issues
show
Bug introduced by
It seems like getPostId() 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...
91
	}
92
93
	/**
94
	 * @param bool $value
95
	 * @return bool
96
	 */
97
	protected function validateIsHome( $value )
98
	{
99
		return $value == ( $this->getPostId() == get_option( 'page_for_posts' ));
0 ignored issues
show
Bug introduced by
It seems like getPostId() 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...
100
	}
101
102
	/**
103
	 * @param string $value
104
	 * @return bool
105
	 */
106
	protected function validateIsPageTemplate( $value )
107
	{
108
		return basename( get_page_template_slug( $this->getPostId() )) == $value;
0 ignored issues
show
Bug introduced by
It seems like getPostId() 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...
109
	}
110
111
	/**
112
	 * @param string $value
113
	 * @return bool
114
	 */
115
	protected function validateIsPluginActive( $value )
116
	{
117
		return is_plugin_active( $value );
118
	}
119
120
	/**
121
	 * @param string $value
122
	 * @return bool
123
	 */
124
	protected function validateIsPluginInactive( $value )
125
	{
126
		return is_plugin_inactive( $value );
127
	}
128
129
	/**
130
	 * @param string $key
131
	 * @param mixed $value
132
	 * @return bool
133
	 */
134
	protected function validateUnknown( $key, $value )
135
	{
136
		return apply_filters( 'pollux/metabox/condition', true, $key, $value );
137
	}
138
}
139