Completed
Push — master ( ebe8f0...0b917b )
by Andrii
04:14
created

Constraint   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 58.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 91
ccs 23
cts 39
cp 0.5897
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 14 4
A isWeaker() 0 4 2
A startsWith() 0 3 2
A endsWith() 0 3 3
A isEmpty() 0 4 3
A findMax() 0 19 4
A toNum() 0 10 4
1
<?php
2
3
/*
4
 * Composer plugin for bower/npm assets
5
 *
6
 * @link      https://github.com/hiqdev/composer-asset-plugin
7
 * @package   composer-asset-plugin
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\composerassetplugin;
13
14
/**
15
 * Constraint helper class.
16
 *
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Constraint
20
{
21
    /**
22
     * Merges two constraints.
23
     * Doesn't resolve version conflicts.
24
     * @param $a
25
     * @param $b
26
     * @return string
27
     */
28
    static public function merge($a, $b)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
29
    {
30
        $a = trim($a);
31
        $b = trim($b);
32
        var_dump("a:$a b:$b");
0 ignored issues
show
Security Debugging Code introduced by
var_dump("a:{$a} b:{$b}"); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
33
34
        if ($a === $b || static::isWeaker($b, $a)) {
35
            return $a;
36
        } elseif (static::isWeaker($a, $b)) {
37
            return $b;
38
        } else {
39
            return $a . ' ' . $b;
40
        }
41
    }
42
43
    /**
44
     * Check if $a is weaker condition then $b, like:
45
     * - a="*"         b="2.2"
46
     * - a="2.2 | 3.3" b="2.2"
47
     * - a="1.1 | 2.2" b="2.2"
48
     * Possible optimization.
49
     * // TODO Rename and implement.
50
     * @param string $a
51
     * @param string $b
52
     * @return boolean
53
     */
54
    static public function isWeaker($a, $b)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
55
    {
56
        return static::isEmpty($a) || static::startsWith($a, $b . ' |') | static::endsWith($a, '| ' . $b);
57
    }
58
59
    static public function startsWith($haystack, $needle) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
60
        return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
61
    }
62
63
    static public function endsWith($haystack, $needle) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
64
        return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
65
    }
66
67
    /**
68
     * Checks whether the $version represents any possible version.
69
     *
70
     * @param string $version
71
     * @return boolean
72
     */
73
    static public function isEmpty($version)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
74
    {
75 1
        return $version === '' || $version === '*' || $version === '>=0.0.0';
76
    }
77 1
78
    static public function findMax(array $versions)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
79
    {
80 1
        $versions = array_unique(array_values($versions));
81
        if (count($versions)<2) {
82 1
            return reset($versions);
83 1
        }
84
        $max = $versions[0];
85
        $maxNum = static::toNum($max);
86 1
        for ($i=1; $i<= count($versions); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
87 1
            $cur = $versions[$i];
88 1
            $curNum = static::toNum($cur);
89 1
            if ($curNum > $maxNum) {
90 1
                $max = $cur;
91 1
                $maxNum = $curNum;
92 1
            }
93 1
        }
94 1
95 1
        return $max;
96
    }
97 1
98
    static public function toNum($version)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
99
    {
100 2
        $version = preg_replace('/[^0-9\.]/', '', $version);
101
        $nums = explode('.', $version);
102 2
        $n1 = isset($nums[0]) ? $nums[0] : 0;
103 2
        $n2 = isset($nums[1]) ? $nums[1] : 0;
104 2
        $n3 = isset($nums[2]) ? $nums[2] : 0;
105 2
106 2
        return (($n1*1000) + $n2)*1000 + $n3;
107
    }
108 2
109
}
110