Completed
Push — master ( 5bf188...109316 )
by Vuong
11:49 queued 08:37
created

N2WFacade::makeDictionary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/phpviet/laravel-number-to-words
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace PHPViet\Laravel\NumberToWords;
10
11
use InvalidArgumentException;
12
use Illuminate\Support\Facades\Facade;
13
use PHPViet\NumberToWords\DictionaryInterface;
14
15
/**
16
 * @method static string toWords($number)
17
 * @method static string toCurrency($number, $unit = 'đồng')
18
 *
19
 * @author Vuong Minh <[email protected]>
20
 * @since 1.0.0
21
 */
22
class N2WFacade extends Facade
23
{
24
    /**
25
     * Từ điển hiện tại.
26
     *
27
     * @var null|DictionaryInterface
28
     */
29
    public static $dictionary;
30
31
    /**
32
     * @inheritDoc
33
     */
34
    protected static function getFacadeAccessor(): Transformer
35
    {
36
        $dictionary = static::$dictionary ?? static::getDefaultDictionary();
37
        $dictionary = static::makeDictionary($dictionary);
0 ignored issues
show
Bug introduced by
It seems like $dictionary defined by static::makeDictionary($dictionary) on line 37 can also be of type object<PHPViet\NumberToWords\DictionaryInterface>; however, PHPViet\Laravel\NumberTo...acade::makeDictionary() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38
39
        return app('n2w', compact('dictionary'));
40
    }
41
42
    /**
43
     * Trả về từ điển mặc định trong config.
44
     *
45
     * @return string
46
     */
47
    protected static function getDefaultDictionary(): string
48
    {
49
        return config('n2w.defaults.dictionary');
50
    }
51
52
    /**
53
     * Tạo từ điển.
54
     *
55
     * @param string $dictionary
56
     * @return DictionaryInterface
57
     */
58
    protected static function makeDictionary(string $dictionary): DictionaryInterface
59
    {
60
        if (!$dictionaryClass = config("n2w.dictionaries.{$dictionary}")) {
61
            throw new InvalidArgumentException(sprintf('Dictionary (%s) is not defined!', $dictionary));
62
        }
63
64
        return app()->make($dictionaryClass);
65
    }
66
67
68
}
69