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

Autoloader_Locator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 74
loc 74
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 3 1
A find_latest_autoloader() 15 15 3
A get_autoloader_path() 3 3 1
A get_autoloader_version() 13 13 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
use \Automattic\Jetpack\Autoloader\AutoloadGenerator;
13
14
/**
15
 * This class locates autoloaders.
16
 */
17 View Code Duplication
class Autoloader_Locator {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
19
	/**
20
	 * The object for comparing autoloader versions.
21
	 *
22
	 * @var Version_Selector
23
	 */
24
	private $version_selector;
25
26
	/**
27
	 * The constructor.
28
	 *
29
	 * @param Version_Selector $version_selector The version selector object.
30
	 */
31
	public function __construct( $version_selector ) {
32
		$this->version_selector = $version_selector;
33
	}
34
35
	/**
36
	 * Finds the path to the plugin with the latest autoloader.
37
	 *
38
	 * @param array  $plugin_paths An array of plugin paths.
39
	 * @param string $latest_version The latest version reference.
40
	 *
41
	 * @return string|null
42
	 */
43
	public function find_latest_autoloader( $plugin_paths, &$latest_version ) {
44
		$latest_plugin = null;
45
46
		foreach ( $plugin_paths as $plugin_path ) {
47
			$version = $this->get_autoloader_version( $plugin_path );
48
			if ( ! $this->version_selector->is_version_update_required( $latest_version, $version ) ) {
49
				continue;
50
			}
51
52
			$latest_version = $version;
53
			$latest_plugin  = $plugin_path;
54
		}
55
56
		return $latest_plugin;
57
	}
58
59
	/**
60
	 * Gets the path to the autoloader.
61
	 *
62
	 * @param string $plugin_path The path to the plugin.
63
	 *
64
	 * @return string
65
	 */
66
	public function get_autoloader_path( $plugin_path ) {
67
		return trailingslashit( $plugin_path ) . 'vendor/autoload_packages.php';
68
	}
69
70
	/**
71
	 * Gets the version for the autoloader.
72
	 *
73
	 * @param string $plugin_path The path to the plugin.
74
	 *
75
	 * @return string|null
76
	 */
77
	public function get_autoloader_version( $plugin_path ) {
78
		$classmap = trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php';
79
		if ( ! file_exists( $classmap ) ) {
80
			return null;
81
		}
82
83
		$classmap = require $classmap;
84
		if ( isset( $classmap[ AutoloadGenerator::class ] ) ) {
85
			return $classmap[ AutoloadGenerator::class ]['version'];
86
		}
87
88
		return null;
89
	}
90
}
91