Completed
Push — master ( 0b917b...fc5a3e )
by Andrii
03:48
created

Constraint::merge()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 11
nc 5
nop 2
crap 6
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
use Composer\Semver\Comparator;
15
use Composer\Semver\Constraint\EmptyConstraint;
16
use Composer\Semver\VersionParser;
17
18
/**
19
 * Constraint helper class.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Constraint
24
{
25
    static private $parser;
26
27 1
    static public function getParser()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
28
    {
29 1
        if (static::$parser === null) {
0 ignored issues
show
Bug introduced by
Since $parser is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $parser to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
30 1
            static::$parser = new VersionParser();
0 ignored issues
show
Bug introduced by
Since $parser is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $parser to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
31 1
        }
32
33 1
        return static::$parser;
0 ignored issues
show
Bug introduced by
Since $parser is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $parser to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
    }
35
36 1
    static public function parse($constraint)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
    {
38 1
        return static::getParser()->parseConstraints($constraint);
39
    }
40
41
    /**
42
     * Merges two constraints.
43
     * Doesn't resolve version conflicts.
44
     * @param string $a
45
     * @param string $b
46
     * @return string
47
     */
48 1
    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...
49
    {
50 1
        $acon = static::parse($a);
51 1
        $bcon = static::parse($b);
52
53 1
        if ($acon instanceof EmptyConstraint) {
54 1
            return $b;
55 1
        } elseif ($bcon instanceof EmptyConstraint) {
56 1
            return $a;
57 1
        } elseif ($acon->matches($bcon) || $bcon->matches($acon)) {
58 1
            return strlen($a)>strlen($b) ? $b : $a;
59
        } else {
60 1
            return $a . ' ' . $b;
61
        }
62
    }
63
64 1
    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...
65
    {
66 1
        $versions = array_unique(array_values($versions));
67 1
        if (count($versions)<2) {
68
            return reset($versions);
69
        }
70 1
        $max = $versions[0];
71 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...
72 1
            $cur = $versions[$i];
73 1
            if (Comparator::compare($cur, '>', $max)) {
74 1
                $max = $cur;
75 1
            }
76 1
        }
77
78 1
        return trim($max);
79
    }
80
81
    /**
82
     * Is constraint disjunctive.
83
     * TODO redo after Semver will have such function.
84
     * @param string $constraint
85
     * @return bool
86
     */
87
    static public function isDisjunctive($constraint)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
88
    {
89
        return strpos($constraint, '|') !== false;
90
    }
91
92
}
93