Completed
Push — renovate/commander-5.x ( da542f...98aca7 )
by
unknown
169:19 queued 151:48
created

Files_Handler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 32.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 32
loc 98
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enqueue_package_file() 16 16 3
A create_filemap_path() 0 3 1
A set_file_paths() 16 16 5
A file_loader() 0 10 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
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class selects the package versions for the package files.
6
 */
7
class Files_Handler {
8
9
	/**
10
	 * The Plugins_Handler object.
11
	 *
12
	 * @var Plugins_Handler
13
	 */
14
	private $plugins_handler = null;
15
16
	/**
17
	 * The Version_Selector object.
18
	 *
19
	 * @var Version_Selector
20
	 */
21
	private $version_selector = null;
22
23
	/**
24
	 * The constructor.
25
	 *
26
	 * @param Plugins_Handler  $plugins_handler The Plugins_Handler object.
27
	 * @param Version_Selector $version_selector The Version_Selector object.
28
	 */
29
	public function __construct( $plugins_handler, $version_selector ) {
30
		$this->plugins_handler  = $plugins_handler;
31
		$this->version_selector = $version_selector;
32
	}
33
34
	/**
35
	 * Adds the version of a package file to the $jetpack_packages_filemap global
36
	 * array so that we can load the most recent version.
37
	 *
38
	 * @param string $file_identifier Unique id to file assigned by composer based on package name and filename.
39
	 * @param string $version Version of the file.
40
	 * @param string $path Absolute path to the file so that we can load it.
41
	 */
42 View Code Duplication
	public function enqueue_package_file( $file_identifier, $version, $path ) {
43
		global $jetpack_packages_filemap;
44
45
		if ( isset( $jetpack_packages_filemap[ $file_identifier ]['version'] ) ) {
46
			$selected_version = $jetpack_packages_filemap[ $file_identifier ]['version'];
47
		} else {
48
			$selected_version = null;
49
		}
50
51
		if ( $this->version_selector->is_version_update_required( $selected_version, $version ) ) {
52
			$jetpack_packages_filemap[ $file_identifier ] = array(
53
				'version' => $version,
54
				'path'    => $path,
55
			);
56
		}
57
	}
58
59
	/**
60
	 * Creates a path to the plugin's filemap. The filemap filename is the filename
61
	 * generated by Jetpack Autoloader version >= 2.0.
62
	 *
63
	 * @param String $plugin_path The plugin path.
64
	 *
65
	 * @return String The filemap path
66
	 */
67
	public function create_filemap_path( $plugin_path ) {
68
		return trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_filemap.php';
69
	}
70
71
	/**
72
	 *  Initializes the filemap.
73
	 */
74 View Code Duplication
	public function set_file_paths() {
75
		$active_plugin_paths = $this->plugins_handler->get_all_active_plugins_paths();
76
		$filemap_paths       = array_map( array( $this, 'create_filemap_path' ), $active_plugin_paths );
77
78
		foreach ( $filemap_paths as $path ) {
79
			if ( is_readable( $path ) ) {
80
				$file_map = require $path;
81
82
				if ( is_array( $file_map ) ) {
83
					foreach ( $file_map as $file_identifier => $file_data ) {
84
						$this->enqueue_package_file( $file_identifier, $file_data['version'], $file_data['path'] );
85
					}
86
				}
87
			}
88
		}
89
	}
90
91
	/**
92
	 * Include latest version of all enqueued files.
93
	 */
94
	public function file_loader() {
95
		global $jetpack_packages_filemap;
96
		foreach ( $jetpack_packages_filemap as $file_identifier => $file_data ) {
97
			if ( empty( $GLOBALS['__composer_autoload_files'][ $file_identifier ] ) ) {
98
				require_once $file_data['path'];
99
100
				$GLOBALS['__composer_autoload_files'][ $file_identifier ] = true;
101
			}
102
		}
103
	}
104
}
105