Completed
Push — add/implement_pre_connection_j... ( 9bf864...afacc2 )
by
unknown
07:50
created

Message::score_plugins()   D

Complexity

Conditions 23
Paths 6

Size

Total Lines 91

Duplication

Lines 44
Ratio 48.35 %

Importance

Changes 0
Metric Value
cc 23
nc 6
nop 3
dl 44
loc 91
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Jetpack's JITM Message class.
4
 *
5
 * @package automattic/jetpack-jitm
6
 */
7
8
namespace Automattic\Jetpack\JITMS;
9
10
/**
11
 * Class JITM\Message
12
 *
13
 * Represents a message the client should display
14
 */
15
class Message {
16
	/**
17
	 * Message ID
18
	 *
19
	 * @var string
20
	 */
21
	public $id;
22
23
	/**
24
	 * Message content
25
	 *
26
	 * @var string
27
	 */
28
	protected $content;
29
30
	/**
31
	 * Call to action
32
	 *
33
	 * @var string
34
	 */
35
	protected $cta;
36
37
	/**
38
	 * Class constructor
39
	 *
40
	 * @param string $id Message ID.
41
	 */
42
	public function __construct( $id ) {
43
		$this->id      = $id;
44
		$this->content = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('message' => '', '...ck', 'list' => array()) of type array<string,string|arra...tring","list":"array"}> is incompatible with the declared type string of property $content.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
			'message' => '',
46
			'icon'    => 'jetpack',
47
			'list'    => array(),
48
		);
49
		$this->cta     = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('message' => '', '...lse, 'primary' => true) of type array<string,string|null...","primary":"boolean"}> is incompatible with the declared type string of property $cta.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
			'message'   => '',
51
			'hook'      => null,
52
			'newWindow' => false,
53
			'primary'   => true,
54
		);
55
	}
56
57
	/**
58
	 * Renders the internal state to a simple object
59
	 *
60
	 * @return \stdClass|bool The simple object
61
	 */
62
	public function render() {
63
64
		$obj                 = new \stdClass();
65
		$obj->content        = $this->content;
66
		$obj->cta            = $this->cta;
67
		$obj->id             = $this->id;
68
		$obj->is_dismissible = true;
69
70
		return $obj;
71
	}
72
73
	/**
74
	 * Show the specified message to the user
75
	 *
76
	 * @param string $message The message.
77
	 * @param string $description A longer description that shows up under the message.
78
	 *
79
	 * @return $this
80
	 */
81
	public function show( $message, $description = '' ) {
82
		$this->content['message']     = $message;
83
		$this->content['description'] = $description;
84
85
		return $this;
86
	}
87
88
	/**
89
	 * The message path that needs to match before showing
90
	 *
91
	 * @param string $regex The message path regex.
92
	 *
93
	 * @return $this
94
	 */
95
	public function message_path( $regex ) {
96
		$this->message_path_regex = $regex;
0 ignored issues
show
Bug introduced by
The property message_path_regex 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...
97
98
		return $this;
99
	}
100
101
	/**
102
	 * A call to action
103
	 *
104
	 * @param string $cta The message to display on the CTA button.
105
	 * @param string $link URL.
106
	 *
107
	 * @return $this
108
	 */
109
	public function with_cta( $cta, $link = '' ) {
110
		$this->cta['message'] = $cta;
111
		$this->cta['link']    = $link;
112
113
		return $this;
114
	}
115
116
}
117