Completed
Push — update/node-12191 ( 8875a2...f69c39 )
by
unknown
147:00 queued 137:34
created

Test_Version_Loading_From_Manifests   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 41.76 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 38
loc 91
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_classmap() 19 19 1
A test_psr4() 19 19 1
A test_filemap() 0 20 1
A set_up() 0 3 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 Test_Version_Loading_From_Manifests extends TestCase {
16
17
	/**
18
	 * A manifest handler.
19
	 *
20
	 * @var Manifest_Reader
21
	 */
22
	private $manifest_handler;
23
24
	/**
25
	 * Setup runs before each test.
26
	 *
27
	 * @before
28
	 */
29
	public function set_up() {
30
		$this->manifest_handler = new Manifest_Reader( new Version_Selector() );
31
	}
32
33
	/**
34
	 * Tests that the classmap manifest from a single plugin can be handled correctly.
35
	 */
36 View Code Duplication
	public function test_classmap() {
37
		$path_map = array();
38
		$this->manifest_handler->read_manifests(
39
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
40
			'vendor/composer/jetpack_autoload_classmap.php',
41
			$path_map
42
		);
43
44
		$loader = new Version_Loader(
45
			new Version_Selector(),
46
			$path_map,
47
			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...
48
			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...
49
		);
50
51
		$file = $loader->find_class_file( Test::class );
52
53
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/includes/class-test.php', $file );
54
	}
55
56
	/**
57
	 * Tests that the PSR-4 manifest from a single plugin can be handled correctly.
58
	 */
59 View Code Duplication
	public function test_psr4() {
60
		$path_map = array();
61
		$this->manifest_handler->read_manifests(
62
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
63
			'vendor/composer/jetpack_autoload_psr4.php',
64
			$path_map
65
		);
66
67
		$loader = new Version_Loader(
68
			new Version_Selector(),
69
			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...
70
			$path_map,
71
			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...
72
		);
73
74
		$file = $loader->find_class_file( Psr4Test::class );
75
76
		$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/src/Psr4/Test.php', $file );
77
	}
78
79
	/**
80
	 * Tests that the filemap manifest from a single plugin can be handled correctly.
81
	 *
82
	 * @preserveGlobalState disabled
83
	 * @runInSeparateProcess
84
	 */
85
	public function test_filemap() {
86
		$path_map = array();
87
		$this->manifest_handler->read_manifests(
88
			array( TEST_DATA_PATH . '/plugins/dummy_current' ),
89
			'vendor/composer/jetpack_autoload_filemap.php',
90
			$path_map
91
		);
92
93
		$loader = new Version_Loader(
94
			new Version_Selector(),
95
			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...
96
			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...
97
			$path_map
98
		);
99
100
		$loader->load_filemap();
101
102
		$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] );
103
		$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) );
104
	}
105
}
106