Completed
Pull Request — master (#5)
by
unknown
13:27
created

N2WFacade::checkConfigIsPublished()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
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 Illuminate\Support\Facades\Facade;
12
use InvalidArgumentException;
13
use PHPViet\NumberToWords\DictionaryInterface;
14
use LogicException;
15
16
/**
17
 * @method static string toWords($number)
18
 * @method static string toCurrency($number, $unit = 'đồng')
19
 *
20
 * @author Vuong Minh <[email protected]>
21
 * @since 1.0.0
22
 */
23
class N2WFacade extends Facade
24
{
25
    /**
26
     * Từ điển hiện tại.
27
     *
28
     * @var null|DictionaryInterface
29
     */
30
    public static $dictionary;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected static function getFacadeAccessor(): Transformer
36
    {
37
        static::checkConfigIsPublished();
38
39
        $dictionary = static::$dictionary ?? static::getDefaultDictionary();
40
        $dictionary = static::makeDictionary($dictionary);
0 ignored issues
show
Bug introduced by
It seems like $dictionary defined by static::makeDictionary($dictionary) on line 40 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...
41
42
        return app('n2w', compact('dictionary'));
43
    }
44
45
    /**
46
     * Trả về từ điển mặc định trong config.
47
     *
48
     * @return string
49
     */
50
    protected static function getDefaultDictionary(): string
51
    {
52
        return config('n2w.defaults.dictionary');
53
    }
54
55
    /**
56
     * Tạo từ điển.
57
     *
58
     * @param string $dictionary
59
     * @return DictionaryInterface
60
     */
61
    protected static function makeDictionary(string $dictionary): DictionaryInterface
62
    {
63
        if (! $dictionaryClass = config("n2w.dictionaries.{$dictionary}")) {
64
            throw new InvalidArgumentException(sprintf('Dictionary (%s) is not defined!', $dictionary));
65
        }
66
67
        return app()->make($dictionaryClass);
68
    }
69
70
    /**
71
     * Throw an Exception when the config is not exist
72
     * Please run: php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"
73
     *
74
     * @throws LogicException
75
     */
76
    protected static function checkConfigIsPublished()
77
    {
78
        if (!config()->has('n2w')) {
79
            throw new LogicException("The config file is not found. You must publish the config before using it!");
80
        }
81
    }
82
}
83