Passed
Push — ci ( 38d7bc...9f72b5 )
by litefeel
03:33
created

Writing_On_GitHub_Export::new_post()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 65
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 52
cp 0
rs 6.2553
c 0
b 0
f 0
cc 10
eloc 48
nc 8
nop 2
crap 110

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
 * GitHub Export Manager.
4
 *
5
 * @package Writing_On_GitHub
6
 */
7
8
/**
9
 * Class Writing_On_GitHub_Export
10
 */
11
class Writing_On_GitHub_Export {
12
13
	/**
14
	 * Application container.
15
	 *
16
	 * @var Writing_On_GitHub
17
	 */
18
	protected $app;
19
20
	/**
21
	 * Initializes a new export manager.
22
	 *
23
	 * @param Writing_On_GitHub $app Application container.
24
	 */
25
	public function __construct( Writing_On_GitHub $app ) {
26
		$this->app = $app;
27
	}
28
29
	/**
30
	 * Updates all of the current posts in the database on master.
31
	 *
32
     * @param  bool    $force
33
     *
34
     * @return string|WP_Error
35
     */
36
	public function full( $force = false ) {
37
		$posts = $this->app->database()->fetch_all_supported( $force );
38
39
		if ( is_wp_error( $posts ) ) {
40
            /* @var WP_Error $posts */
41
			return $posts;
42
		}
43
44
        $error = false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression false; of type false adds false to the return on line 58 which is incompatible with the return type documented by Writing_On_GitHub_Export::full of type string|WP_Error. It seems like you forgot to handle an error condition.
Loading history...
45
46 View Code Duplication
        foreach ( $posts as $post ) {
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...
47
            $result = $this->update( $post->id() );
48
            if ( is_wp_error( $result ) ) {
49
                if ( $error ) {
50
                    $error->add( $result->get_error_code(), $result->get_error_message() );
51
                } else {
52
                    $error = $result;
53
                }
54
            }
55
        }
56
57
        if ( is_wp_error( $error ) ) {
58
            return $error;
59
        }
60
61
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
62
	}
63
64
65
	/**
66
	 * Check if it exists in github
67
	 * @param  int  $post_id
68
	 * @return boolean
69
	 */
70
	protected function github_path( $post_id ) {
71
		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
72
73
		if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) {
74
			return $github_path;
75
		}
76
77
		return false;
78
	}
79
80
	/**
81
	 * Updates the provided post ID in master.
82
	 *
83
	 * @param int $post_id Post ID to update.
84
	 *
85
	 * @return string|WP_Error
86
	 */
87
	public function update( $post_id ) {
88
		$post = $this->app->database()->fetch_by_id( $post_id );
89
90
		if ( is_wp_error( $post ) ) {
91
            /* @var WP_Error $post */
92
			return $post;
93
		}
94
95
		if ( 'trash' === $post->status() ) {
96
			return $this->delete( $post_id );
97
		}
98
99
		if ( $old_github_path = $this->github_path( $post->id() ) ) {
100
			error_log("old_github_path: $old_github_path");
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
101
			$post->set_old_github_path($old_github_path);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
102
		}
103
104
		$result = $this->new_posts( array( $post ) );
0 ignored issues
show
Documentation introduced by
array($post) is of type array<integer,object<WP_...ting_On_GitHub_Post>"}>, but the function expects a array<integer,object<Writing_On_GitHub_Post>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
106
		if ( is_wp_error( $result ) ) {
107
            /* @var WP_Error $result */
108
			return $result;
109
		}
110
111
		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
112
	}
113
114
	/**
115
	 * Updates GitHub-created posts with latest WordPress data.
116
	 *
117
	 * @param Writing_On_GitHub_Post[] $posts Array of Posts to create.
118
	 *
119
	 * @return string|WP_Error
120
	 */
121
	public function new_posts( array $posts ) {
122
		$error = false;
123
124
		$persist = $this->app->api()->persist();
125
126 View Code Duplication
		foreach ( $posts as $post ) {
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...
127
			$result = $this->new_post( $post, $persist );
128
			if ( is_wp_error( $result ) ) {
129
				if ( $error ) {
130
					$error->add( $result->get_error_code(), $result->get_error_message() );
131
				} else {
132
					$error = $result;
133
				}
134
			}
135
		}
136
137
		if ( is_wp_error( $error ) ) {
138
			return $error;
139
		}
140
141
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
142
	}
143
144
	protected function new_post( $post, $persist ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
145
		$github_path = $post->github_path();
146
		$old_github_path = $post->old_github_path();
147
		$blob = $post->to_blob();
148
		$result = false;
149
150
		if ( $old_github_path && $old_github_path != $github_path ) {
151
			// rename
152
			$message = apply_filters(
153
				'wogh_commit_msg_move_post',
154
				sprintf(
155
					'Move %s to %s via WordPress at %s (%s)',
156
					$old_github_path, $github_path,
157
					site_url(),
158
					get_bloginfo( 'name' )
159
				)
160
			) . $this->get_commit_msg_tag();
161
162
			$result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message );
163
			if ( is_wp_error( $result ) ) {
164
				return $result;
165
			}
166
167
			$result = $persist->create_file( $blob, $message );
168
			if ( is_wp_error( $result ) ) {
169
				return $result;
170
			}
171
		} elseif ( ! $old_github_path ) {
172
			// create new
173
			$message = apply_filters(
174
				'wogh_commit_msg_new_post',
175
				sprintf(
176
					'Create new post %s from WordPress at %s (%s)',
177
					$github_path,
178
					site_url(),
179
					get_bloginfo( 'name' )
180
				)
181
			) . $this->get_commit_msg_tag();
182
			$result = $persist->create_file( $blob, $message );
183
			if ( is_wp_error( $result ) ) {
184
				return $result;
185
			}
186
		} elseif ( $old_github_path && $old_github_path == $github_path ) {
187
			// update
188
			$message = apply_filters(
189
				'wogh_commit_msg_update_post',
190
				sprintf(
191
					'Update post %s from WordPress at %s (%s)',
192
					$github_path,
193
					site_url(),
194
					get_bloginfo( 'name' )
195
				)
196
			) . $this->get_commit_msg_tag();
197
			$result = $persist->update_file( $blob, $message );
198
			if ( is_wp_error( $result ) ) {
199
				return $result;
200
			}
201
		}
202
203
		$sha = $result->content->sha;
204
		$post->set_sha($sha);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
205
		$post->set_old_github_path($github_path);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
206
207
		return true;
208
	}
209
210
	/**
211
	 * Deletes a provided post ID from master.
212
	 *
213
	 * @param int $post_id Post ID to delete.
214
	 *
215
	 * @return string|WP_Error
216
	 */
217
	public function delete( $post_id ) {
218
		$post = $this->app->database()->fetch_by_id( $post_id );
219
220
		if ( is_wp_error( $post ) ) {
221
            /* @var WP_Error $post */
222
			return $post;
223
		}
224
225
		$github_path = get_post_meta( $post_id, '_wogh_github_path', true );
226
227
		$message = apply_filters(
228
			'wogh_commit_msg_delete',
229
			sprintf(
230
				'Deleting %s via WordPress at %s (%s)',
231
				$github_path,
232
				site_url(),
233
				get_bloginfo( 'name' )
234
			),
235
			$post
236
		) . $this->get_commit_msg_tag();
237
238
		$result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
239
240
		if ( is_wp_error( $result ) ) {
241
            /* @var WP_Error $result */
242
			return $result;
243
		}
244
245
		return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
246
	}
247
248
249
	/**
250
	 * Saves the export user to the database.
251
	 *
252
	 * @param int $user_id User ID to export with.
253
	 *
254
	 * @return bool
255
	 */
256
	public function set_user( $user_id ) {
257
		return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
258
	}
259
260
	/**
261
	 * Gets the commit message tag.
262
	 *
263
	 * @return string
264
	 */
265
	protected function get_commit_msg_tag() {
266
		$tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
267
268
		if ( ! $tag ) {
269
			throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
270
		}
271
272
		return ' - ' . $tag;
273
	}
274
}
275