Version::compare()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 5
eloc 14
nc 12
nop 2
1
<?php
2
3
namespace kujaff\VersionsBundle\Service;
4
5
use Symfony\Component\DependencyInjection\ContainerAware;
6
use kujaff\VersionsBundle\Entity\Version as VersionEntity;
7
8
/**
9
 * Service for BundleVersions
10
 */
11
class Version extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated with message: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
12
{
13
14
    /**
15
     * Return -1 if $version1 < $version2, 0 if $version1 = $version2, +1 if $version1 > $version2
16
     *
17
     * @param mixed $version1 Can be a string (X.Y.Z) or a Version instance
18
     * @param mixed $version2 Can be a string (X.Y.Z) or a Version instance
19
     */
20
    public function compare($version1, $version2)
21
    {
22
        if (is_string($version1)) {
23
            $version1 = new VersionEntity($version1);
24
        }
25
        if (is_string($version2)) {
26
            $version2 = new VersionEntity($version2);
27
        }
28
29
        $lengthMajor = max(strlen($version1->getMajor()), strlen($version2->getMajor()));
30
        $lengthMinor = max(strlen($version1->getMinor()), strlen($version2->getMinor()));
31
        $lengthPatch = max(strlen($version1->getPatch()), strlen($version2->getPatch()));
32
        $version1Number = sprintf('%0' . $lengthMajor . 's', $version1->getMajor()) . sprintf('%0' . $lengthMinor . 's', $version1->getMinor()) . sprintf('%0' . $lengthPatch . 's', $version1->getPatch());
33
        $version2Number = sprintf('%0' . $lengthMajor . 's', $version2->getMajor()) . sprintf('%0' . $lengthMinor . 's', $version2->getMinor()) . sprintf('%0' . $lengthPatch . 's', $version2->getPatch());
34
35
        if ($version1Number == $version2Number) {
36
            return 0;
37
        } else {
38
            return ($version1Number > $version2Number) ? 1 : -1;
39
        }
40
    }
41
}
42