Conditions | 39 |
Paths | > 20000 |
Total Lines | 202 |
Code Lines | 126 |
Lines | 51 |
Ratio | 25.25 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
5 | protected function check_post_data( $post, $data, $context, $links ) { |
||
6 | $post_type_obj = get_post_type_object( $post->post_type ); |
||
7 | |||
8 | // Standard fields |
||
9 | $this->assertEquals( $post->ID, $data['id'] ); |
||
10 | $this->assertEquals( $post->post_name, $data['slug'] ); |
||
11 | $this->assertEquals( get_permalink( $post->ID ), $data['link'] ); |
||
12 | if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { |
||
13 | $this->assertNull( $data['date_gmt'] ); |
||
14 | } |
||
15 | $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] ); |
||
16 | |||
17 | if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { |
||
18 | $this->assertNull( $data['modified_gmt'] ); |
||
19 | } |
||
20 | $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] ); |
||
21 | |||
22 | // author |
||
23 | if ( post_type_supports( $post->post_type, 'author' ) ) { |
||
24 | $this->assertEquals( $post->post_author, $data['author'] ); |
||
25 | } else { |
||
26 | $this->assertEmpty( $data['author'] ); |
||
27 | } |
||
28 | |||
29 | // post_parent |
||
30 | if ( $post_type_obj->hierarchical ) { |
||
31 | $this->assertArrayHasKey( 'parent', $data ); |
||
32 | if ( $post->post_parent ) { |
||
33 | if ( is_int( $data['parent'] ) ) { |
||
34 | $this->assertEquals( $post->post_parent, $data['parent'] ); |
||
35 | } else { |
||
36 | $this->assertEquals( $post->post_parent, $data['parent']['id'] ); |
||
37 | $this->check_get_post_response( $data['parent'], get_post( $data['parent']['id'] ), 'view-parent' ); |
||
38 | } |
||
39 | } else { |
||
40 | $this->assertEmpty( $data['parent'] ); |
||
41 | } |
||
42 | } else { |
||
43 | $this->assertFalse( isset( $data['parent'] ) ); |
||
44 | } |
||
45 | |||
46 | // page attributes |
||
47 | View Code Duplication | if ( $post_type_obj->hierarchical && post_type_supports( $post->post_type, 'page-attributes' ) ) { |
|
48 | $this->assertEquals( $post->menu_order, $data['menu_order'] ); |
||
49 | } else { |
||
50 | $this->assertFalse( isset( $data['menu_order'] ) ); |
||
51 | } |
||
52 | |||
53 | // Comments |
||
54 | if ( post_type_supports( $post->post_type, 'comments' ) ) { |
||
55 | $this->assertEquals( $post->comment_status, $data['comment_status'] ); |
||
56 | $this->assertEquals( $post->ping_status, $data['ping_status'] ); |
||
57 | } else { |
||
58 | $this->assertFalse( isset( $data['comment_status'] ) ); |
||
59 | $this->assertFalse( isset( $data['ping_status'] ) ); |
||
60 | } |
||
61 | |||
62 | if ( 'post' === $post->post_type ) { |
||
63 | $this->assertEquals( is_sticky( $post->ID ), $data['sticky'] ); |
||
64 | } |
||
65 | |||
66 | if ( 'post' === $post->post_type && 'edit' === $context ) { |
||
67 | $this->assertEquals( $post->post_password, $data['password'] ); |
||
68 | } |
||
69 | |||
70 | if ( 'page' === $post->post_type ) { |
||
71 | $this->assertEquals( get_page_template_slug( $post->ID ), $data['template'] ); |
||
72 | } |
||
73 | |||
74 | View Code Duplication | if ( post_type_supports( $post->post_type, 'thumbnail' ) ) { |
|
75 | $this->assertEquals( (int) get_post_thumbnail_id( $post->ID ), $data['featured_media'] ); |
||
76 | } else { |
||
77 | $this->assertFalse( isset( $data['featured_media'] ) ); |
||
78 | } |
||
79 | |||
80 | // Check post format. |
||
81 | if ( post_type_supports( $post->post_type, 'post-formats' ) ) { |
||
82 | $post_format = get_post_format( $post->ID ); |
||
83 | if ( empty( $post_format ) ) { |
||
84 | $this->assertEquals( 'standard', $data['format'] ); |
||
85 | } else { |
||
86 | $this->assertEquals( get_post_format( $post->ID ), $data['format'] ); |
||
87 | } |
||
88 | } else { |
||
89 | $this->assertFalse( isset( $data['format'] ) ); |
||
90 | } |
||
91 | |||
92 | // Check filtered values. |
||
93 | if ( post_type_supports( $post->post_type, 'title' ) ) { |
||
94 | add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); |
||
95 | $this->assertEquals( get_the_title( $post->ID ), $data['title']['rendered'] ); |
||
96 | remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); |
||
97 | if ( 'edit' === $context ) { |
||
98 | $this->assertEquals( $post->post_title, $data['title']['raw'] ); |
||
99 | } else { |
||
100 | $this->assertFalse( isset( $data['title']['raw'] ) ); |
||
101 | } |
||
102 | } else { |
||
103 | $this->assertFalse( isset( $data['title'] ) ); |
||
104 | } |
||
105 | |||
106 | View Code Duplication | if ( post_type_supports( $post->post_type, 'editor' ) ) { |
|
107 | // TODO: apply content filter for more accurate testing. |
||
108 | if ( ! $post->post_password ) { |
||
109 | $this->assertEquals( wpautop( $post->post_content ), $data['content']['rendered'] ); |
||
110 | } |
||
111 | |||
112 | if ( 'edit' === $context ) { |
||
113 | $this->assertEquals( $post->post_content, $data['content']['raw'] ); |
||
114 | } else { |
||
115 | $this->assertFalse( isset( $data['content']['raw'] ) ); |
||
116 | } |
||
117 | } else { |
||
118 | $this->assertFalse( isset( $data['content'] ) ); |
||
119 | } |
||
120 | |||
121 | View Code Duplication | if ( post_type_supports( $post->post_type, 'excerpt' ) ) { |
|
122 | if ( empty( $post->post_password ) ) { |
||
123 | // TODO: apply excerpt filter for more accurate testing. |
||
124 | $this->assertEquals( wpautop( $post->post_excerpt ), $data['excerpt']['rendered'] ); |
||
125 | } else { |
||
126 | // TODO: better testing for excerpts for password protected posts. |
||
127 | } |
||
128 | if ( 'edit' === $context ) { |
||
129 | $this->assertEquals( $post->post_excerpt, $data['excerpt']['raw'] ); |
||
130 | } else { |
||
131 | $this->assertFalse( isset( $data['excerpt']['raw'] ) ); |
||
132 | } |
||
133 | } else { |
||
134 | $this->assertFalse( isset( $data['excerpt'] ) ); |
||
135 | } |
||
136 | |||
137 | $this->assertEquals( $post->guid, $data['guid']['rendered'] ); |
||
138 | |||
139 | if ( 'edit' === $context ) { |
||
140 | $this->assertEquals( $post->guid, $data['guid']['raw'] ); |
||
141 | $this->assertEquals( $post->post_status, $data['status'] ); |
||
142 | |||
143 | if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { |
||
144 | $this->assertNull( $data['date_gmt'] ); |
||
145 | } else { |
||
146 | $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] ); |
||
147 | } |
||
148 | |||
149 | if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) { |
||
150 | $this->assertNull( $data['modified_gmt'] ); |
||
151 | } else { |
||
152 | $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] ); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | $taxonomies = wp_list_filter( get_object_taxonomies( $post->post_type, 'objects' ), array( 'show_in_rest' => true ) ); |
||
157 | foreach ( $taxonomies as $taxonomy ) { |
||
158 | $this->assertTrue( isset( $data[ $taxonomy->rest_base ] ) ); |
||
159 | $terms = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'ids' ) ); |
||
160 | sort( $terms ); |
||
161 | sort( $data[ $taxonomy->rest_base ] ); |
||
162 | $this->assertEquals( $terms, $data[ $taxonomy->rest_base ] ); |
||
163 | } |
||
164 | |||
165 | // test links |
||
166 | if ( $links ) { |
||
167 | |||
168 | $links = test_rest_expand_compact_links( $links ); |
||
169 | $post_type = get_post_type_object( $data['type'] ); |
||
170 | $this->assertEquals( $links['self'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] ) ); |
||
171 | $this->assertEquals( $links['collection'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base ) ); |
||
172 | $this->assertEquals( $links['about'][0]['href'], rest_url( 'wp/v2/types/' . $data['type'] ) ); |
||
173 | |||
174 | View Code Duplication | if ( post_type_supports( $post->post_type, 'author' ) && $data['author'] ) { |
|
175 | $this->assertEquals( $links['author'][0]['href'], rest_url( 'wp/v2/users/' . $data['author'] ) ); |
||
176 | } |
||
177 | |||
178 | View Code Duplication | if ( post_type_supports( $post->post_type, 'comments' ) ) { |
|
179 | $this->assertEquals( $links['replies'][0]['href'], add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/comments' ) ) ); |
||
180 | } |
||
181 | |||
182 | View Code Duplication | if ( post_type_supports( $post->post_type, 'revisions' ) ) { |
|
183 | $this->assertEquals( $links['version-history'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['id'] . '/revisions' ) ); |
||
184 | } |
||
185 | |||
186 | View Code Duplication | if ( $post_type->hierarchical && ! empty( $data['parent'] ) ) { |
|
187 | $this->assertEquals( $links['up'][0]['href'], rest_url( 'wp/v2/' . $post_type->rest_base . '/' . $data['parent'] ) ); |
||
188 | } |
||
189 | |||
190 | if ( ! in_array( $data['type'], array( 'attachment', 'nav_menu_item', 'revision' ) ) ) { |
||
191 | $this->assertEquals( $links['https://api.w.org/attachment'][0]['href'], add_query_arg( 'parent', $data['id'], rest_url( 'wp/v2/media' ) ) ); |
||
192 | } |
||
193 | |||
194 | if ( ! empty( $data['featured_media'] ) ) { |
||
195 | $this->assertEquals( $links['https://api.w.org/featuredmedia'][0]['href'], rest_url( 'wp/v2/media/' . $data['featured_media'] ) ); |
||
196 | } |
||
197 | |||
198 | $num = 0; |
||
199 | foreach ( $taxonomies as $key => $taxonomy ) { |
||
200 | $this->assertEquals( $taxonomy->name, $links['https://api.w.org/term'][ $num ]['attributes']['taxonomy'] ); |
||
201 | $this->assertEquals( add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/' . $taxonomy->rest_base ) ), $links['https://api.w.org/term'][ $num ]['href'] ); |
||
202 | $num++; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | } |
||
207 | |||
314 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.