Completed
Push — master ( f01300...e5b8b7 )
by
unknown
01:51 queued 28s
created

NilaiController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 265
Duplicated Lines 15.85 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 42
loc 265
rs 10
c 0
b 0
f 0
wmc 22
lcom 2
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B index() 7 25 4
B create() 35 35 4
B store() 0 45 3
A show() 0 11 1
A edit() 0 14 1
B update() 0 57 6
A destroy() 0 12 2

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 Bantenprov\Nilai\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Nilai\Facades\NilaiFacade;
9
10
/* Models */
11
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
12
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The NilaiController class.
20
 *
21
 * @package Bantenprov\Nilai
22
 * @author  bantenprov <[email protected]>
23
 */
24
class NilaiController extends Controller
25
{
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    protected $siswa;
32
    protected $nilai;
33
    protected $user;
34
35
    public function __construct(Nilai $nilai, Siswa $siswa, User $user)
36
    {
37
        $this->nilai = $nilai;
38
        $this->siswa = $siswa;
39
        $this->user = $user;
40
    }
41
42
    /**
43
     * Display a listing of the resource.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function index(Request $request)
48
    {
49 View Code Duplication
        if (request()->has('sort')) {
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...
50
            list($sortCol, $sortDir) = explode('|', request()->sort);
51
52
            $query = $this->nilai->orderBy($sortCol, $sortDir);
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
53
        } else {
54
            $query = $this->nilai->orderBy('id', 'asc');
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
55
        }
56
57
        if ($request->exists('filter')) {
58
            $query->where(function($q) use($request) {
59
                $value = "%{$request->filter}%";
60
                $q->where('user_id', 'like', $value)
61
                    ->orWhere('id', 'like', $value);
62
            });
63
        }
64
65
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
66
        $response = $query->with('user')->with('siswa')->paginate($perPage);
67
68
        return response()->json($response)
69
            ->header('Access-Control-Allow-Origin', '*')
70
            ->header('Access-Control-Allow-Methods', 'GET');
71
    }
72
73
    /**
74
     * Show the form for creating a new resource.
75
     *
76
     * @return \Illuminate\Http\Response
77
     */
78 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method 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...
79
    {
80
        $response = [];
81
82
        $siswas = $this->siswa->all();
83
        $users_special = $this->user->all();
84
        $users_standar = $this->user->find(\Auth::User()->id);
85
        $current_user = \Auth::User();
86
87
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
88
89
        if($role_check){
90
            $response['user_special'] = true;
91
            foreach($users_special as $user){
92
                array_set($user, 'label', $user->name);
93
            }
94
            $response['user'] = $users_special;
95
        }else{
96
            $response['user_special'] = false;
97
            array_set($users_standar, 'label', $users_standar->name);
98
            $response['user'] = $users_standar;
99
        }
100
101
        array_set($current_user, 'label', $current_user->name);
102
103
        foreach($siswas as $siswa){
104
            array_set($siswa, 'label', $siswa->nama_siswa);
105
        }
106
107
        $response['siswa'] = $siswas;
108
        $response['current_user'] = $current_user;
109
        $response['status'] = true;
110
111
        return response()->json($response);
112
    }
113
114
    /**
115
     * Display the specified resource.
116
     *
117
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. 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...
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function store(Request $request)
121
    {
122
        $nilai = $this->nilai;
123
124
        $validator = Validator::make($request->all(), [
125
            'user_id' => 'required|unique:nilais,user_id',
126
            'siswa_id' => 'required|unique:nilais,siswa_id',
127
            'akademik' => 'required',
128
            'prestasi' => 'required',
129
            'zona' => 'required',
130
            'sktm' => 'required'
131
        ]);
132
133
        if($validator->fails()){
134
            $check = $nilai->where('user_id',$request->user_id)->orWhere('siswa_id',$request->siswa_id)->whereNull('deleted_at')->count();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
136
            if ($check > 0) {
137
                $response['message'] = 'Failed ! Username, Nama Siswa, already exists';
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...
138
            } else {
139
                $nilai->user_id = $request->input('user_id');
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
140
                $nilai->siswa_id = $request->input('siswa_id');
0 ignored issues
show
Documentation introduced by
The property siswa_id does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
                $nilai->akademik = $request->input('akademik');
0 ignored issues
show
Documentation introduced by
The property akademik does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
                $nilai->prestasi = $request->input('prestasi');
0 ignored issues
show
Documentation introduced by
The property prestasi does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
                $nilai->zona = $request->input('zona');
0 ignored issues
show
Documentation introduced by
The property zona does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
144
                $nilai->sktm = $request->input('sktm');
0 ignored issues
show
Documentation introduced by
The property sktm does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
145
                $nilai->save();
146
147
                $response['message'] = 'success';
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...
148
            }
149
        } else {
150
                $nilai->user_id = $request->input('user_id');
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
                $nilai->siswa_id = $request->input('siswa_id');
0 ignored issues
show
Documentation introduced by
The property siswa_id does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
                $nilai->akademik = $request->input('akademik');
0 ignored issues
show
Documentation introduced by
The property akademik does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
                $nilai->prestasi = $request->input('prestasi');
0 ignored issues
show
Documentation introduced by
The property prestasi does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
154
                $nilai->zona = $request->input('zona');
0 ignored issues
show
Documentation introduced by
The property zona does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
155
                $nilai->sktm = $request->input('sktm');
0 ignored issues
show
Documentation introduced by
The property sktm does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156
                $nilai->save();
157
158
            $response['message'] = 'success';
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...
159
        }
160
161
        $response['status'] = true;
162
163
        return response()->json($response);
164
    }
165
166
    /**
167
     * Store a newly created resource in storage.
168
     *
169
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. 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...
170
     * @return \Illuminate\Http\Response
171
     */
172
    public function show($id)
173
    {
174
        $nilai = $this->nilai->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
175
176
        $response['user'] = $nilai->user;
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...
177
        $response['nilai'] = $nilai;
178
        $response['siswa'] = $nilai->siswa;
179
        $response['status'] = true;
180
181
        return response()->json($response);
182
    }
183
184
    /**
185
     * Show the form for editing the specified resource.
186
     *
187
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. 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...
188
     * @return \Illuminate\Http\Response
189
     */
190
    public function edit($id)
191
    {
192
        $nilai = $this->nilai->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
193
194
        array_set($nilai->user, 'label', $nilai->user->name);
195
        array_set($nilai->siswa, 'label', $nilai->siswa->nama_siswa);
196
197
        $response['nilai'] = $nilai;
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...
198
        $response['siswa'] = $nilai->siswa;
199
        $response['user'] = $nilai->user;
200
        $response['status'] = true;
201
202
        return response()->json($response);
203
    }
204
205
    /**
206
     * Update the specified resource in storage.
207
     *
208
     * @param  \Illuminate\Http\Request  $request
209
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. 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...
210
     * @return \Illuminate\Http\Response
211
     */
212
    public function update(Request $request, $id)
213
    {
214
        $response = array();
215
        $message  = array();
216
217
        $nilai = $this->nilai->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
218
219
            $validator = Validator::make($request->all(), [
220
                'user_id' => 'required|unique:nilais,user_id,'.$id,
221
                'siswa_id' => 'required|unique:nilais,siswa_id,'.$id,
222
                'akademik' => 'required',
223
                'prestasi' => 'required',
224
                'zona' => 'required',
225
                'sktm' => 'required'
226
227
            ]);
228
229
        if ($validator->fails()) {
230
231
            foreach($validator->messages()->getMessages() as $key => $error){
232
                        foreach($error AS $error_get) {
233
                            array_push($message, $error_get);
234
                        }
235
                    }
236
237
             $check_user     = $this->nilai->where('id','!=', $id)->where('user_id', $request->user_id);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
238
             $check_siswa = $this->nilai->where('id','!=', $id)->where('siswa_id', $request->siswa_id);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
239
240
             if($check_user->count() > 0 || $check_siswa->count() > 0 ){
241
                  $response['message'] = implode("\n",$message);
242
            } else {
243
                $nilai->user_id = $request->input('user_id');
244
                $nilai->siswa_id = $request->input('siswa_id');
245
                $nilai->akademik = $request->input('akademik');
246
                $nilai->prestasi = $request->input('prestasi');
247
                $nilai->zona = $request->input('zona');
248
                $nilai->sktm = $request->input('sktm');
249
                $nilai->save();
250
251
                $response['message'] = 'success';
252
            }
253
        } else {
254
                $nilai->user_id = $request->input('user_id');
255
                $nilai->siswa_id = $request->input('siswa_id');
256
                $nilai->akademik = $request->input('akademik');
257
                $nilai->prestasi = $request->input('prestasi');
258
                $nilai->zona = $request->input('zona');
259
                $nilai->sktm = $request->input('sktm');
260
                $nilai->save();
261
262
            $response['message'] = 'success';
263
        }
264
265
        $response['status'] = true;
266
267
        return response()->json($response);
268
    }
269
270
    /**
271
     * Remove the specified resource from storage.
272
     *
273
     * @param  \App\Nilai  $nilai
0 ignored issues
show
Bug introduced by
There is no parameter named $nilai. 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...
274
     * @return \Illuminate\Http\Response
275
     */
276
    public function destroy($id)
277
    {
278
        $nilai = $this->nilai->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Bantenprov\Nilai\...Bantenprov\Nilai\Nilai>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
279
280
        if ($nilai->delete()) {
281
            $response['status'] = true;
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...
282
        } else {
283
            $response['status'] = false;
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...
284
        }
285
286
        return json_encode($response);
287
    }
288
}
289