L10nSettings::getSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Packages\L10n;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
22
use Limoncello\Contracts\Provider\ProvidesMessageResourcesInterface;
23
use Limoncello\Contracts\Settings\Packages\L10nSettingsInterface;
24
use Limoncello\l10n\Messages\FileBundleEncoder;
25
use function assert;
26
use function class_implements;
27
use function glob;
28
use function in_array;
29
use function is_string;
30
31
/**
32
 * @package Limoncello\Application
33
 */
34
abstract class L10nSettings implements L10nSettingsInterface
35
{
36
    /**
37 1
     * @var array
38
     */
39 1
    private $appConfig;
40
41 1
    /**
42
     * @inheritdoc
43 1
     */
44 1
    final public function get(array $appConfig): array
45
    {
46 1
        $this->appConfig = $appConfig;
47 1
48 1
        $defaults = $this->getSettings();
49 1
50
        $defaultLocale = $defaults[static::KEY_DEFAULT_LOCALE] ?? null;
51
        assert(
52 1
            is_string($defaultLocale) === true && empty($defaultLocale) === false,
53
            "Invalid default locale `$defaultLocale`."
54
        );
55 1
56
        $localesFolder = $defaults[static::KEY_LOCALES_FOLDER] ?? null;
57
        assert(
58
            $localesFolder !== null && empty(glob($localesFolder)) === false,
59
            "Invalid Locales folder `$localesFolder`."
60
        );
61
62 1
        $bundleEncoder = new FileBundleEncoder($this->getMessageDescriptionsFromProviders(), (string)$localesFolder);
0 ignored issues
show
Documentation introduced by
$this->getMessageDescriptionsFromProviders() is of type object<Generator>, but the function expects a object<Limoncello\l10n\Messages\iterable>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
64
        return $defaults + [
65 1
                static::KEY_LOCALES_DATA => $bundleEncoder->getStorageData((string)$defaultLocale),
66
            ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72 1
    protected function getSettings(): array
73
    {
74 1
        return [
75
            static::KEY_DEFAULT_LOCALE => 'en',
76
        ];
77
    }
78
79
    /**
80
     * @return mixed
81 1
     */
82
    protected function getAppConfig()
83 1
    {
84 1
        return $this->appConfig;
85 1
    }
86
87 1
    /**
88 1
     *
89
     * @return iterable
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    private function getMessageDescriptionsFromProviders(): iterable
92
    {
93
        $providerClasses = $this->getAppConfig()[A::KEY_PROVIDER_CLASSES] ?? [];
94
        foreach ($providerClasses as $class) {
95
            if (in_array(ProvidesMessageResourcesInterface::class, class_implements($class)) === true) {
96
                /** @var ProvidesMessageResourcesInterface $class */
97
                foreach ($class::getMessageDescriptions() as $messageDescription) {
98
                    yield $messageDescription;
99
                }
100
            }
101
        }
102
    }
103
}
104