Completed
Push — development ( f57bd7...a7d33c )
by Claudio
04:40
created

NuxController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 6
loc 57
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B checkName() 6 12 5
A selectName() 0 6 1
A selectRoom() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\Nux;
6
use App\Facades\User as UserFacade;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response;
10
use Laravel\Lumen\Routing\Controller as BaseController;
11
12
/**
13
 * Class NuxController
14
 * @package App\Http\Controllers
15
 */
16
class NuxController extends BaseController
17
{
18
    /**
19
     * Check an User Name.
20
     *
21
     * @param Request $request
22
     *
23
     * @return JsonResponse
24
     */
25
    public function checkName(Request $request): JsonResponse
26
    {
27 View Code Duplication
        if (UserFacade::where('username', $request->json()->get('name'))->count() > 0 && $request->json()->get('name') != UserFacade::getUser()->name) {
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...
28
            return response()->json(['code' => 'NAME_IN_USE', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
29
        }
30
31 View Code Duplication
        if (strlen($request->json()->get('name')) > 50 || !UserFacade::filterName($request->json()->get('name'))) {
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...
32
            return response()->json(['code' => 'INVALID_NAME', 'validationResult' => ['resultType' => 'VALIDATION_ERROR_ILLEGAL_WORDS'], 'suggestions' => []]);
33
        }
34
35
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
36
    }
37
38
    /**
39
     * Select an User Name.
40
     *
41
     * @param Request $request
42
     *
43
     * @return JsonResponse
44
     */
45
    public function selectName(Request $request): JsonResponse
46
    {
47
        UserFacade::updateSession(['username' => $request->json()->get('name')]);
48
49
        return response()->json(['code' => 'OK', 'validationResult' => null, 'suggestions' => []]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\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...
50
    }
51
52
    /**
53
     * Select a Room.
54
     *
55
     * @TODO: Generate the Room for the User
56
     * @TODO: Get Room Models.
57
     *
58
     * @param Request $request
59
     *
60
     * @return Response
61
     */
62
    public function selectRoom(Request $request): Response
63
    {
64
        if (!in_array($request->json()->get('roomIndex'), [1, 2, 3])) {
65
            return response('', 400);
66
        }
67
68
        UserFacade::updateSession(['traits' => (Nux::generateRoom($request) ? ['USER'] : ['USER', 'NEW_USER'])]);
69
70
        return response(null, 200);
71
    }
72
}