Passed
Push — develop ( a578dd...775faa )
by Paul
03:23
created

Notice   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 163
Duplicated Lines 18.4 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 30
loc 163
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 9 3
A __get() 0 7 2
A activateButton() 10 10 1
A button() 0 12 1
A generate() 0 10 3
A installButton() 10 10 1
A updateButton() 10 10 1
A title() 0 4 1
A addNotice() 0 9 1
A buildMessage() 0 8 3
A buildNotice() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use BadMethodCallException;
6
use Exception;
7
use GeminiLabs\Pollux\Application;
8
9
class Notice
10
{
11
	/**
12
	 * @var Application
13
	 */
14
	protected $app;
15
16
	public function __construct( Application $app )
17
	{
18
		$this->app = $app;
19
	}
20
21
	/**
22
	 * @method void addError( mixed $messages, array $args )
23
	 * @method void addInfo( mixed $messages, array $args )
24
	 * @method void addSuccess( mixed $messages, array $args )
25
	 * @method void addWarning( mixed $messages, array $args )
26
	 */
27
	public function __call( $name, $args )
28
	{
29
		$method = strtolower( $name );
30
		$status = substr( $method, 3 );
31
		if( 'add' == substr( $method, 0, 3 ) && in_array( $status, ['error', 'info', 'success', 'warning'] )) {
32
			return call_user_func_array( [$this, 'addNotice'], array_merge( [$status], $args ));
33
		}
34
		throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
35
	}
36
37
	/**
38
	 * @property array $all
39
	 */
40
	public function __get( $property )
41
	{
42
		if( $property == 'all' ) {
43
			return $this->app->notices;
44
		}
45
		throw new Exception( sprintf( 'Not a valid property: %s', $property ));
46
	}
47
48
	/**
49
	 * @return string
50
	 */
51 View Code Duplication
	public function activateButton( array $plugin )
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...
52
	{
53
		$actionUrl = self_admin_url( sprintf( 'plugins.php?action=activate&plugin=%s', $plugin['plugin'] ));
54
		return $this->button( sprintf( '%s %s', __( 'Activate', 'pollux' ), $plugin['name'] ), [
55
			'data-name' => $plugin['name'],
56
			'data-plugin' => $plugin['plugin'],
57
			'data-slug' => $plugin['slug'],
58
			'href' => wp_nonce_url( $actionUrl, sprintf( 'activate-plugin_%s', $plugin['plugin'] )),
59
		]);
60
	}
61
62
	/**
63
	 * @param string $title
64
	 * @param string $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
	 * @param string $attributes
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
	 * @return string
67
	 */
68
	public function button( $title, array $atts = [] )
69
	{
70
		$atts = wp_parse_args( $atts, [
71
			'class' => '',
72
			'href' => '',
73
		]);
74
		$atts['class'] = trim( $atts['class'] . ' button button-small' );
75
		$attributes = array_reduce( array_keys( $atts ), function( $carry, $key ) use( $atts ) {
76
			return $carry . sprintf( ' %s="%s"', $key, $atts[$key] );
77
		});
78
		return sprintf( '<a%s>%s</a>', $attributes, $title );
79
	}
80
81
	/**
82
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
83
	 */
84
	public function generate( array $notice, $unset = true )
85
	{
86
		if( $unset ) {
87
			$index = array_search( $notice, $this->app->notices );
88
			if( $index !== false ) {
89
				unset( $this->app->notices[$index] );
90
			}
91
		}
92
		return $this->buildNotice( $notice );
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98 View Code Duplication
	public function installButton( array $plugin )
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...
99
	{
100
		$actionUrl = self_admin_url( sprintf( 'update.php?action=install-plugin&plugin=%s', $plugin['slug'] ));
101
		return $this->button( sprintf( '%s %s', __( 'Install', 'pollux' ), $plugin['name'] ), [
102
			'data-name' => $plugin['name'],
103
			'data-plugin' => $plugin['plugin'],
104
			'data-slug' => $plugin['slug'],
105
			'href' => wp_nonce_url( $actionUrl, sprintf( 'install-plugin_%s', $plugin['slug'] )),
106
		]);
107
	}
108
109
	/**
110
	 * @return string
111
	 */
112 View Code Duplication
	public function updateButton( array $plugin )
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...
113
	{
114
		$actionUrl = self_admin_url( sprintf( 'update.php?action=upgrade-plugin&plugin=%s', $plugin['plugin'] ));
115
		return $this->button( sprintf( '%s %s', __( 'Update', 'pollux' ), $plugin['name'] ), [
116
			'data-name' => $plugin['name'],
117
			'data-plugin' => $plugin['plugin'],
118
			'data-slug' => $plugin['slug'],
119
			'href' => wp_nonce_url( $actionUrl, sprintf( 'upgrade-plugin_%s', $plugin['plugin'] )),
120
		]);
121
	}
122
123
	/**
124
	 * @param string $string
125
	 * @return string
126
	 */
127
	public function title( $string )
128
	{
129
		return sprintf( '<strong>%s</strong>', $string );
130
	}
131
132
	/**
133
	 * @param string $type
134
	 * @param string|array $messages
135
	 * @param bool $dismissible
136
	 * @return void
137
	 */
138
	protected function addNotice( $type, $messages, $dismissible = true )
139
	{
140
		$this->app->notices[] = [
141
			'dismissible' => $dismissible,
142
			'message' => $this->buildMessage( array_filter( (array) $messages )),
143
			'type' => $type,
144
		];
145
		$this->app->notices = array_unique( $this->app->notices, SORT_REGULAR );
146
	}
147
148
	/**
149
	 * @return string
150
	 */
151
	protected function buildMessage( array $messages )
152
	{
153
		foreach( $messages as $key => &$message ) {
154
			if( !is_wp_error( $message ))continue;
155
			$message = $message->get_error_message();
156
		}
157
		return wpautop( implode( PHP_EOL . PHP_EOL, $messages ));
158
	}
159
160
	/**
161
	 * @return string
162
	 */
163
	protected function buildNotice( array $notice )
164
	{
165
		$class = sprintf( 'notice notice-%s', $notice['type'] );
166
		if( $notice['dismissible'] ) {
167
			$class .= ' is-dismissible';
168
		}
169
		return sprintf( '<div class="pollux-notice %s">%s</div>', $class, $notice['message'] );
170
	}
171
}
172