Completed
Push — master ( 9b42c4...5e919a )
by Nicolas
03:13
created

shouldRegisterCustomTranslator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 3
eloc 6
nc 3
nop 0
1
<?php namespace Modules\Translation\Providers;
2
3
use Illuminate\Support\Facades\Validator;
4
use Illuminate\Support\ServiceProvider;
5
use Modules\Core\Composers\CurrentUserViewComposer;
6
use Modules\Translation\Console\BuildTranslationsCacheCommand;
7
use Modules\Translation\Entities\Translation;
8
use Modules\Translation\Repositories\Cache\CacheTranslationDecorator;
9
use Modules\Translation\Repositories\Eloquent\EloquentTranslationRepository;
10
use Modules\Translation\Repositories\File\FileTranslationRepository as FileDiskTranslationRepository;
11
use Modules\Translation\Repositories\FileTranslationRepository;
12
use Modules\Translation\Repositories\TranslationRepository;
13
use Modules\Translation\Services\TranslationLoader;
14
use Modules\Translation\Services\Translator;
15
use Schema;
16
17
class TranslationServiceProvider extends ServiceProvider
18
{
19
    /**
20
     * Indicates if loading of the provider is deferred.
21
     *
22
     * @var bool
23
     */
24
    protected $defer = false;
25
26
    /**
27
     * Register the service provider.
28
     *
29
     * @return void
30
     */
31
    public function register()
32
    {
33
        $this->registerBindings();
34
        $this->registerConsoleCommands();
35
36
        view()->composer('translation::admin.translations.index', CurrentUserViewComposer::class);
0 ignored issues
show
Bug introduced by
The method composer does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
37
    }
38
39
    public function boot()
40
    {
41
        $this->registerValidators();
42
43
        if ($this->shouldRegisterCustomTranslator()) {
44
            $this->registerCustomTranslator();
45
        }
46
    }
47
48
    /**
49
     * Should we register the Custom Translator?
50
     * @return bool
51
     */
52
    protected function shouldRegisterCustomTranslator()
53
    {
54
        if (config('app.translations-gui', true) === false) {
55
            return false;
56
        }
57
58
        if (Schema::hasTable((new Translation)->getTable()) === false) {
59
            return false;
60
        }
61
62
        return true;
63
    }
64
65
    /**
66
     * Get the services provided by the provider.
67
     *
68
     * @return array
69
     */
70
    public function provides()
71
    {
72
        return array();
73
    }
74
75
    private function registerBindings()
76
    {
77
        $this->app->bind(TranslationRepository::class, function () {
78
            $repository = new EloquentTranslationRepository(new Translation());
0 ignored issues
show
Documentation introduced by
new \Modules\Translation\Entities\Translation() is of type object<Modules\Translation\Entities\Translation>, but the function expects a object<Modules\Core\Repositories\Eloquent\Model>.

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...
79
80
            return new CacheTranslationDecorator($repository);
81
        });
82
83
        $this->app->bind(FileTranslationRepository::class, function ($app) {
84
            return new FileDiskTranslationRepository($app['files'], $app['translation.loader']);
85
        });
86
    }
87
88
    private function registerConsoleCommands()
89
    {
90
        $this->commands([
91
            BuildTranslationsCacheCommand::class,
92
        ]);
93
    }
94
95
    protected function registerCustomTranslator()
96
    {
97
        $this->app->offsetUnset('translation.loader');
98
        $this->app->offsetUnset('translator');
99
100
        $this->app->singleton('translation.loader', function ($app) {
101
            return new TranslationLoader($app['files'], $app['path.lang']);
102
        });
103
        $this->app->singleton('translator', function ($app) {
104
            $loader = $app['translation.loader'];
105
106
            $locale = $app['config']['app.locale'];
107
108
            $trans = new Translator($loader, $locale);
109
110
            $trans->setFallback($app['config']['app.fallback_locale']);
111
112
            return $trans;
113
        });
114
    }
115
116
    private function registerValidators()
117
    {
118
        Validator::extend('extensions', function ($attribute, $value, $parameters) {
119
            return in_array($value->getClientOriginalExtension(), $parameters);
120
        });
121
122
        Validator::replacer('extensions', function ($message, $attribute, $rule, $parameters) {
123
            return str_replace([':attribute', ':values'], [$attribute, implode(',', $parameters)], $message);
124
        });
125
    }
126
}
127