VersionFormatter::determineVersionValue()   C
last analyzed

Complexity

Conditions 12
Paths 16

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 36
cp 0
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 29
nc 16
nop 1
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Packy.
5
 *
6
 * (c) Peter Nijssen
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AppBundle\Service;
13
14
class VersionFormatter
15
{
16
    /**
17
     * Normalize vendor version.
18
     *
19
     * @param string $rawVersion
20
     *
21
     * @return string $version
22
     */
23
    public function normalizeVersion($rawVersion)
24
    {
25
        $normalizedVersion = '0.0.0';
26
        if (mb_strpos($rawVersion, ',') !== false) {
27
            $versionsRange = explode(',', $rawVersion);
28
            foreach ($versionsRange as $versionRange) {
29
                $nowVersion = $this->determineVersionValue($versionRange);
30
                if (version_compare($normalizedVersion, $nowVersion) < 0) {
31
                    $normalizedVersion = $nowVersion;
32
                }
33
            }
34
        } else {
35
            $normalizedVersion = $this->determineVersionValue($rawVersion);
36
        }
37
38
        // Remove any characters which don't belong in an actual version number
39
        $normalizedVersion = preg_replace('/[^0-9.]/', '', $normalizedVersion);
40
41
        return $normalizedVersion;
42
    }
43
44
    /**
45
     * Determine version value and handle wildcards and comparison operator.
46
     *
47
     * @param string $rawVersion
48
     *
49
     * @return string $version
50
     */
51
    private function determineVersionValue($rawVersion)
52
    {
53
        $version = str_replace(['*', ' '], ['999', ''], $rawVersion);
54
        if (preg_match('/^([\~\>\<\=\!]+)([0-9\.]+)$/', $version, $m) && count($m) == 3) {
55
            $operator = $m[1];
56
            $version = $m[2];
57
            $versionAnnotations = explode('.', $version);
58
            if (count($versionAnnotations) == 3) {
59
                list($major, $minor, $patch) = $versionAnnotations;
60
            } else {
61
                switch (count($versionAnnotations)) {
62
                    case 2:
63
                        list($major, $minor) = $versionAnnotations;
64
                        $patch = 999;
65
                        break;
66
67
                    default:
68
                        $major = $versionAnnotations;
69
                        $minor = 999;
70
                        $patch = 999;
71
                        break;
72
                }
73
            }
74
            if (mb_strpos($operator, '>') !== false || mb_strpos($operator, '!') !== false || mb_strpos($operator, '~') !== false) {
75
                $version = $major . '.999.999';
76
            } elseif (mb_strpos($operator, '<') !== false) {
77
                if ($major == 0 && $minor > 0) {
78
                    $version = $major . '.' . (((int) $minor) - 1) . '.999';
79
                } elseif ($patch == 0) {
80
                    $version = (((int) $major) - 1) . '.999.999';
81
                } else {
82
                    $version = $major . '.0.0';
83
                }
84
            }
85
        }
86
87
        return $version;
88
    }
89
}
90