Completed
Push — add/cli ( 74cf7f...2842da )
by
unknown
10:13
created

Version_Selector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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