MslsOutput::get()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 2
nop 3
dl 0
loc 58
rs 7.983
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
use lloc\Msls\Map\HrefLang;
11
12
/**
13
 * Output in the frontend
14
 * @package Msls
15
 */
16
class MslsOutput extends MslsMain {
17
18
	/**
19
	 * Holds the format for the output
20
	 * @var array $tags
21
	 */
22
	protected $tags;
23
24
	/**
25
	 * Creates and gets the output as an array
26
	 *
27
	 * @param int $display
28
	 * @param bool $filter
29
	 * @param bool $exists
30
	 *
31
	 * @return array
32
	 * @uses MslsLink
33
	 * @uses MslsOptions
34
	 */
35
	public function get( $display, $filter = false, $exists = false ) {
36
		$arr = [];
37
38
		$blogs = $this->collection->get_filtered( $filter );
39
		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...
40
			$mydata = MslsOptions::create();
41
			$link   = MslsLink::create( $display );
42
43
			foreach ( $blogs as $blog ) {
44
				$language = $blog->get_language();
45
46
				$link->src = $this->options->get_flag_url( $language );
47
				$link->alt = $language;
48
49
				$is_current_blog = $this->collection->is_current_blog( $blog );
50
				if ( $is_current_blog ) {
51
					$url       = $mydata->get_current_link();
52
					$link->txt = $blog->get_description();
53
				} else {
54
					switch_to_blog( $blog->userblog_id );
55
56
					if ( $this->is_requirements_not_fulfilled( $mydata, $exists, $language ) ) {
57
						restore_current_blog();
58
						continue;
59
					} else {
60
						$url       = $mydata->get_permalink( $language );
61
						$link->txt = $blog->get_description();
62
					}
63
64
					restore_current_blog();
65
				}
66
67
				if ( has_filter( 'msls_output_get' ) ) {
68
					/**
69
					 * Returns HTML-link for an item of the output-arr
70
					 *
71
					 * @param string $url
72
					 * @param MslsLink $link
73
					 * @param bool $is_current_blog
74
					 *
75
					 * @since 0.9.8
76
					 *
77
					 */
78
					$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $is_current_blog );
79
				} else {
80
					$arr[] = sprintf(
81
						'<a href="%s" title="%s"%s>%s</a>',
82
						$url,
83
						$link->txt,
84
						$is_current_blog ? ' class="current_language"' : '',
85
						$link
86
					);
87
				}
88
			}
89
		}
90
91
		return $arr;
92
	}
93
94
	/**
95
	 * Get alternate links for the head section
96
	 *
97
	 * @return string
98
	 */
99
	public function get_alternate_links() {
100
		$blogs    = MslsBlogCollection::instance();
101
		$hreflang = new HrefLang( $blogs );
102
		$options  = MslsOptions::create();
103
104
		$arr     = [];
105
		$default = '';
106
107
		foreach ( $blogs->get_objects() as $blog ) {
108
			$url = apply_filters( 'mlsl_output_get_alternate_links', $blog->get_url( $options ), $blog );
0 ignored issues
show
Bug introduced by
It seems like $options defined by \lloc\Msls\MslsOptions::create() on line 102 can be null; however, lloc\Msls\MslsBlog::get_url() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
109
110
			if ( is_null( $url ) ) {
111
				continue;
112
			}
113
114
			$description = $blog->get_description();
115
116
			$format = '<link rel="alternate" hreflang="%s" href="%s" title="%s" />';
117
			if ( '' === $default ) {
118
				$default = sprintf( $format, 'x-default', $url, esc_attr( $description ) );
119
			}
120
121
			$arr[] = sprintf( $format, $hreflang->get( $blog->get_language() ), $url, esc_attr( $description ) );
122
		}
123
124
		return 1 === count( $arr ) ? $default : implode( PHP_EOL, $arr );
125
	}
126
127
	/**
128
	 * Returns a string when the object will be treated like a string
129
	 * @return string
130
	 */
131
	public function __toString() {
132
		$display = (int) $this->options->display;
133
		$filter  = false;
134
		$exists  = isset( $this->options->only_with_translation );
135
136
		$arr = $this->get( $display, $filter, $exists );
137
		if ( empty( $arr ) ) {
138
			return apply_filters( 'msls_output_no_translation_found', '' );
139
		}
140
141
		$tags = $this->get_tags();
142
143
		return $tags['before_output'] . $tags['before_item'] .
144
		       implode( $tags['after_item'] . $tags['before_item'], $arr ) .
145
		       $tags['after_item'] . $tags['after_output'];
146
	}
147
148
	/**
149
	 * Gets tags for the output
150
	 * @return array
151
	 */
152
	public function get_tags() {
153
		if ( empty( $this->tags ) ) {
154
			$this->tags = [
155
				'before_item'   => $this->options->before_item,
156
				'after_item'    => $this->options->after_item,
157
				'before_output' => $this->options->before_output,
158
				'after_output'  => $this->options->after_output,
159
			];
160
161
			/**
162
			 * Returns tags array for the output
163
			 *
164
			 * @param array $tags
165
			 *
166
			 * @since 1.0
167
			 *
168
			 */
169
			$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
170
		}
171
172
		return $this->tags;
173
	}
174
175
	/**
176
	 * Sets tags for the output
177
	 *
178
	 * @param array $arr
179
	 *
180
	 * @return MslsOutput
181
	 */
182
	public function set_tags( array $arr = [] ) {
183
		$this->tags = wp_parse_args( $this->get_tags(), $arr );
184
185
		return $this;
186
	}
187
188
	/**
189
	 * Returns true if the requirements not fulfilled
190
	 *
191
	 * @param MslsOptions|null $thing
192
	 * @param boolean $exists
193
	 * @param string $language
194
	 *
195
	 * @return boolean
196
	 */
197
	public function is_requirements_not_fulfilled( $thing, $exists, $language ) {
198
		if ( is_null( $thing ) ) {
199
			return $exists;
200
		}
201
202
		return MslsOptions::class != get_class( $thing ) && ! $thing->has_value( $language ) && $exists;
203
	}
204
205
}
206