Completed
Push — master ( d9d69e...fc895f )
by Kenji
14s queued 11s
created

PhpParserVersion::isComposerInstalled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2021 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
namespace Kenjis\MonkeyPatch;
12
13
use Composer\InstalledVersions;
14
use OutOfBoundsException;
15
16
class PhpParserVersion
17
{
18
	/**
19
	 * @var string|null
20
	 */
21
	private $version;
22
23
	public function __construct()
24
	{
25
		try {
26
			$this->version = InstalledVersions::getVersion('nikic/php-parser');
27
		} catch (OutOfBoundsException $e) {
28
			$this->version = null;
29
		}
30
	}
31
32
	public function isComposerInstalled()
33
	{
34
		if ($this->version === null) {
35
			return false;
36
		}
37
38
		return true;
39
	}
40
41
	/**
42
	 * @return string|null
43
	 */
44
	public function getVersion()
45
	{
46
		return $this->version;
47
	}
48
49
	/**
50
	 * @param string $version eg. '4.5'
51
	 */
52
	public function isGreaterThanOrEqualTo($version)
53
	{
54
		$parts = explode('.', $version);
55
		$major = (int) $parts[0];
56
		$minor = (int) $parts[1];
57
58
		$parts = explode('.', $this->version);
59
		$major_installed = (int) $parts[0];
60
		$minor_installed = (int) $parts[1];
61
62
		if ($major_installed < $major) {
63
			return false;
64
		}
65
66
		if ($major_installed > $major) {
67
			return true;
68
		}
69
70
		if ($minor_installed >= $minor) {
71
			return true;
72
		}
73
74
		return false;
75
	}
76
}
77