Completed
Push — master ( e87eab...888d29 )
by Sebastian
03:03
created

Number::roman2Dec()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 10
nop 1
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is a part of HDS (HeBIS Discovery System). HDS is an 
4
 * extension of the open source library search engine VuFind, that 
5
 * allows users to search and browse beyond resources. More 
6
 * Information about VuFind you will find on http://www.vufind.org
7
 * 
8
 * Copyright (C) 2016 
9
 * HeBIS Verbundzentrale des HeBIS-Verbundes 
10
 * Goethe-Universität Frankfurt / Goethe University of Frankfurt
11
 * http://www.hebis.de
12
 * 
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
 */
27
28
namespace Seboettg\CiteProc\Util;
29
30
31
/**
32
 * Class Number
33
 * @package Seboettg\CiteProc\Util
34
 *
35
 * @author Sebastian Böttger <[email protected]>
36
 */
37
class Number
38
{
39
40
    const PATTERN_ORDINAL = "/\d+(st|nd|rd|th)?\.?$/";
41
42
    const PATTERN_ROMAN = "/^[ivxlcdm]+\.?$/i";
43
44
    const PATTERN_AFFIXES = "/^[a-z]?\d+[a-z]?$/i";
45
46
    const PATTERN_COMMA_AMPERSAND_RANGE = "/\d*([\s?\-&+,;\s])+\d+/";
47
48
    const ROMAN_NUMERALS = [
49
        ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"],
50
        ["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"],
51
        ["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"],
52
        ["", "m", "mm", "mmm", "mmmm", "mmmmm"]
53
    ];
54
55
    /**
56
     * @return \Closure
57
     */
58
    public static function getCompareNumber()
59
    {
60
        return function ($numA, $numB, $order) {
61
            if (is_numeric($numA) && is_numeric($numB)) {
62
                $ret = $numA - $numB;
63
            } else {
64
                $ret = strcmp($numA, $numB);
65
            }
66
            if ("descending" === $order) {
67
                return $ret > 0 ? -1 : 1;
68
            }
69
            return $ret > 0 ? 1 : -1;
70
        };
71
    }
72
73
    /**
74
     * @param $num
75
     * @return string
76
     */
77
    public static function dec2roman($num)
78
    {
79
        $ret = "";
80
        if ($num < 6000) {
81
82
            $numStr = strrev($num);
83
            $len = strlen($numStr);
84
            for ($pos = 0; $pos < $len; $pos++) {
85
                $n = $numStr[$pos];
86
                $ret = self::ROMAN_NUMERALS[$pos][$n] . $ret;
87
            }
88
        }
89
        return $ret;
90
    }
91
92
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
    public static function roman2dec($roman)
94
    {
95
96
        $preDecimal = [1, 10, 100, 1000];
97
98
        $roman = strtolower($roman);
99
        //XVII
100
        $ret = 0;
101
        for ($i = strlen($roman); $i > 0;) {
102
            $numberFound = false;
103
            for ($j = 0; $j < $i && $j < strlen($roman);) {
104
                $char = substr($roman, $j, ($i - $j));
105
106
                for ($k = 0; $k < 4; ++$k) {
107
                    if (($pos = array_search($char, self::ROMAN_NUMERALS[$k])) !== false) {
108
                        $ret = ($preDecimal[$k] * $pos) + $ret;
109
                        $i = $j;
110
                        $j = 0;
111
                        $numberFound = true;
112
                        break;
113
                    }
114
                }
115
                if (!$numberFound) {
116
                    ++$j;
117
                }
118
            }
119
            if (!$numberFound) {
120
                --$i;
121
            }
122
        }
123
        $x = "foo";
124
        return $ret;
125
    }
126
    */
127
128
    public static function roman2Dec($romanNumber)
129
    {
130
131
        $roman = [
132
            "M" => 1000,
133
            "D" => 500,
134
            "C" => 100,
135
            "L" => 50,
136
            "X" => 10,
137
            "V" => 5,
138
            "I" => 1
139
        ];
140
141
        if(is_numeric($romanNumber)) {
142
            return 0;
143
        }
144
145
146
        // Convert the string to an array of roman values:
147
        for ($i = 0; $i < strlen($romanNumber); ++$i) {
148
            $char = strtoupper($romanNumber{$i});
149
            if (isset($roman[$char])) {
150
                $values[] = $roman[$char];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
151
            }
152
        }
153
154
        $sum = 0;
155
        while($current = current($values)) {
0 ignored issues
show
Bug introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
156
            $next = next($values);
157
            $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current;
158
        }
159
160
        // Return the value:
161
        return $sum;
162
    }
163
164
}