Completed
Push — master ( 120590...805ea8 )
by
unknown
05:39 queued 34s
created

DistrictController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Bantenprov\WilayahIndonesia\Http\Controllers;
3
4
/* Require */
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
/* Models */
9
use Laravolt\Indonesia\Indonesia;
10
use Laravolt\Indonesia\Models\Province;
11
use Laravolt\Indonesia\Models\City;
12
use Laravolt\Indonesia\Models\District;
13
use Laravolt\Indonesia\Models\Village;
14
15
/* Etc */
16
17
/**
18
 * The DistrictController class.
19
 *
20
 * @package Bantenprov\WilayahIndonesia
21
 * @author  bantenprov <[email protected]>
22
 */
23 View Code Duplication
class DistrictController extends Controller
0 ignored issues
show
Duplication introduced by
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...
24
{
25
    protected $indonesia;
26
    protected $province;
27
    protected $city;
28
    protected $district;
29
    protected $village;
30
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
		$this->indonesia    = new Indonesia;
39
		$this->province     = new Province;
40
		$this->city         = new City;
41
		$this->district     = new District;
42
		$this->village      = new Village;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function get()
51
    {
52
        $districts = $this->indonesia->allDistricts();
53
54
        foreach($districts as $district){
55
            array_set($district, 'label', $district->name);
56
        }
57
58
        $response['districts']  = $districts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
59
        $response['message']    = 'Success';
60
        $response['error']      = false;
61
        $response['status']     = true;
62
63
        return response()->json($response);
64
    }
65
66
    /**
67
     * Display a listing of the resource.
68
     *
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function getByCity($id)
72
    {
73
        $districts = $this->indonesia->findCity($id, ['districts']);
74
75
        if (isset($districts->districts)) {
76
            $districts = $districts->districts;
77
        } else {
78
            $districts = $this->district->getAttributes();
79
        }
80
81
        foreach($districts as $district){
82
            array_set($district, 'label', $district->name);
83
        }
84
85
        $response['districts']  = $districts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
86
        $response['message']    = 'Success';
87
        $response['error']      = false;
88
        $response['status']     = true;
89
90
        return response()->json($response);
91
    }
92
93
    /**
94
     * Display the specified resource.
95
     *
96
     * @param  \App\District  $district
0 ignored issues
show
Bug introduced by
There is no parameter named $district. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function show($id)
100
    {
101
        $district = $this->indonesia->findDistrict($id);
102
103
        array_set($district, 'label', $district->name);
104
105
        $response['district']   = $district;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
106
        $response['message']    = 'Success';
107
        $response['error']      = false;
108
        $response['status']     = true;
109
110
        return response()->json($response);
111
    }
112
}
113