Passed
Push — master ( 12d5d1...bed47c )
by Zhengchao
06:23
created

InputRequestServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 74
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 16 2
A register() 0 4 1
A registerCommands() 0 6 1
A registerRequestMakeCommand() 0 6 1
A initializeRequest() 0 17 2
1
<?php
2
3
/*
4
 * This file is part of emanci/lumen-request package.
5
 *
6
 * (c) emanci <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Emanci\LumenRequest;
13
14
use Emanci\LumenRequest\Console\RequestMakeCommand;
15
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
16
use Illuminate\Support\ServiceProvider;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class InputRequestServiceProvider extends ServiceProvider
20
{
21
    /**
22
     * Bootstrap the application events.
23
     */
24
    public function boot()
25
    {
26 3
        $this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) {
27 3
            if (method_exists($resolved, 'validate')) {
28 3
                $resolved->validate();
29
            } else {
30
                $resolved->validateResolved();
31
            }
32 3
        });
33
34 3
        $this->app->resolving(InputRequest::class, function ($request, $app) {
35 3
            $this->initializeRequest($request, $app['request']);
36
37 3
            $request->setContainer($app);
38 3
        });
39 3
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function register()
45
    {
46 3
        $this->registerCommands();
47 3
    }
48
49
    /**
50
     * Register the given commands.
51
     */
52 3
    protected function registerCommands()
53
    {
54 3
        $this->registerRequestMakeCommand();
55
56 3
        $this->commands('command.request.make');
57 3
    }
58
59
    /**
60
     * Register the command.
61
     */
62
    protected function registerRequestMakeCommand()
63
    {
64 3
        $this->app->singleton('command.request.make', function ($app) {
65
            return new RequestMakeCommand($app['files']);
66 3
        });
67 3
    }
68
69
    /**
70
     * Initialize the form request with data from the given request.
71
     *
72
     * @param InputRequest $form
73
     * @param Request      $current
74
     */
75 3
    protected function initializeRequest(InputRequest $form, Request $current)
76
    {
77 3
        $files = $current->files->all();
78
79 3
        $files = is_array($files) ? array_filter($files) : $files;
80
81 3
        $form->initialize(
82 3
            $current->query->all(), $current->request->all(), $current->attributes->all(),
83 3
            $current->cookies->all(), $files, $current->server->all(), $current->getContent()
84
        );
85
86 3
        $form->setJson($current->json());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method json() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Emanci\LumenRequest\InputRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
87
88 3
        $form->setUserResolver($current->getUserResolver());
0 ignored issues
show
Bug introduced by
The method getUserResolver() does not exist on Symfony\Component\HttpFoundation\Request. Did you maybe mean getUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
89
90 3
        $form->setRouteResolver($current->getRouteResolver());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\Request as the method getRouteResolver() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\Request: Emanci\LumenRequest\InputRequest, Illuminate\Http\Request. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
91 3
    }
92
}
93