Completed
Pull Request — master (#2)
by Julien
02:16
created

PreReleaseComparator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 15.66 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 13
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 17 4
B compareIdentifiers() 13 18 6
A compareIdentifier() 0 15 4
A isInt() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace SemVer\SemVer;
5
6
final class PreReleaseComparator
7
{
8
    /**
9
     * @param Version $version1
10
     * @param Version $version2
11
     *
12
     * @return int
13
     */
14
    public static function compare(Version $version1, Version $version2) : int
15
    {
16
        $preRelease1 = $version1->getPreRelease();
17
        $preRelease2 = $version2->getPreRelease();
18
19
        $leftPreReleaseIsEmpty = '' === $preRelease1;
20
        $rightPreReleaseIsEmpty = '' === $preRelease2;
21
        if ($rightPreReleaseIsEmpty !== $leftPreReleaseIsEmpty) {
22
            return $leftPreReleaseIsEmpty ? 1 : -1;
23
        }
24
25
        if ($leftPreReleaseIsEmpty) {
26
            return 0;
27
        }
28
29
        return self::compareIdentifiers(explode('.', $preRelease1), explode('.', $preRelease2));
30
    }
31
32
    /**
33
     * @param array $preReleaseIdentifiers1
34
     * @param array $preReleaseIdentifiers2
35
     *
36
     * @return int
37
     */
38
    private static function compareIdentifiers(array $preReleaseIdentifiers1, array $preReleaseIdentifiers2) : int
39
    {
40 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
            $preReleasePart1 = array_shift($preReleaseIdentifiers1);
42
            $preReleasePart2 = array_shift($preReleaseIdentifiers2);
43
            if (null === $preReleasePart1) {
44
                return -1;
45
            }
46
47
            if (null === $preReleasePart2) {
48
                return 1;
49
            }
50
51
            $compare = self::compareIdentifier($preReleasePart1, $preReleasePart2);
52
        } while (0 === $compare && (count($preReleaseIdentifiers1) || count($preReleaseIdentifiers2)));
53
54
        return $compare;
55
    }
56
57
    /**
58
     * @param string $preReleasePart1
59
     * @param string $preReleasePart2
60
     *
61
     * @return int
62
     */
63
    private static function compareIdentifier(string $preReleasePart1, string $preReleasePart2) : int
64
    {
65
        $mineIsInt = self::isInt($preReleasePart1);
66
        $theirIsInt = self::isInt($preReleasePart2);
67
68
        if ($mineIsInt !== $theirIsInt) {
69
            return $mineIsInt ? -1 : 1;
70
        }
71
72
        if ($mineIsInt) {
73
            return ((int)$preReleasePart1) <=> ((int)$preReleasePart2);
74
        }
75
76
        return $preReleasePart1 <=> $preReleasePart2;
77
    }
78
79
    /**
80
     * @param string $identifier
81
     *
82
     * @return bool
83
     */
84
    private static function isInt(string $identifier)
85
    {
86
        return ctype_digit($identifier) && strpos($identifier, '00') !== 0;
87
    }
88
}
89