Completed
Pull Request — master (#139)
by
unknown
11:31
created

helpers.php ➔ write_wpghs_link()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 75
Code Lines 41

Duplication

Lines 16
Ratio 21.33 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 7
eloc 41
c 3
b 0
f 2
nc 15
nop 1
dl 16
loc 75
ccs 0
cts 0
cp 0
crap 56
rs 6.5862

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
 * Theme helper functions.
4
 *
5
 * @package WordPress_GitHub_SYnc
6
 */
7
8
/**
9
 * Returns the HTML markup to view the current post on GitHub.
10
 *
11
 * @return string
12
 */
13
function get_the_github_view_link() {
14 1
	return '<a href="' . get_the_github_view_url() . '">' . apply_filters( 'wpghs_view_link_text', 'View this post on GitHub.' ) . '</a>';
15
}
16
add_shortcode( 'get_the_github_view_link', 'get_the_github_view_link' );
17
18
/**
19
 * Returns the URL to view the current post on GitHub.
20
 *
21
 * @return string
22
 */
23 2
function get_the_github_view_url() {
24
	$wpghs_post = new WordPress_GitHub_Sync_Post( get_the_ID(), WordPress_GitHub_Sync::$instance->api() );
25 2
26
	return $wpghs_post->github_view_url();
27
}
28
add_shortcode( 'get_the_github_view_url', 'get_the_github_view_url' );
29
30
/**
31
 * Returns the HTML markup to edit the current post on GitHub.
32
 *
33
 * @return string
34 1
 */
35
function get_the_github_edit_link() {
36
	return '<a href="' . get_the_github_edit_url() . '">' . apply_filters( 'wpghs_edit_link_text', 'Edit this post on GitHub.' ) . '</a>';
37
}
38
add_shortcode( 'get_the_github_edit_link', 'get_the_github_edit_link' );
39
40
/**
41
 * Returns the URL to edit the current post on GitHub.
42
 *
43 2
 * @return string
44
 */
45 2
function get_the_github_edit_url() {
46
	$wpghs_post = new WordPress_GitHub_Sync_Post( get_the_ID(), WordPress_GitHub_Sync::$instance->api() );
47
48
	return $wpghs_post->github_edit_url();
49
}
50
add_shortcode( 'get_the_github_edit_url', 'get_the_github_edit_url' );
51
52
53
/**
54
 * EXAMPLE FROM https://codex.wordpress.org/Shortcode_API
55
 * // [bartag foo="foo-value"]
56
 * function bartag_func( $atts ) {
57
 *	 $a = shortcode_atts( array(
58
 *		 'foo' => 'something',
59
 *		 'bar' => 'something else',
60
 *	 ), $atts );
61
 * 
62
 *	 return "foo = {$a['foo']}";
63
 * }
64
 * add_shortcode( 'bartag', 'bartag_func' );
65
 */
66
67
/**
68
 * Function with attributes
69
 *   - type: 'link' (default) to return a HTML anchor tag with text, or 'url' for bare URL.
70
 *   - target: 'view' (default) or 'edit' to return the respective link/url.
71
 *   - text: text to be included in the link. Ignored if type='url'.
72
 * 
73
 * Returns either a HTML formatted anchor tag or the bare URL of the current post on GitHub.
74
 *
75
 * @return string
76
 */
77
function write_wpghs_link( $atts ) {
78
79
	// extract is deprecated
80
	// extract(shortcode_atts(array(
81
	// 	'type' => 'link',
82
	// 	'target' => 'view',
83
	// 	'text' => ''
84
	// ), $atts));
85
86
	// Example from http://wordpress.stackexchange.com/a/99604
87
	// $args = shortcode_atts( 
88
	// 	array(
89
	// 		'w'   => '500',
90
	// 		'h'   => '330',
91
	// 		'q'   => '',
92
	// 		'geo' => 'US',
93
	// 	), 
94
    // $atts
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
95
	// );
96
	// $w = (int) $args['w'];
97
	// $h = (int) $args['h'];
98
	// $q = esc_attr( $args['q'] );
99
100
	$args = shortcode_atts(
101
		array(
102
			'type'   => 'link',
103
			'target' => 'view',
104
			'text'   => '',
105
		),
106
		$atts
107
	);
108
	$type   = esc_attr( $args['type'] );
109
	$target = esc_attr( $args['target'] );
110
	$text   = esc_attr( $args['text'] );
111
112
	$output = '';
113
114
	switch( $target ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
115 View Code Duplication
	case 'view':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 1
Loading history...
116
		$getter = get_the_github_view_url();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 3 tabs, found 2
Loading history...
117
		if(!empty( $text )) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 2
Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
118
			$linktext = $text;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 tabs, found 3
Loading history...
119
		} else {
120
			$linktext = 'View this post on GitHub';
121
		}
122
		break;
123 View Code Duplication
	case 'edit':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
124
		$getter = get_the_github_edit_url();
125
		if( !empty( $text ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
126
			$linktext = $text;
127
		} else {
128
			$linktext = 'Edit this post on GitHub';
129
		}
130
		break;
131
	default:
132
		$getter = get_the_github_view_url();
133
		$linktext = 'View this post on GitHub';
134
		break;
135
	}
136
137
	switch( $type ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
138
	case 'link':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 1
Loading history...
139
		$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 3 tabs, found 2
Loading history...
140
		break;
141
	case 'url':
142
		$output .= $getter;
143
		break;
144
	default:
145
		$output .= '<a href="' . $getter . '">' . $linktext . '</a>';
146
		break;
147
	}
148
149
	return $output;
150
151
}
152
add_shortcode( 'wpghs', 'write_wpghs_link' );
153