Version   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B compare() 0 21 5
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