Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsOutput::get()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 33
nc 2
nop 3
dl 0
loc 57
rs 7.6759
c 0
b 0
f 0

How to fix   Long Method   

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
 * MslsOutput
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Output in the frontend
12
 * @package Msls
13
 */
14
class MslsOutput extends MslsMain {
15
16
	/**
17
	 * Holds the format for the output
18
	 * @var array $tags
19
	 */
20
	protected $tags;
21
22
	/**
23
	 * Creates and gets the output as an array
24
	 *
25
	 * @param int $display
26
	 * @param bool $filter
27
	 * @param bool $exists
28
	 *
29
	 * @uses MslsOptions
30
	 * @uses MslsLink
31
	 * @return array
32
	 */
33
	public function get( $display, $filter = false, $exists = false ) {
34
		$arr = array();
35
36
		$blogs = $this->collection->get_filtered( $filter );
37
		if ( $blogs ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blogs of type lloc\Msls\MslsBlog[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
			$mydata = MslsOptions::create();
39
			$link   = MslsLink::create( $display );
40
41
			foreach ( $blogs as $blog ) {
42
				$language = $blog->get_language();
43
				$url      = $mydata->get_current_link();
44
				$current  = ( $blog->userblog_id == $this->collection->get_current_blog_id() );
45
46
				if ( $current ) {
47
					$link->txt = $blog->get_description();
48
				} else {
49
					switch_to_blog( $blog->userblog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
50
51
					if ( $this->is_requirements_not_fulfilled( $mydata, $exists, $language ) ) {
52
						restore_current_blog();
53
						continue;
54
					} else {
55
						$url       = $mydata->get_permalink( $language );
56
						$link->txt = $blog->get_description();
57
					}
58
59
					restore_current_blog();
60
				}
61
62
				$link->src = $this->options->get_flag_url( $language );
63
				$link->alt = $language;
64
65
				if ( has_filter( 'msls_output_get' ) ) {
66
					/**
67
					 * Returns HTML-link for an item of the output-arr
68
					 * @since 0.9.8
69
					 *
70
					 * @param string $url
71
					 * @param MslsLink $link
72
					 * @param bool current
73
					 */
74
					$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $current );
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(string)" but found "( string )"
Loading history...
75
				}
76
				else {
77
					$arr[] = sprintf(
78
						'<a href="%s" title="%s"%s>%s</a>',
79
						$url,
80
						$link->txt,
81
						( $current ? ' class="current_language"' : '' ),
82
						$link
83
					);
84
				}
85
			}
86
		}
87
88
		return $arr;
89
	}
90
91
	/**
92
	 * Returns a string when the object will be treated like a string
93
	 * @return string
94
	 */
95
	public function __toString() {
96
		$display = (int) $this->options->display;
97
		$filter  = false;
98
		$exists  = isset( $this->options->only_with_translation );
99
100
		$arr = $this->get( $display, $filter, $exists );
101
		if ( empty( $arr ) ) {
102
			return '';
103
		}
104
105
		$tags = $this->get_tags();
106
107
		return $tags['before_output'] . $tags['before_item'] .
108
		       implode( $tags['after_item'] . $tags['before_item'], $arr ) .
109
		       $tags['after_item'] . $tags['after_output'];
110
	}
111
112
	/**
113
	 * Gets tags for the output
114
	 * @return array
115
	 */
116
	public function get_tags() {
117
		if ( empty( $this->tags ) ) {
118
			$this->tags = array(
119
				'before_item'   => $this->options->before_item,
120
				'after_item'    => $this->options->after_item,
121
				'before_output' => $this->options->before_output,
122
				'after_output'  => $this->options->after_output,
123
			);
124
125
			/**
126
			 * Returns tags array for the output
127
			 * @since 1.0
128
			 *
129
			 * @param array $tags
130
			 */
131
			$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(array)" but found "( array )"
Loading history...
132
		}
133
134
		return $this->tags;
135
	}
136
137
	/**
138
	 * Sets tags for the output
139
	 *
140
	 * @param array $arr
141
	 *
142
	 * @return MslsOutput
143
	 */
144
	public function set_tags( array $arr = [] ) {
145
		$this->tags = wp_parse_args( $this->get_tags(), $arr );
146
147
		return $this;
148
	}
149
150
	/**
151
	 * Returns true if the requirements not fulfilled
152
	 *
153
	 * @param MslsOptions|null $mydata
154
	 * @param boolean $exists
155
	 * @param string $language
156
	 *
157
	 * @return boolean
158
	 */
159
	public function is_requirements_not_fulfilled( $mydata, $exists, $language ) {
160
		return MslsOptions::class != get_class( $mydata ) && $exists && ( is_null( $mydata ) || ! $mydata->has_value( $language ) );
161
	}
162
}
163