NumberFormat::applyToNode()   B
last analyzed

Complexity

Conditions 11
Paths 130

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 11.0176

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 18
cts 19
cp 0.9474
rs 7.0666
c 0
b 0
f 0
cc 11
nc 130
nop 1
crap 11.0176

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use NumberFormatter;
8
9
/**
10
 * NumberFormat processor
11
 */
12
class NumberFormat implements Processor
13
{
14
    use Processor\Implementation;
15
    
16
    /**
17
     * Apply processing to a single node
18
     * 
19
     * @param Node $node
20
     */
21 4
    public function applyToNode(Node $node)
22
    {
23 4
        $instruction = $node->getInstruction($this);
24
        
25 4
        if (is_array($instruction) || is_object($instruction)) {
26 4
            $instruction = (object)$instruction;
27 4
        }
28
        
29 4
        if (!isset($instruction) || !isset($instruction->input)) {
30
            return;
31
        }
32
        
33 4
        $locale = isset($instruction->locale) ? $instruction->locale : 'en_US';
34 4
        $decimals = isset($instruction->decimals) ? $instruction->decimals : null;
35 4
        $currency = isset($instruction->currency) ? $instruction->currency : null;
36 4
        $style = $currency ? NumberFormatter::CURRENCY : NumberFormatter::DECIMAL;
37
        
38 4
        $number = new NumberFormatter($locale, $style);
39
        
40 4
        if (isset($decimals)) {
41 1
            $number->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
42 1
        }
43
        
44
        $result = $currency ?
45 4
            $number->formatCurrency($instruction->input, $currency) :
46 4
            $number->format($instruction->input);
47
        
48 4
        $node->setResult($result);
49 4
    }
50
}
51