Completed
Pull Request — master (#1)
by
unknown
13:50
created

Converter::toRoman()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 0
cts 0
cp 0
rs 5.3846
cc 8
eloc 23
nc 65
nop 1
crap 72
1
<?php
2
3
namespace MaringaDojo\Romanizer;
4
5
use MaringaDojo\Romanizer\Exceptions\MaisDeTresSimbolosIguaisException;
6
use MaringaDojo\Romanizer\Exceptions\SimboloInvalidoException;
7
8
class Converter
9
{
10
    private $romanoParaNumero = [
11
        'M' => 1000,
12
        'D' => 500,
13
        'C' => 100,
14
        'L' => 50,
15
        'X' => 10,
16
        'V' => 5,
17
        'I' => 1,
18
    ];
19
20 36
    private $numeroParaRomano = [
21
        1000 => 'M',
22 36
        500 => 'D',
23 36
        100 => 'C',
24 36
        50 => 'L',
25
        10 => 'X',
26 36
        5 => 'V',
27 36
        1 => 'I',
28
    ];
29 36
30 3
    public function toDecimal($numeroRomano)
31 1
    {
32 2
        $valor = 0;
33
        $valorAnterior = 0;
34
        $repeticoes = 0;
35 33
36
        for ($i = strlen($numeroRomano) - 1; $i >= 0; $i--) {
37 33
            $letra = $numeroRomano[$i];
38 9
39
            if (! array_key_exists($letra, $this->romanoParaNumero)) {
40 9
                throw new SimboloInvalidoException(
41 3
                    $letra . 'É um simbolo invalido'
42 3
                );
43 2
            }
44
45 6
            $valorAtual = $this->romanoParaNumero[$letra];
46 33
47
            if ($valorAtual == $valorAnterior) {
48
                $repeticoes++;
49 33
50 33
                if ($repeticoes >= 3) {
51 22
                    throw new MaisDeTresSimbolosIguaisException(
52 6
                        'Os simbolos só podem ser repitidos até três vezes.'
53
                    );
54
                }
55 33
            } else {
56 22
                $repeticoes = 0;
57
            }
58 30
59
            if ($valorAtual >= $valorAnterior) {
60
                $valor += $valorAtual;
61
            } else {
62
                $valor -= $valorAtual;
63
            }
64
65
            $valorAnterior = $valorAtual;
66
        }
67
68
        return $valor;
69
    }
70
71
    public function toRoman($numero)
72
    {
73
        if(isset($this->numeroParaRomano[$numero])){
74
          return $this->numeroParaRomano[$numero];
75
        }
76
77
        $controladorMilhares = 0;
78
        $controladorCentenas = 0;
79
        $controladorDezenas = 0;
80
        if($numero > 1000){
81
            $controladorMilhares = intval($numero / 1000);
82
            $numero = $numero - ($controladorMilhares * 1000);
83
        }
84
        if($numero > 100){
85
            $controladorCentenas = intval($numero / 100);
86
            $numero = $numero - ($controladorCentenas * 100);
87
        }
88
        if($numero > 10){
89
            $controladorDezenas = intval($numero / 10);
90
            $numero = $numero - ($controladorDezenas * 10);
0 ignored issues
show
Unused Code introduced by
$numero is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        }
92
93
        $stringRomano = '';
94
95
        for($x = 0; $x < $controladorMilhares; $x++){
96
          $stringRomano .= 'M';
97
        }
98
        for($x = 0; $x < $controladorCentenas; $x++){
99
          $stringRomano .= 'C';
100
        }
101
        for($x = 0; $x < $controladorDezenas; $x++){
102
          $stringRomano .= 'X';
103
        }
104
105
        return $stringRomano;
106
    }
107
}
108