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

Instruction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addInstructions() 0 18 2
A generateInstructions() 0 13 1
A getInstructions() 0 7 2
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Component;
7
use GeminiLabs\Pollux\MetaBox\Instruction;
8
use GeminiLabs\Pollux\MetaBox\Validator;
9
use RecursiveArrayIterator;
10
use RecursiveIteratorIterator;
11
12
trait Instruction
13
{
14
	/**
15
	 * @return void
16
	 */
17
	protected function addInstructions()
18
	{
19
		if( !count( array_filter( $this->metaboxes, function( $metabox ) {
0 ignored issues
show
Bug introduced by
The property metaboxes 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...
20
			return $this->show( false, $metabox );
0 ignored issues
show
Bug introduced by
It seems like show() 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...
21
		})))return;
22
		$this->metaboxes[] = [
23
			'id' => 'infodiv',
24
			'post_types' => $this->getPostTypes(),
0 ignored issues
show
Bug introduced by
It seems like getPostTypes() 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...
25
			'title' => __( 'How to use in your theme', 'pollux' ),
26
			'context' => 'side',
27
			'priority' => 'low',
28
			'fields' => [[
29
				'slug' => '',
30
				'std' => $this->generateInstructions(),
31
				'type' => 'custom_html',
32
			]],
33
		];
34
	}
35
36
	/**
37
	 * @return string
38
	 */
39
	protected function generateInstructions()
40
	{
41
		return array_reduce( $this->getInstructions(), function( $html, $metabox ) {
42
			$fields = array_reduce( array_column( $metabox['fields'], 'slug' ), function( $html, $slug ) use( $metabox ) {
43
				$hook = sprintf( 'pollux/%s/instruction', $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...
44
				return $html . apply_filters( $hook, "PostMeta::get('{$slug}');", $slug, $metabox['slug'] ) . PHP_EOL;
45
			});
46
			return $html . sprintf( '<p><strong>%s</strong></p><pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
47
				$metabox['title'],
48
				$fields
49
			);
50
		});
51
	}
52
53
	/**
54
	 * @return array
55
	 */
56
	protected function getInstructions()
57
	{
58
		return array_filter( $this->metaboxes, function( $metabox ) {
59
			return $this->validate( $metabox['condition'] )
0 ignored issues
show
Bug introduced by
It seems like validate() 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...
60
				&& $this->hasPostType( $metabox );
0 ignored issues
show
Bug introduced by
It seems like hasPostType() 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...
61
		});
62
	}
63
}
64