Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Test_Jetpack_Photon often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Test_Jetpack_Photon, and based on these observations, apply Extract Interface, too.
1 | <?php // phpcs:ignore WordPress.Files.FileName |
||
13 | class WP_Test_Jetpack_Photon extends Jetpack_Attachment_Test_Case { |
||
14 | /** |
||
15 | * Test image. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected static $test_image; |
||
20 | |||
21 | /** |
||
22 | * Save the existing globals. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $protected_globals; |
||
27 | |||
28 | /** |
||
29 | * ID for an author-level user created by this class. |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | protected static $author_id; |
||
34 | |||
35 | /** |
||
36 | * Special setups. |
||
37 | * |
||
38 | * @param Object $factory Testing factory. |
||
39 | */ |
||
40 | public static function wpSetUpBeforeClass( $factory ) { |
||
47 | |||
48 | /** |
||
49 | * Clean up the special sauce. |
||
50 | */ |
||
51 | public static function wpTearDownAfterClass() { |
||
54 | |||
55 | /** |
||
56 | * Sets up the test. |
||
57 | */ |
||
58 | public function setUp() { |
||
72 | |||
73 | /** |
||
74 | * Clean up after the test. |
||
75 | */ |
||
76 | public function tearDown() { |
||
100 | |||
101 | /** |
||
102 | * Helper to get a query string part from the data returned by the filter_image_downsize method |
||
103 | * |
||
104 | * @author zinigor |
||
105 | * @since 3.8.2 |
||
106 | * @param array $data an array of data returned by the filter. |
||
107 | * @return String $query_string |
||
108 | */ |
||
109 | protected function helper_get_query( $data ) { |
||
113 | |||
114 | /** |
||
115 | * Helper to get a new image object. |
||
116 | * |
||
117 | * @param string $size Test image size. Accepts 'large' (default) or 'medium'. |
||
118 | * @param bool $meta Meta data to pass to _create_upload_object. |
||
119 | * |
||
120 | * @return int Post ID (attachment) of the image. |
||
121 | */ |
||
122 | protected function helper_get_image( $size = 'large', $meta = true ) { |
||
151 | |||
152 | /** |
||
153 | * Helper to remove image sizes added in helper_get_image(). |
||
154 | */ |
||
155 | protected function helper_remove_image_sizes() { |
||
171 | |||
172 | /** |
||
173 | * Tests that Photon creates a Photon instance. |
||
174 | * |
||
175 | * @author scotchfield |
||
176 | * @covers Jetpack_Photon::instance |
||
177 | * @since 3.2 |
||
178 | */ |
||
179 | public function test_photon_instance() { |
||
182 | |||
183 | /** |
||
184 | * Tests that Photon creates a singleton. |
||
185 | * |
||
186 | * @author scotchfield |
||
187 | * @covers Jetpack_Photon::instance |
||
188 | * @since 3.2 |
||
189 | */ |
||
190 | public function test_photon_instance_singleton() { |
||
195 | |||
196 | /** |
||
197 | * Tests Photon's HTML parsing when there is nothing to parse. |
||
198 | * |
||
199 | * @author scotchfield |
||
200 | * @covers Jetpack_Photon::parse_images_from_html |
||
201 | * @since 3.2 |
||
202 | */ |
||
203 | public function test_photon_parse_images_from_html_empty() { |
||
208 | |||
209 | /** |
||
210 | * Helper function to get sample content files. |
||
211 | * |
||
212 | * @author scotchfield |
||
213 | * @return array |
||
214 | * @since 3.2 |
||
215 | * |
||
216 | * @param string $filename File name for sample content. |
||
217 | */ |
||
218 | private function get_photon_sample_content( $filename ) { |
||
226 | |||
227 | /** |
||
228 | * Tests Photon's HTML parsing. |
||
229 | * |
||
230 | * @author scotchfield |
||
231 | * @covers Jetpack_Photon::parse_images_from_html |
||
232 | * @since 3.2 |
||
233 | */ |
||
234 | public function test_photon_parse_images_from_html_a_tags_without_images() { |
||
239 | |||
240 | /** |
||
241 | * Tests Photon's HTML parsing. |
||
242 | * |
||
243 | * @author biskobe |
||
244 | * @covers Jetpack_Photon::filter_the_content |
||
245 | * @since 3.2 |
||
246 | */ |
||
247 | public function test_photon_parse_images_from_html_a_tags_with_hash_in_href() { |
||
260 | |||
261 | /** |
||
262 | * Tests Photon's HTML parsing. |
||
263 | * |
||
264 | * @author scotchfield |
||
265 | * @covers Jetpack_Photon::parse_images_from_html |
||
266 | * @since 3.2 |
||
267 | */ |
||
268 | public function test_photon_parse_images_from_html_empty_a_tag() { |
||
273 | |||
274 | /** |
||
275 | * Tests Photon's HTML parsing. |
||
276 | * |
||
277 | * @author scotchfield |
||
278 | * @covers Jetpack_Photon::parse_images_from_html |
||
279 | * @since 3.2 |
||
280 | */ |
||
281 | public function test_photon_parse_images_from_html_extra_attributes() { |
||
286 | |||
287 | /** |
||
288 | * Tests Photon's HTML parsing. |
||
289 | * |
||
290 | * @author scotchfield |
||
291 | * @covers Jetpack_Photon::parse_images_from_html |
||
292 | * @since 3.2 |
||
293 | */ |
||
294 | public function test_photon_parse_images_from_html_minimum_multiple_with_links() { |
||
299 | |||
300 | /** |
||
301 | * Tests Photon's HTML parsing. |
||
302 | * |
||
303 | * @author scotchfield |
||
304 | * @covers Jetpack_Photon::parse_images_from_html |
||
305 | * @since 3.2 |
||
306 | */ |
||
307 | public function test_photon_parse_images_from_html_minimum_multiple() { |
||
312 | |||
313 | /** |
||
314 | * Tests Photon's HTML parsing. |
||
315 | * |
||
316 | * @author scotchfield |
||
317 | * @covers Jetpack_Photon::parse_images_from_html |
||
318 | * @since 3.2 |
||
319 | */ |
||
320 | public function test_photon_parse_images_from_html_minimum() { |
||
325 | |||
326 | /** |
||
327 | * Tests that Photon will parse a multiline html snippet. |
||
328 | * |
||
329 | * @author scotchfield |
||
330 | * @covers Jetpack_Photon::parse_images_from_html |
||
331 | * @since 3.2 |
||
332 | */ |
||
333 | public function test_photon_parse_images_from_html_multiline() { |
||
338 | |||
339 | /** |
||
340 | * Tests Photon's parse of the src attribute. |
||
341 | * |
||
342 | * @author ccprog |
||
343 | * @covers Jetpack_Photon::parse_images_from_html |
||
344 | */ |
||
345 | public function test_photon_parse_images_from_html_src_attribute() { |
||
350 | |||
351 | /** |
||
352 | * Tests Photon will parse the dimensions from a filename when there is no value. |
||
353 | * |
||
354 | * @author scotchfield |
||
355 | * @covers Jetpack_Photon::parse_dimensions_from_filename |
||
356 | * @since 3.2 |
||
357 | */ |
||
358 | public function test_photon_parse_dimensions_from_filename_no_dimensions() { |
||
363 | |||
364 | /** |
||
365 | * Tests Photon will parse the dimensions from a filename for an invalid value. |
||
366 | * |
||
367 | * @author scotchfield |
||
368 | * @covers Jetpack_Photon::parse_dimensions_from_filename |
||
369 | * @since 3.2 |
||
370 | */ |
||
371 | public function test_photon_parse_dimensions_from_filename_no_dimensions_letter() { |
||
376 | |||
377 | /** |
||
378 | * Tests Photon will parse the dimensions from a filename for an invalid value. |
||
379 | * |
||
380 | * @author scotchfield |
||
381 | * @covers Jetpack_Photon::parse_dimensions_from_filename |
||
382 | * @since 3.2 |
||
383 | */ |
||
384 | public function test_photon_parse_dimensions_from_filename_invalid_dimensions() { |
||
389 | |||
390 | /** |
||
391 | * Tests Photon will parse the dimensions from a filename for a small value. |
||
392 | * |
||
393 | * @author scotchfield |
||
394 | * @covers Jetpack_Photon::parse_dimensions_from_filename |
||
395 | * @since 3.2 |
||
396 | */ |
||
397 | public function test_photon_parse_dimensions_from_filename_valid_dimensions() { |
||
402 | |||
403 | /** |
||
404 | * Tests Photon will parse the dimensions from a filename for a large value. |
||
405 | * |
||
406 | * @author scotchfield |
||
407 | * @covers Jetpack_Photon::parse_dimensions_from_filename |
||
408 | * @since 3.2 |
||
409 | */ |
||
410 | public function test_photon_parse_dimensions_from_filename_valid_large_dimensions() { |
||
415 | |||
416 | /** |
||
417 | * Tests Photon image_downsize filter will return accurate size for a full-size image. |
||
418 | * |
||
419 | * @author kraftbj |
||
420 | * @covers Jetpack_Photon::filter_image_downsize |
||
421 | * @since 3.8.2 |
||
422 | */ |
||
423 | public function test_photon_return_full_size_dimensions() { |
||
435 | |||
436 | /** |
||
437 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
438 | * |
||
439 | * @author kraftbj |
||
440 | * @covers Jetpack_Photon::filter_image_downsize |
||
441 | * @since 3.8.2 |
||
442 | */ |
||
443 | public function test_photon_return_large_size_dimensions() { |
||
458 | |||
459 | /** |
||
460 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
461 | * |
||
462 | * @author emilyatmobtown |
||
463 | * @covers Jetpack_Photon::filter_image_downsize |
||
464 | */ |
||
465 | public function test_photon_return_medium_large_size_dimensions() { |
||
480 | |||
481 | /** |
||
482 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
483 | * |
||
484 | * @author kraftbj |
||
485 | * @covers Jetpack_Photon::filter_image_downsize |
||
486 | * @since 3.8.2 |
||
487 | */ |
||
488 | public function test_photon_return_jetpack_soft_defined_size_dimensions() { |
||
503 | |||
504 | /** |
||
505 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
506 | * |
||
507 | * @author kraftbj |
||
508 | * @covers Jetpack_Photon::filter_image_downsize |
||
509 | * @since 3.8.2 |
||
510 | */ |
||
511 | public function test_photon_return_jetpack_soft_undefined_size_dimensions() { |
||
526 | |||
527 | /** |
||
528 | * Tests Photon image_downsize filter will return accurate size for an known size. |
||
529 | * |
||
530 | * @author kraftbj |
||
531 | * @covers Jetpack_Photon::filter_image_downsize |
||
532 | * @since 3.8.2 |
||
533 | */ |
||
534 | public function test_photon_return_jetpack_soft_undefined_zero_size_dimensions() { |
||
549 | |||
550 | /** |
||
551 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
552 | * |
||
553 | * @author kraftbj |
||
554 | * @covers Jetpack_Photon::filter_image_downsize |
||
555 | * @since 3.8.2 |
||
556 | */ |
||
557 | public function test_photon_return_jetpack_hard_defined_size_dimensions() { |
||
572 | |||
573 | /** |
||
574 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
575 | * |
||
576 | * @author kraftbj |
||
577 | * @covers Jetpack_Photon::filter_image_downsize |
||
578 | * @since 3.8.2 |
||
579 | */ |
||
580 | public function test_photon_return_jetpack_hard_undefined_size_dimensions() { |
||
595 | |||
596 | /** |
||
597 | * Tests Photon image_downsize filter will return accurate size for a known size. |
||
598 | * |
||
599 | * @author kraftbj |
||
600 | * @covers Jetpack_Photon::filter_image_downsize |
||
601 | * @since 3.8.2 |
||
602 | */ |
||
603 | public function test_photon_return_jetpack_hard_undefined_zero_size_dimensions() { |
||
618 | |||
619 | /** |
||
620 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
621 | * |
||
622 | * @author kraftbj |
||
623 | * @covers Jetpack_Photon::filter_image_downsize |
||
624 | * @since 3.8.2 |
||
625 | */ |
||
626 | public function test_photon_return_jetpack_soft_defined_after_upload_size_dimensions() { |
||
642 | |||
643 | /** |
||
644 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
645 | * |
||
646 | * @author kraftbj |
||
647 | * @covers Jetpack_Photon::filter_image_downsize |
||
648 | * @since 3.8.2 |
||
649 | */ |
||
650 | public function test_photon_return_jetpack_soft_undefined_after_upload_size_dimensions() { |
||
666 | |||
667 | /** |
||
668 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
669 | * |
||
670 | * @author kraftbj |
||
671 | * @covers Jetpack_Photon::filter_image_downsize |
||
672 | * @since 3.8.2 |
||
673 | */ |
||
674 | public function test_photon_return_jetpack_soft_undefined_zero_after_upload_size_dimensions() { |
||
690 | |||
691 | /** |
||
692 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
693 | * |
||
694 | * @author kraftbj |
||
695 | * @covers Jetpack_Photon::filter_image_downsize |
||
696 | * @since 3.8.2 |
||
697 | */ |
||
698 | public function test_photon_return_jetpack_hard_defined_after_upload_size_dimensions() { |
||
714 | |||
715 | /** |
||
716 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
717 | * |
||
718 | * @author kraftbj |
||
719 | * @covers Jetpack_Photon::filter_image_downsize |
||
720 | * @since 3.8.2 |
||
721 | */ |
||
722 | public function test_photon_return_jetpack_hard_undefined_after_upload_size_dimensions() { |
||
738 | |||
739 | /** |
||
740 | * Tests Photon image_downsize filter will return accurate size for an unknown-when-uploading size. |
||
741 | * |
||
742 | * @author kraftbj |
||
743 | * @covers Jetpack_Photon::filter_image_downsize |
||
744 | * @since 3.8.2 |
||
745 | */ |
||
746 | public function test_photon_return_jetpack_hard_undefined_zero_after_upload_size_dimensions() { |
||
762 | |||
763 | /** |
||
764 | * Tests Photon image_downsize will return a custom-size image. |
||
765 | * |
||
766 | * @author kraftbj |
||
767 | * @covers Jetpack_Photon::filter_image_downsize |
||
768 | * @since 3.8.2 |
||
769 | */ |
||
770 | public function test_photon_return_custom_size_array_dimensions() { |
||
785 | |||
786 | /** |
||
787 | * Tests that Photon will not return an image larger than the original via image_downsize. |
||
788 | * |
||
789 | * @author kraftbj |
||
790 | * @covers Jetpack_Photon::filter_image_downsize |
||
791 | * @since 3.9.0 |
||
792 | */ |
||
793 | View Code Duplication | public function test_photon_return_custom_size_array_dimensions_larger_than_original() { |
|
808 | |||
809 | /** |
||
810 | * Tests image_downsize filter for a defined size with no meta. |
||
811 | * |
||
812 | * @author dereksmart |
||
813 | * @covers Jetpack_Photon::filter_image_downsize |
||
814 | * @since 3.9.0 |
||
815 | */ |
||
816 | public function test_photon_return_jetpack_soft_defined_size_dimensions_no_meta() { |
||
831 | |||
832 | /** |
||
833 | * Tests image_downsize filter for a soft crop for an existing oversized image size. |
||
834 | * |
||
835 | * @author kraftbj |
||
836 | * @covers Jetpack_Photon::filter_image_downsize |
||
837 | * @since 3.9.0 |
||
838 | */ |
||
839 | public function test_photon_return_jetpack_soft_oversized() { |
||
855 | |||
856 | /** |
||
857 | * Tests image_downsize filter for a soft crop for an undefined size. |
||
858 | * |
||
859 | * @author dereksmart |
||
860 | * @covers Jetpack_Photon::filter_image_downsize |
||
861 | * @since 3.9.0 |
||
862 | */ |
||
863 | public function test_photon_return_jetpack_soft_undefined_size_dimensions_no_meta() { |
||
878 | |||
879 | /** |
||
880 | * Tests image_downsize filter for a soft crop after upload. |
||
881 | * |
||
882 | * @author kraftbj |
||
883 | * @covers Jetpack_Photon::filter_image_downsize |
||
884 | * @since 3.9.0 |
||
885 | */ |
||
886 | public function test_photon_return_jetpack_soft_oversized_after_upload() { |
||
902 | |||
903 | /** |
||
904 | * Tests Photon's image_downsize on a known image size. |
||
905 | * |
||
906 | * @author dereksmart |
||
907 | * @covers Jetpack_Photon::filter_image_downsize |
||
908 | * @since 3.9.0 |
||
909 | */ |
||
910 | public function test_photon_return_jetpack_hard_defined_size_dimensions_no_meta() { |
||
925 | |||
926 | /** |
||
927 | * Tests Photon's image_downsize filter with an undefined size. |
||
928 | * |
||
929 | * @author dereksmart |
||
930 | * @covers Jetpack_Photon::filter_image_downsize |
||
931 | * @since 3.9.0 |
||
932 | */ |
||
933 | public function test_photon_return_jetpack_hard_undefined_size_dimensions_no_meta() { |
||
948 | |||
949 | /** |
||
950 | * Tests image_downsize filter when there is no meta information available. |
||
951 | * |
||
952 | * @author dereksmart |
||
953 | * @covers Jetpack_Photon::filter_image_downsize |
||
954 | * @since 3.9.0 |
||
955 | */ |
||
956 | View Code Duplication | public function test_photon_return_custom_size_array_dimensions_no_meta() { |
|
971 | |||
972 | /** |
||
973 | * Tests Photon's filtering of the_content when both height/width are known. |
||
974 | * |
||
975 | * @author ebinnion |
||
976 | * @covers Jetpack_Photon::filter_the_content |
||
977 | * @since 5.6.0 |
||
978 | */ |
||
979 | public function test_photon_filter_the_content_does_not_remove_width_height_when_both_known() { |
||
992 | |||
993 | /** |
||
994 | * Test Photon's filtering of the_content when either width/height is not known. |
||
995 | * |
||
996 | * @author ebinnion |
||
997 | * @covers Jetpack_Photon::filter_the_content |
||
998 | * @since 5.6.0 |
||
999 | */ |
||
1000 | public function test_photon_filter_the_content_does_not_have_width_height_when_at_least_one_not_known() { |
||
1009 | |||
1010 | /** |
||
1011 | * Tests Photon's filtering of the_content with filtered args. |
||
1012 | * |
||
1013 | * @author ebinnion |
||
1014 | * @covers Jetpack_Photon::filter_the_content |
||
1015 | * @dataProvider photon_attributes_when_filtered_data_provider |
||
1016 | * @since 5.6.0 |
||
1017 | * |
||
1018 | * @param callable $filter_callback Filter callback. |
||
1019 | * @param bool $has_attributes If the attributes are filtered. |
||
1020 | * @param int $width Image width in pixels. |
||
1021 | * @param int $height Image height in pixels. |
||
1022 | */ |
||
1023 | public function test_photon_filter_the_content_width_height_attributes_when_image_args_filtered( $filter_callback, $has_attributes, $width, $height ) { |
||
1045 | |||
1046 | /** |
||
1047 | * Checks that Photon ignores data-width and data-height attributes when parsing the attributes. |
||
1048 | * |
||
1049 | * @author mmtr |
||
1050 | * @covers Jetpack_Photon::filter_the_content |
||
1051 | * @since 8.0.0 |
||
1052 | */ |
||
1053 | View Code Duplication | public function test_photon_filter_the_content_ignores_data_width_and_data_height_attributes() { |
|
1062 | |||
1063 | /** |
||
1064 | * Checks that Photon parses correctly the width and height attributes when they are not preceded by a space. |
||
1065 | * |
||
1066 | * @author mmtr |
||
1067 | * @covers Jetpack_Photon::filter_the_content |
||
1068 | * @since 8.0.0 |
||
1069 | */ |
||
1070 | View Code Duplication | public function test_photon_filter_the_content_parses_width_height_when_no_spaces_between_attributes() { |
|
1080 | |||
1081 | /** |
||
1082 | * Data provider for filtered attributes. |
||
1083 | * |
||
1084 | * @return array[] |
||
1085 | */ |
||
1086 | public function photon_attributes_when_filtered_data_provider() { |
||
1147 | |||
1148 | /** |
||
1149 | * Tests that Photon ignores percentage dimensions. It should fall back to e.g. a "size-foo" class. |
||
1150 | * |
||
1151 | * @covers Jetpack_Photon::filter_the_content |
||
1152 | */ |
||
1153 | public function test_photon_filter_the_content_percentage_width_and_height() { |
||
1168 | |||
1169 | /** |
||
1170 | * Tests that Photon will filter for an AMP response. |
||
1171 | * |
||
1172 | * @author westonruter |
||
1173 | * @covers Jetpack_Photon::filter_the_content |
||
1174 | * @dataProvider photon_attributes_when_amp_response |
||
1175 | * @since 7.6.0 |
||
1176 | * |
||
1177 | * @param string $sample_html Sample HTML. |
||
1178 | * @param string $photon_src Photon URL suffix (after the subdomain). |
||
1179 | */ |
||
1180 | public function test_photon_filter_the_content_for_amp_responses( $sample_html, $photon_src ) { |
||
1189 | |||
1190 | /** |
||
1191 | * Data provider for testing AMP responses. |
||
1192 | * |
||
1193 | * @return array |
||
1194 | */ |
||
1195 | public function photon_attributes_when_amp_response() { |
||
1207 | |||
1208 | /** |
||
1209 | * Tests that Photon will filter for AMP stories. |
||
1210 | * |
||
1211 | * @author westonruter |
||
1212 | * @covers Jetpack_Photon::filter_the_content |
||
1213 | * @covers Jetpack_AMP_Support::filter_photon_post_image_args_for_stories |
||
1214 | * @since 7.6.0 |
||
1215 | */ |
||
1216 | public function test_photon_filter_the_content_for_amp_story() { |
||
1243 | |||
1244 | /** |
||
1245 | * Tests that Photon does filter the URLs on REST API media requests in the view context. |
||
1246 | * |
||
1247 | * @group rest-api |
||
1248 | */ |
||
1249 | public function test_photon_cdn_in_rest_response_with_view_context() { |
||
1267 | |||
1268 | /** |
||
1269 | * Tests Photon does not filter the URL on REST API media requests in the edit context. |
||
1270 | * |
||
1271 | * @group rest-api |
||
1272 | */ |
||
1273 | public function test_photon_cdn_in_rest_response_with_edit_context() { |
||
1310 | |||
1311 | /** |
||
1312 | * Verifies that the REST API upload endpoint does not return with Photon URLs. |
||
1313 | * |
||
1314 | * The endpoint sets the context to edit, but not before the callback executes. |
||
1315 | |||
1316 | * @author kraftbj |
||
1317 | * @requires PHPUnit 7.5 |
||
1318 | * @covers Jetpack_Photon::should_rest_photon_image_downsize_insert_attachment |
||
1319 | * @group rest-api |
||
1320 | */ |
||
1321 | public function test_photon_cdn_in_rest_response_with_created_item() { |
||
1347 | |||
1348 | /** |
||
1349 | * Verifies that the REST API external-media/copy endpoint does not return |
||
1350 | * Photonized URLs. |
||
1351 | * |
||
1352 | * @author ebinnion |
||
1353 | * @requires PHPUnit 7.5 |
||
1354 | * @covers Jetpack_Photon::should_rest_photon_image_downsize_insert_attachment |
||
1355 | * @group rest-api |
||
1356 | */ |
||
1357 | public function test_photon_cdn_in_rest_response_external_media() { |
||
1377 | |||
1378 | /** |
||
1379 | * This function copies a file to an expected location and then returns a successful request to support mocking download_url(). |
||
1380 | * |
||
1381 | * @param mixed $pre By default, this is false. We can set it to any truthy value to pre-empty the request. |
||
1382 | * @param array $args An array of arguments for the request. |
||
1383 | * @return array |
||
1384 | */ |
||
1385 | public function pre_http_request_mocked_download_url( $pre, $args ) { |
||
1410 | |||
1411 | /** |
||
1412 | * Tests that Photon will not strip the dimensions from an external URL. |
||
1413 | * |
||
1414 | * @covers Jetpack_Photon::strip_image_dimensions_maybe |
||
1415 | */ |
||
1416 | public function test_photon_strip_image_dimensions_maybe_ignores_external_files() { |
||
1421 | |||
1422 | /** |
||
1423 | * Tests Photon stripping the image dimensions from filename. |
||
1424 | * |
||
1425 | * @covers Jetpack_Photon::strip_image_dimensions_maybe |
||
1426 | */ |
||
1427 | public function test_photon_strip_image_dimensions_maybe_strips_resized_string() { |
||
1447 | } |
||
1448 | |||
1450 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: