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

ProvinceController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
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 ProvinceController class.
19
 *
20
 * @package Bantenprov\WilayahIndonesia
21
 * @author  bantenprov <[email protected]>
22
 */
23
class ProvinceController extends Controller
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
        $provinces = $this->indonesia->allProvinces();
53
54
        foreach($provinces as $province){
55
            array_set($province, 'label', $province->name);
56
        }
57
58
        $response['provinces']  = $provinces;
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 the specified resource.
68
     *
69
     * @param  \App\Province  $province
0 ignored issues
show
Bug introduced by
There is no parameter named $province. 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...
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function show($id)
73
    {
74
        $provinces = $this->indonesia->findProvince($id);
75
76
        array_set($provinces, 'label', $provinces->name);
77
78
        $response['provinces']  = $provinces;
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...
79
        $response['message']    = 'Success';
80
        $response['error']      = false;
81
        $response['status']     = true;
82
83
        return response()->json($response);
84
    }
85
}
86