ServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 25 2
A register() 0 4 1
1
<?php namespace Felixkiss\UniqueWithValidator;
2
3
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
4
5
class ServiceProvider extends BaseServiceProvider
6
{
7
    /**
8
     * Bootstrap the application events.
9
     *
10
     * @return void
11
     */
12
    public function boot()
13
    {
14
        $this->loadTranslationsFrom(
15
            __DIR__ . '/../../lang',
16
            'uniquewith-validator'
17
        );
18
19
        $message = $this->app->translator->trans('uniquewith-validator::validation.unique_with');
20
        $this->app->validator->extend('unique_with', Validator::class . '@validateUniqueWith', $message);
21
        $this->app->validator->replacer('unique_with', function() {
22
            // Since 5.4.20, the validator is passed in as the 5th parameter.
23
            // In order to preserve backwards compatibility, we check if the 
24
            // validator is passed and use the validator's translator instead
25
            // of getting it out of the container.
26
            $arguments = func_get_args();
27
            if (sizeof($arguments) >= 5) {
28
                $arguments[4] = $arguments[4]->getTranslator();
29
            }
30
            else {
31
                $arguments[4] = $this->app->translator;
0 ignored issues
show
Bug introduced by
Accessing translator on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
32
            }
33
34
            return call_user_func_array([new Validator, 'replaceUniqueWith'], $arguments);
35
        });
36
    }
37
38
    /**
39
     * Register the service provider.
40
     *
41
     * @return void
42
     */
43
    public function register()
44
    {
45
46
    }
47
}
48