Completed
Pull Request — master (#27)
by Sebastian
04:29
created

TranslationLoader::load()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 12
nc 2
nop 3
1
<?php
2
3
namespace App\Services\Locale;
4
5
use App\Models\Fragment;
6
use Illuminate\Translation\FileLoader;
7
8
class TranslationLoader extends FileLoader
9
{
10
    /**
11
     * Load the messages for the given locale.
12
     *
13
     * @param  string $locale
14
     * @param  string $group
15
     * @param  string $namespace
16
     *
17
     * @return array
18
     */
19
    public function load($locale, $group, $namespace = null)
20
    {
21
        if (!is_null($namespace) && $namespace !== '*') {
22
            return $this->loadNamespaced($locale, $group, $namespace);
23
        }
24
25
        return Fragment::query()
26
            ->where('name', 'LIKE', "{$group}.%")
27
            ->get()
28
            ->map(function (Fragment $fragment) use ($locale, $group) {
29
                return [
30
                    'key' => str_replace("{$group}.", '', $fragment->name),
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Models\Fragment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
                    'text' => $fragment->translate($locale)->text,
32
                ];
33
            })
34
            ->pluck('text', 'key')
35
            ->toArray();
36
    }
37
}
38