Version::check()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace App;
4
5
/**
6
 * Version class.
7
 *
8
 * @package App
9
 *
10
 * @copyright YetiForce S.A.
11
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
12
 * @author    Mariusz Krzaczkowski <[email protected]>
13
 */
14
class Version
15
{
16
	private static $versions = false;
17
18
	/**
19
	 * Get current version of system.
20
	 *
21
	 * @param string $type
22
	 *
23 11
	 * @return string
24
	 */
25 11
	public static function get($type = 'appVersion'): string
26
	{
27 11
		static::init();
28
		return static::$versions[$type];
0 ignored issues
show
Bug introduced by
Since $versions is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $versions to at least protected.
Loading history...
29
	}
30
31
	/**
32
	 * Get current short version of system.
33 12
	 *
34
	 * @param string $type
35 12
	 *
36
	 * @return string
37
	 */
38 12
	public static function getShort($type = 'appVersion'): string
39
	{
40
		$fullVer = \explode('.', self::get($type));
41
		array_pop($fullVer);
42
		return \implode('.', $fullVer);
43
	}
44
45
	/**
46
	 * Function to load versions.
47
	 */
48
	private static function init(): void
49 4
	{
50
		if (false === static::$versions) {
0 ignored issues
show
Bug introduced by
Since $versions is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $versions to at least protected.
Loading history...
51 4
			static::$versions = require 'config/version.php';
52 4
		}
53
	}
54
55
	/**
56
	 * Check app versions with given version.
57
	 *
58
	 * @param string $version   - String Version against which comparision to be done
59
	 * @param string $type
60
	 * @param string $condition - String Condition like ( '=', '!=', '<', '<=', '>', '>=')
61
	 *
62
	 * @return bool|int
63
	 */
64 9
	public static function check($version, $type = 'appVersion', $condition = '>=')
65
	{
66 9
		static::init();
67 2
		return static::compare($version, static::$versions[$type], $condition);
0 ignored issues
show
Bug introduced by
Since $versions is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $versions to at least protected.
Loading history...
68 2
	}
69 2
70 2
	/**
71 2
	 * Compares two version number strings.
72
	 *
73 9
	 * @param string $v1
74
	 * @param string $v2
75
	 * @param string $operator
76
	 *
77
	 * @return mixed
78
	 */
79
	public static function compare($v1, $v2, $operator = '==')
80
	{
81
		if ('x' === substr($v2, -1)) {
82
			$ev2 = \explode('.', $v2);
83
			\array_pop($ev2);
84
			$lv2 = \count($ev2);
85
			$v2 = \implode('.', $ev2);
86
			$v1 = \implode('.', \array_slice(\explode('.', $v1), 0, $lv2));
87
		}
88
		return version_compare($v1, $v2, $operator);
89
	}
90
}
91