GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/gallery-template-clien... ( 9a51d7...15851d )
by Brad
03:30
created

render-functions.php ➔ foogallery_attachment_html_anchor()   F

Complexity

Conditions 15
Paths 1153

Size

Total Lines 71
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 40
nc 1153
nop 4
dl 0
loc 71
rs 2.4373
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
 *
4
 * FooGallery helper functions for rendering HTML
5
 * Created by Brad Vincent
6
 * Date: 11/07/2017
7
 *
8
 * @since 2.0.0
9
 */
10
11
/**
12
 * Returns the attachment image source only
13
 *
14
 * @param FooGalleryAttachment $foogallery_attachment
15
 * @param array $args
16
 *
17
 * @since 2.0.0
18
 *
19
 * @return string
20
 */
21
function foogallery_attachment_html_image_src( $foogallery_attachment, $args = array() ) {
22
	return apply_filters( 'foogallery_attachment_resize_thumbnail', $foogallery_attachment->url, $args, $foogallery_attachment );
23
}
24
25
/**
26
 * Returns the attachment img HTML
27
 *
28
 * @param FooGalleryAttachment $foogallery_attachment
29
 * @param array $args
30
 *
31
 * @since 2.0.0
32
 *
33
 * @return string
34
 */
35
function foogallery_attachment_html_image_full( $foogallery_attachment, $args = array() ) {
36
	$attr['data-src'] = foogallery_attachment_html_image_src( $foogallery_attachment, $args );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
37
38
	if ( ! empty( $this->alt ) ) {
39
		$attr['alt'] = $this->alt;
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
	}
41
42
	//pull any custom attributes out the args
43
	if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) {
44
		$attr = array_merge( $attr, $args['image_attributes'] );
45
	}
46
47
	//check for width and height args and add those to the image
48
	if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) {
49
		$attr['width'] = $args['width'];
50
	}
51
	if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) {
52
		$attr['height'] = $args['height'];
53
	}
54
55
	$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $foogallery_attachment );
56
	$attr = array_map( 'esc_attr', $attr );
57
	$html = '<img ';
58
	foreach ( $attr as $name => $value ) {
59
		$html .= " $name=" . '"' . $value . '"';
60
	}
61
	$html .= ' />';
62
63
	return apply_filters( 'foogallery_attachment_html_image', $html, $args, $foogallery_attachment );
64
}
65
66
/**
67
 * Returns the attachment anchor HTML
68
 *
69
 * @param FooGalleryAttachment $foogallery_attachment
70
 * @param array $args
71
 * @param bool $output_image
72
 * @param bool $output_closing_tag
73
 *
74
 * @return string
75
 */
76
function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) {
77
	if ( empty ( $this->url ) )  {
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
78
		return '';
79
	}
80
81
	$arg_defaults = array(
82
		'link' => 'image',
83
		'custom_link' => $this->custom_url
84
	);
85
86
	$args = wp_parse_args( $args, $arg_defaults );
87
88
	$link = $args['link'];
89
90
	$img = $this->html_img( $args );
91
92
	if ( 'page' === $link ) {
93
		//get the URL to the attachment page
94
		$url = get_attachment_link( $this->ID );
95
	} else if ( 'custom' === $link ) {
96
		$url = $args['custom_link'];
97
	} else {
98
		$url = $this->url;
99
	}
100
101
	//fallback for images that might not have a custom url
102
	if ( empty( $url ) ) {
103
		$url = $this->url;
104
	}
105
106
	$attr = array();
107
108
	//only add href and target attributes to the anchor if the link is NOT set to 'none'
109
	if ( $link !== 'none' ){
110
		$attr['href'] = $url;
111
		if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) {
112
			$attr['target'] = $this->custom_target;
113
		}
114
	}
115
116
	if ( ! empty( $this->caption ) ) {
117
		$attr['data-caption-title'] = $this->caption;
118
	}
119
120
	if ( !empty( $this->description ) ) {
121
		$attr['data-caption-desc'] = $this->description;
122
	}
123
124
	$attr['data-attachment-id'] = $this->ID;
125
126
	//pull any custom attributes out the args
127
	if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
128
		$attr = array_merge( $attr, $args['link_attributes'] );
129
	}
130
131
	$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment );
132
	$attr = array_map( 'esc_attr', $attr );
133
	$html = '<a ';
134
	foreach ( $attr as $name => $value ) {
135
		$html .= " $name=" . '"' . $value . '"';
136
	}
137
	$html .= '>';
138
	if ( $output_image ) {
139
		$html .= $img;
140
	}
141
	if ( $output_closing_tag ) {
142
		$html .= '</a>';
143
	};
144
145
	return apply_filters( 'foogallery_attachment_html_link', $html, $args, $foogallery_attachment );
146
}
147
148
/**
149
 * Returns generic html for captions
150
 *
151
 * @param FooGalleryAttachment $foogallery_attachment
152
 * @param $caption_content string Include title, desc, or both
153
 *
154
 * @return string
155
 */
156
function foogallery_attachment_html_caption( $foogallery_attachment, $caption_content ) {
157
	$html = '';
158
	$caption_html = array();
159
	if ( $foogallery_attachment->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) {
160
		$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>';
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
161
	}
162
	if ( $foogallery_attachment->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) {
163
		$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>';
164
	}
165
166
	if ( count($caption_html) > 0 ) {
167
		$html = '<div class="foogallery-caption"><div class="foogallery-caption-inner">';
168
		$html .= implode( $caption_html );
169
		$html .= '</div></div>';
170
	}
171
172
	return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this );
173
}