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:
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 13 | class AssetsTest extends \WorDBless\BaseTestCase { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Test get_file_url_for_environment |
||
| 17 | * |
||
| 18 | * @author ebinnion goldsounds |
||
| 19 | * @dataProvider get_file_url_for_environment_data_provider |
||
| 20 | * |
||
| 21 | * @param string $min_path minified path. |
||
| 22 | * @param string $non_min_path non-minified path. |
||
| 23 | * @param bool $is_script_debug Is SCRIPT_DEBUG enabled. |
||
| 24 | * @param string $expected Expected result. |
||
| 25 | * @param string $not_expected Non expected result. |
||
| 26 | */ |
||
| 27 | View Code Duplication | public function test_get_file_url_for_environment( $min_path, $non_min_path, $is_script_debug, $expected, $not_expected ) { |
|
| 28 | Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
||
|
|
|||
| 29 | $file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path ); |
||
| 30 | |||
| 31 | // note the double-$$ here, $(non_)min_path is referenced by var name. |
||
| 32 | $this->assertContains( $$expected, $file_url ); |
||
| 33 | $this->assertNotContains( $$not_expected, $file_url ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Test that get_file_url_for_environment returns a full URL when given a full URL |
||
| 38 | * |
||
| 39 | * @author jeherve |
||
| 40 | * @dataProvider get_file_url_for_environment_full_urls_data_provider |
||
| 41 | * |
||
| 42 | * @param string $url Full URL we want to enqueue. |
||
| 43 | */ |
||
| 44 | public function test_get_file_url_for_environment_full_url( $url ) { |
||
| 45 | $file_url = Assets::get_file_url_for_environment( $url, $url ); |
||
| 46 | |||
| 47 | $this->assertEquals( $url, $file_url ); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Test that get_file_url_for_environment returns a full package asset url when package path is provided. |
||
| 52 | * |
||
| 53 | * @param string $min_path minified path. |
||
| 54 | * @param string $non_min_path non-minified path. |
||
| 55 | * @param string $package_path Package path. |
||
| 56 | * @param bool $is_script_debug Is SCRIPT_DEBUG enabled. |
||
| 57 | * @param string $expected Expected result. |
||
| 58 | * @param string $not_expected Non expected result. |
||
| 59 | * |
||
| 60 | * @author davidlonjon |
||
| 61 | * @dataProvider get_file_url_for_environment_package_path_data_provider |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function test_get_file_url_for_environment_package_path( $min_path, $non_min_path, $package_path, $is_script_debug, $expected, $not_expected ) { |
|
| 64 | Constants::set_constant( 'SCRIPT_DEBUG', $is_script_debug ); |
||
| 65 | $file_url = Assets::get_file_url_for_environment( $min_path, $non_min_path, $package_path ); |
||
| 66 | |||
| 67 | $this->assertContains( $expected, $file_url ); |
||
| 68 | $this->assertNotContains( $not_expected, $file_url ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Tests ability for a filter to map specific URLs. |
||
| 73 | * |
||
| 74 | * @author kraftbj |
||
| 75 | * @see p58i-8nS-p2 |
||
| 76 | */ |
||
| 77 | public function test_get_file_url_for_environment_with_filter() { |
||
| 78 | add_filter( |
||
| 79 | 'jetpack_get_file_for_environment', |
||
| 80 | function() { |
||
| 81 | return 'special-test.js'; |
||
| 82 | } |
||
| 83 | ); |
||
| 84 | |||
| 85 | $file_url = Assets::get_file_url_for_environment( 'test.min.js', 'test.js' ); |
||
| 86 | |||
| 87 | $this->assertContains( 'special-test.js', $file_url ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Possible values for test_get_file_url_for_environment. |
||
| 92 | */ |
||
| 93 | public function get_file_url_for_environment_data_provider() { |
||
| 94 | return array( |
||
| 95 | 'script-debug-true' => array( |
||
| 96 | '_inc/build/shortcodes/js/recipes.js', |
||
| 97 | 'modules/shortcodes/js/recipes.js', |
||
| 98 | true, |
||
| 99 | 'non_min_path', |
||
| 100 | 'min_path', |
||
| 101 | ), |
||
| 102 | 'script-debug-false' => array( |
||
| 103 | '_inc/build/shortcodes/js/recipes.js', |
||
| 104 | 'modules/shortcodes/js/recipes.js', |
||
| 105 | false, |
||
| 106 | 'min_path', |
||
| 107 | 'non_min_path', |
||
| 108 | ), |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Possible values for test_get_file_url_for_environment. |
||
| 114 | */ |
||
| 115 | public function get_file_url_for_environment_full_urls_data_provider() { |
||
| 116 | return array( |
||
| 117 | 'full_url' => array( 'https://jetpack.com/scripts/test.js' ), |
||
| 118 | 'protocol_relative' => array( '//jetpack.com/styles/test.css' ), |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Possible values for test_get_file_url_for_environment. |
||
| 124 | */ |
||
| 125 | public function get_file_url_for_environment_package_path_data_provider() { |
||
| 126 | $min_path = 'src/js/test.min.js'; |
||
| 127 | $non_min_path = 'src/js/test.js'; |
||
| 128 | $package_path = '/var/html/wp-content/plugins/jetpack/packages/test-package/test-package.php'; |
||
| 129 | |||
| 130 | return array( |
||
| 131 | 'script-debug-true' => array( |
||
| 132 | $min_path, |
||
| 133 | $non_min_path, |
||
| 134 | $package_path, |
||
| 135 | true, |
||
| 136 | 'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path, |
||
| 137 | 'wp-content/plugins/jetpack/packages/test-package/' . $min_path, |
||
| 138 | |||
| 139 | ), |
||
| 140 | 'script-debug-false' => array( |
||
| 141 | $min_path, |
||
| 142 | $non_min_path, |
||
| 143 | $package_path, |
||
| 144 | false, |
||
| 145 | 'wp-content/plugins/jetpack/packages/test-package/' . $min_path, |
||
| 146 | 'wp-content/plugins/jetpack/packages/test-package/' . $non_min_path, |
||
| 147 | ), |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Test that enqueue_async_script calls adds the script_loader_tag filter |
||
| 153 | */ |
||
| 154 | public function test_enqueue_async_script_adds_script_loader_tag_filter() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Test that enqueue_async_script calls wp_enqueue_script |
||
| 162 | */ |
||
| 163 | public function test_enqueue_async_script_calls_wp_enqueue_script() { |
||
| 164 | Assets::enqueue_async_script( 'test-handle', '/minpath.js', '/path.js', array(), '123', true ); |
||
| 165 | |||
| 166 | $this->assertTrue( wp_script_is( 'test-handle', 'enqueued' ) ); |
||
| 174 | } |
||
| 175 |
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: