Updater   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 4
b 0
f 0
dl 0
loc 75
ccs 0
cts 25
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStability() 0 9 2
A newVersionAvailable() 0 33 4
A parseOffset() 0 7 2
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Updater;
12
13
14
use Humbug\SelfUpdate\Updater as BaseUpdater;
15
use Humbug\SelfUpdate\VersionParser;
16
17
class Updater extends BaseUpdater
18
{
19
20
	/**
21
	 * Detects if new versions are available.
22
	 *
23
	 * @return boolean
24
	 */
25
	protected function newVersionAvailable()
26
	{
27
		$old_version = $this->strategy->getCurrentLocalVersion($this);
28
29
		// Cloned Git repository is considered the latest version.
30
		if ( trim($old_version, '@') === 'git-version' ) {
31
			// Have git version placeholder in obscure form to avoid Box replacing it.
32
			return false;
33
		}
34
35
		$this->oldVersion = $old_version;
36
		$this->newVersion = $this->strategy->getCurrentRemoteVersion($this);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->strategy->getCurrentRemoteVersion($this) can also be of type boolean. However, the property $newVersion is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37
38
		list ($new_stability, $new_version) = $this->parseStability($this->newVersion);
39
		list ($old_stability, $old_version) = $this->parseStability($this->oldVersion);
40
41
		// Release with different stability a newer one.
42
		if ( $new_stability !== $old_stability ) {
43
			return true;
44
		}
45
46
		list ($new_version, $new_offset) = $this->parseOffset($new_version);
47
		list ($old_version, $old_offset) = $this->parseOffset($old_version);
48
49
		// Release made with larger offset is a newer one.
50
		if ( $new_version === $old_version ) {
51
			return $new_offset > $old_offset;
52
		}
53
54
		// Just see which version is larger.
55
		$version_parser = new VersionParser(array($new_version, $old_version));
56
57
		return $version_parser->getMostRecentAll() === $new_version;
58
	}
59
60
	/**
61
	 * Returns version parsed into stability and actual version.
62
	 *
63
	 * @param string $version Version.
64
	 *
65
	 * @return array
66
	 */
67
	protected function parseStability($version)
68
	{
69
		$parts = explode(':', $version);
70
71
		if ( count($parts) === 1 ) {
72
			return array(Stability::STABLE, $parts[0]);
73
		}
74
75
		return $parts;
76
	}
77
78
	/**
79
	 * Returns version parsed into tag and commit count after that tag.
80
	 *
81
	 * @param string $version Version.
82
	 *
83
	 * @return array
84
	 */
85
	protected function parseOffset($version)
86
	{
87
		if ( preg_match('/^(.*)-([\d]+)-g.{7}$/', $version, $regs) ) {
88
			return array($regs[1], $regs[2]);
89
		};
90
91
		return array($version, 0);
92
	}
93
94
}
95