MoneyValues   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 14.93 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
dl 10
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 17 1
A transform() 9 9 3
A parseMoney() 0 16 3

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
3
namespace Ghc\Rosetta\Transformers;
4
5
use Money\Currencies\ISOCurrencies;
6
use Money\Parser\AggregateMoneyParser;
7
use Money\Parser\DecimalMoneyParser;
8
use Money\Parser\IntlMoneyParser;
9
10
class MoneyValues extends Transformer
11
{
12
    /**
13
     * @var AggregateMoneyParser
14
     */
15
    private $moneyParser;
16
17
    /**
18
     * Boot Transformer.
19
     */
20
    protected function boot()
21
    {
22
        $defaultLocale = 'en_US';
23
        $numberFormatter = new \NumberFormatter($defaultLocale, \NumberFormatter::CURRENCY);
0 ignored issues
show
Bug introduced by
The type NumberFormatter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
        $this->addDefaultConfig([
26
            'locale'        => $defaultLocale,
27
            'currency_code' => $numberFormatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE),
28
        ]);
29
30
        $currencies = new ISOCurrencies();
31
        $intlParser = new IntlMoneyParser($numberFormatter, $currencies);
32
        $decimalParser = new DecimalMoneyParser($currencies);
33
34
        $this->moneyParser = new AggregateMoneyParser([
35
            $intlParser,
36
            $decimalParser,
37
        ]);
38
    }
39
40
    /**
41
     * @param array $data
42
     *
43
     * @return array
44
     */
45 View Code Duplication
    public function transform($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        foreach ($this->config['properties'] as $property) {
48
            if (isset($data[$property])) {
49
                $data[$property] = $this->parseMoney($data[$property]);
50
            }
51
        }
52
53
        return $data;
54
    }
55
56
    /**
57
     * @param $value
58
     *
59
     * @return \Money\Money
60
     */
61
    private function parseMoney($value)
62
    {
63
        $isNegative = str_contains($value, '-');
64
        $valueToParse = str_replace([' ', '+', '-'], '', $value);
65
66
        try {
67
            /** @var \Money\Money $money */
68
            $money = $this->moneyParser->parse($valueToParse);
69
            $value = [
70
                'amount'   => (int) (!$isNegative ? $money->getAmount() : -$money->getAmount()),
71
                'currency' => $money->getCurrency()->getCode(),
72
            ];
73
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
74
        }
75
76
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value also could return the type array<string,integer|string> which is incompatible with the documented return type Money\Money.
Loading history...
77
    }
78
}
79