Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

Manifest_Handler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 45.16 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 42
loc 93
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register_plugin_manifests() 14 14 2
A register_manifest() 14 14 4
A register_record() 14 14 3

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
2
/**
3
 * This file was automatically generated by automattic/jetpack-autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
namespace Automattic\Jetpack\Autoloader\jp95016e8b7af5cfd6a3cbf90d4433a769;
9
10
 // phpcs:ignore
11
12
/**
13
 * This class selects the most recent version of a registered path.
14
 */
15
class Manifest_Handler {
16
17
	/**
18
	 * An array of the active plugin paths we want to search.
19
	 *
20
	 * @var array
21
	 */
22
	private $active_plugin_paths;
23
24
	/**
25
	 * The Version_Selector object.
26
	 *
27
	 * @var Version_Selector
28
	 */
29
	private $version_selector;
30
31
	/**
32
	 * The constructor.
33
	 *
34
	 * @param array            $active_plugin_paths An array of the active plugin paths we want to search.
35
	 * @param Version_Selector $version_selector The Version_Selector object.
36
	 */
37
	public function __construct( $active_plugin_paths, $version_selector ) {
38
		$this->active_plugin_paths = $active_plugin_paths;
39
		$this->version_selector    = $version_selector;
40
	}
41
42
	/**
43
	 * Registers all of the paths in a given manifest.
44
	 *
45
	 * @param string $manifest_path The path that we're loading the manifest from in each plugin.
46
	 * @param array  $path_map The path map to add the contents of the manifests to.
47
	 *
48
	 * @return array $path_map The path map we've built using the manifests in each plugin.
49
	 */
50 View Code Duplication
	public function register_plugin_manifests( $manifest_path, &$path_map ) {
51
		$file_paths = array_map(
52
			function ( $path ) use ( $manifest_path ) {
53
				return trailingslashit( $path ) . $manifest_path;
54
			},
55
			$this->active_plugin_paths
56
		);
57
58
		foreach ( $file_paths as $path ) {
59
			$this->register_manifest( $path, $path_map );
60
		}
61
62
		return $path_map;
63
	}
64
65
	/**
66
	 * Registers a plugin's manifest file with the path map.
67
	 *
68
	 * @param string $manifest_path The absolute path to the manifest that we're loading.
69
	 * @param array  $path_map The path map to add the contents of the manifest to.
70
	 */
71 View Code Duplication
	protected function register_manifest( $manifest_path, &$path_map ) {
72
		if ( ! is_readable( $manifest_path ) ) {
73
			return;
74
		}
75
76
		$manifest = require $manifest_path;
77
		if ( ! is_array( $manifest ) ) {
78
			return;
79
		}
80
81
		foreach ( $manifest as $key => $data ) {
82
			$this->register_record( $key, $data, $path_map );
83
		}
84
	}
85
86
	/**
87
	 * Registers an entry from the manifest in the path map.
88
	 *
89
	 * @param string $key The identifier for the entry we're registering.
90
	 * @param array  $data The data for the entry we're registering.
91
	 * @param array  $path_map The path map to add the contents of the manifest to.
92
	 */
93 View Code Duplication
	protected function register_record( $key, $data, &$path_map ) {
94
		if ( isset( $path_map[ $key ]['version'] ) ) {
95
			$selected_version = $path_map[ $key ]['version'];
96
		} else {
97
			$selected_version = null;
98
		}
99
100
		if ( $this->version_selector->is_version_update_required( $selected_version, $data['version'] ) ) {
101
			$path_map[ $key ] = array(
102
				'version' => $data['version'],
103
				'path'    => $data['path'],
104
			);
105
		}
106
	}
107
}
108