Completed
Push — renovate/mocha-8.x ( 07030e...e8e64c )
by
unknown
28:17 queued 19:09
created

WP_Test_Integration_Loader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 177
Duplicated Lines 62.15 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 110
loc 177
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A test_single_plugin_classmap() 18 18 1
A test_multiple_plugin_classmap() 18 18 1
A test_single_plugin_psr4() 18 18 1
A test_multiple_plugin_psr4() 18 18 1
A test_single_plugin_filemap() 19 19 1
A test_multiple_plugin_filemap() 19 19 1

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Integration test suite for the loader population.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use Jetpack\AutoloaderTestData\Plugin\Psr4\Test as Psr4Test;
9
use Jetpack\AutoloaderTestData\Plugin\Test as Test;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Test.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Test suite class for verifying that parsed manifests can be put into the loader and used.
14
 */
15
class WP_Test_Integration_Loader extends TestCase {
16
17
	/**
18
	 * A manifest handler configured for a single plugin.
19
	 *
20
	 * @var Manifest_Handler
21
	 */
22
	private $single_manifest_handler;
23
24
	/**
25
	 * A manifest handler configured for multiple plugins.
26
	 *
27
	 * @var Manifest_Handler
28
	 */
29
	private $multiple_manifest_handler;
30
31
	/**
32
	 * Setup runs before each test.
33
	 */
34
	public function setUp() {
35
		parent::setUp();
36
37
		$this->single_manifest_handler   = new Manifest_Handler(
38
			array(
39
				TEST_DATA_PATH . '/plugins/plugin_current',
40
			),
41
			new Version_Selector()
42
		);
43
		$this->multiple_manifest_handler = new Manifest_Handler(
44
			array(
45
				TEST_DATA_PATH . '/plugins/plugin_current',
46
				TEST_DATA_PATH . '/plugins/plugin_newer',
47
			),
48
			new Version_Selector()
49
		);
50
	}
51
52
	/**
53
	 * Tests that the classmap manifest from a single plugin can be handled correctly.
54
	 */
55 View Code Duplication
	public function test_single_plugin_classmap() {
56
		$path_map = array();
57
		$this->single_manifest_handler->register_plugin_manifests(
58
			'vendor/composer/jetpack_autoload_classmap.php',
59
			$path_map
60
		);
61
62
		$loader = new Version_Loader(
63
			new Version_Selector(),
64
			$path_map,
65
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
66
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
67
		);
68
69
		$file = $loader->find_class_file( Test::class );
70
71
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php', $file );
72
	}
73
74
	/**
75
	 * Tests that the classmap manifest from multiple plugins can be handled correctly.
76
	 */
77 View Code Duplication
	public function test_multiple_plugin_classmap() {
78
		$path_map = array();
79
		$this->multiple_manifest_handler->register_plugin_manifests(
80
			'vendor/composer/jetpack_autoload_classmap.php',
81
			$path_map
82
		);
83
84
		$loader = new Version_Loader(
85
			new Version_Selector(),
86
			$path_map,
87
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
88
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
89
		);
90
91
		$file = $loader->find_class_file( Test::class );
92
93
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/includes/class-test.php', $file );
94
	}
95
96
	/**
97
	 * Tests that the PSR-4 manifest from a single plugin can be handled correctly.
98
	 */
99 View Code Duplication
	public function test_single_plugin_psr4() {
100
		$path_map = array();
101
		$this->single_manifest_handler->register_plugin_manifests(
102
			'vendor/composer/jetpack_autoload_psr4.php',
103
			$path_map
104
		);
105
106
		$loader = new Version_Loader(
107
			new Version_Selector(),
108
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
109
			$path_map,
110
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
111
		);
112
113
		$file = $loader->find_class_file( Psr4Test::class );
114
115
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/src/Psr4/Test.php', $file );
116
	}
117
118
	/**
119
	 * Tests that the PSR-4 manifest from multiple plugins can be handled correctly.
120
	 */
121 View Code Duplication
	public function test_multiple_plugin_psr4() {
122
		$path_map = array();
123
		$this->multiple_manifest_handler->register_plugin_manifests(
124
			'vendor/composer/jetpack_autoload_psr4.php',
125
			$path_map
126
		);
127
128
		$loader = new Version_Loader(
129
			new Version_Selector(),
130
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
131
			$path_map,
132
			null
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
133
		);
134
135
		$file = $loader->find_class_file( Psr4Test::class );
136
137
		$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/src/Psr4/Test.php', $file );
138
	}
139
140
	/**
141
	 * Tests that the filemap manifest from a single plugin can be handled correctly.
142
	 *
143
	 * @preserveGlobalState disabled
144
	 * @runInSeparateProcess
145
	 */
146 View Code Duplication
	public function test_single_plugin_filemap() {
147
		$path_map = array();
148
		$this->single_manifest_handler->register_plugin_manifests(
149
			'vendor/composer/jetpack_autoload_filemap.php',
150
			$path_map
151
		);
152
153
		$loader = new Version_Loader(
154
			new Version_Selector(),
155
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
156
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
157
			$path_map
158
		);
159
160
		$loader->load_filemap();
161
162
		$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] );
163
		$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) );
164
	}
165
166
	/**
167
	 * Tests that the filemap manifest from multiple plugins can be handled correctly.
168
	 *
169
	 * @preserveGlobalState disabled
170
	 * @runInSeparateProcess
171
	 */
172 View Code Duplication
	public function test_multiple_plugin_filemap() {
173
		$path_map = array();
174
		$this->multiple_manifest_handler->register_plugin_manifests(
175
			'vendor/composer/jetpack_autoload_filemap.php',
176
			$path_map
177
		);
178
179
		$loader = new Version_Loader(
180
			new Version_Selector(),
181
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
182
			null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
183
			$path_map
184
		);
185
186
		$loader->load_filemap();
187
188
		$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] );
189
		$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginNewer\\if_i_exist_then_this_test_passed' ) );
190
	}
191
}
192