UserCheck::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Torralbodavid\DuckFunkCore\Exceptions\Welcome;
4
5
use Exception;
6
use Illuminate\Contracts\Validation\Validator;
7
use Torralbodavid\DuckFunkCore\Models\Arcturus\User;
8
9
class UserCheck extends Exception
10
{
11
    protected $validator;
12
13
    protected $code = 422;
14
15
    public function __construct(Validator $validator)
16
    {
17
        $this->validator = $validator;
18
    }
19
20
    public function render()
21
    {
22 View Code Duplication
        if ($this->validator->errors()->first() == 'validation.min.string') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
23
            return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

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...
24
                'code' => 'INVALID_NAME',
25
                'validationResult' => [
26
                    'resultType' => 'VALIDATION_ERROR_NAME_TOO_SHORT',
27
                    'additionalInfo' => 2,
28
                    'valid' => false,
29
                ],
30
                'suggestions' => [User::randomNickname()],
31
            ]);
32
        }
33
34 View Code Duplication
        if ($this->validator->errors()->first() == 'validation.max.string') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35
            return response()->json([
36
                'code' => 'INVALID_NAME',
37
                'validationResult' => [
38
                    'resultType' => 'VALIDATION_ERROR_NAME_TOO_LONG',
39
                    'additionalInfo' => 15,
40
                    'valid' => false,
41
                ],
42
                'suggestions' => [User::randomNickname()],
43
            ]);
44
        }
45
46 View Code Duplication
        if ($this->validator->errors()->first() == 'validation.regex') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            return response()->json([
48
                'code' => 'INVALID_NAME',
49
                'validationResult' => [
50
                    'resultType' => 'VALIDATION_ERROR_ILLEGAL_CHARS',
51
                    'additionalInfo' => null,
52
                    'valid' => false,
53
                ],
54
                'suggestions' => [],
55
            ]);
56
        }
57
58
        if ($this->validator->errors()->first() == 'validation.user.taken') {
59
            return response()->json([
60
                'code' => 'NAME_IN_USE',
61
                'validationResult' => null,
62
                'suggestions' => [User::randomNickname()],
63
            ]);
64
        }
65
66 View Code Duplication
        if ($this->validator->errors()->first() == 'validation.user.illegal.words') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
            return response()->json([
68
                'code' => 'INVALID_NAME',
69
                'validationResult' => [
70
                    'resultType' => 'VALIDATION_ERROR_ILLEGAL_WORDS',
71
                    'additionalInfo' => '',
72
                    'valid' => false,
73
                ],
74
                'suggestions' => [User::randomNickname()],
75
            ]);
76
        }
77
78
        return response()->json([
79
            'code' => 'INVALID_NAME',
80
            'validationResult' => [
81
                'resultType' => 'VALIDATION_ERROR_UNKNOWN',
82
                'additionalInfo' => null,
83
                'valid' => false,
84
            ],
85
            'suggestions' => [],
86
        ]);
87
    }
88
}
89