ValidationExceptionFormatter::modifyResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Ship\Exceptions\Formatters;
4
5
use Apiato\Core\Exceptions\Formatters\ExceptionsFormatter as CoreExceptionsFormatter;
6
use Exception;
7
use Illuminate\Http\JsonResponse;
8
9
/**
10
 * Class ValidationExceptionFormatter
11
 *
12
 * @author Johannes Schobel <[email protected]>
13
 * @author  Mahmoud Zalt  <[email protected]>
14
 */
15 View Code Duplication
class ValidationExceptionFormatter extends CoreExceptionsFormatter
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
18
    /**
19
     * Status Code.
20
     *
21
     * @var  integer
22
     */
23
    CONST STATUS_CODE = 422;
24
25
    /**
26
     * @param \Exception                    $exception
27
     * @param \Illuminate\Http\JsonResponse $response
28
     *
29
     * @return  array
30
     */
31
    public function responseData(Exception $exception, JsonResponse $response)
32
    {
33
        return [
34
            'code'    => $exception->getCode(),
35
            'message' => $exception->getMessage(),
36
            'errors'      => $exception->errors(),
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like you code against a specific sub-type and not the parent class Exception as the method errors() does only exist in the following sub-classes of Exception: Illuminate\Validation\ValidationException. 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...
37
            'status_code' => self::STATUS_CODE,
38
        ];
39
    }
40
41
    /**
42
     * @param \Exception                    $exception
43
     * @param \Illuminate\Http\JsonResponse $response
44
     *
45
     * @return  mixed
46
     */
47
    function modifyResponse(Exception $exception, JsonResponse $response)
0 ignored issues
show
Best Practice introduced by Mahmoud Zalt
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49
        return $response;
50
    }
51
52
    /**
53
     * @return  int
54
     */
55
    public function statusCode()
56
    {
57
        return self::STATUS_CODE;
58
    }
59
}
60