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

Version_Selector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 7
loc 55
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B is_version_update_required() 0 25 8
A is_package_version_dev() 7 7 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
 * Used to select package versions.
14
 */
15
class Version_Selector {
16
17
	/**
18
	 * Checks whether the selected package version should be updated. Composer development
19
	 * package versions ('9999999-dev' or versions that start with 'dev-') are favored
20
	 * when the JETPACK_AUTOLOAD_DEV constant is set to true.
21
	 *
22
	 * @param String $selected_version The currently selected package version.
23
	 * @param String $compare_version The package version that is being evaluated to
24
	 *                                determine if the version needs to be updated.
25
	 *
26
	 * @return Boolean Returns true if the selected package version should be updated,
27
	 *                 else false.
28
	 */
29
	public function is_version_update_required( $selected_version, $compare_version ) {
30
		$use_dev_versions = defined( 'JETPACK_AUTOLOAD_DEV' ) && JETPACK_AUTOLOAD_DEV;
31
32
		if ( is_null( $selected_version ) ) {
33
			return true;
34
		}
35
36
		if ( $use_dev_versions && $this->is_package_version_dev( $selected_version ) ) {
37
			return false;
38
		}
39
40
		if ( $this->is_package_version_dev( $compare_version ) ) {
41
			if ( $use_dev_versions ) {
42
				return true;
43
			} else {
44
				return false;
45
			}
46
		}
47
48
		if ( version_compare( $selected_version, $compare_version, '<' ) ) {
49
			return true;
50
		}
51
52
		return false;
53
	}
54
55
	/**
56
	 * Checks whether the given package version is a development version.
57
	 *
58
	 * @param String $version The package version.
59
	 *
60
	 * @return Boolean True if the version is a dev version, else false.
61
	 */
62 View Code Duplication
	private function is_package_version_dev( $version ) {
63
		if ( 'dev-' === substr( $version, 0, 4 ) || '9999999-dev' === $version ) {
64
			return true;
65
		}
66
67
		return false;
68
	}
69
}
70