Failed Conditions
Push — master ( 674943...308042 )
by Alexander
03:21
created

Updater::newVersionAvailable()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 0
cts 17
cp 0
rs 8.8571
cc 3
eloc 13
nc 3
nop 0
crap 12
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
		$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...
28
		$this->oldVersion = $this->strategy->getCurrentLocalVersion($this);
29
30
		list ($new_stability, $new_version) = $this->parseStability($this->newVersion);
0 ignored issues
show
Bug introduced by
It seems like $this->newVersion can also be of type boolean; however, ConsoleHelpers\SVNBuddy\...dater::parseStability() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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