Completed
Push — master ( adcb27...ffa39f )
by
unknown
10s
created

NilaiController::index()   C

Complexity

Conditions 9
Paths 25

Size

Total Lines 53
Code Lines 33

Duplication

Lines 11
Ratio 20.75 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 25
nop 1
dl 11
loc 53
rs 6.8963
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Illuminate\Support\Facades\DB;
9
use Bantenprov\Nilai\Facades\NilaiFacade;
10
11
/* Models */
12
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use App\User;
15
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
16
17
/* Etc */
18
use Validator;
19
use Auth;
20
21
/**
22
 * The NilaiController class.
23
 *
24
 * @package Bantenprov\Nilai
25
 * @author  bantenprov <[email protected]>
26
 */
27
class NilaiController extends Controller
28
{
29
    protected $siswa;
30
    protected $nilai;
31
    protected $user;
32
    protected $admin_sekolah;
33
34
    /**
35
     * Create a new controller instance.
36
     *
37
     * @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...
38
     */
39
    public function __construct(Nilai $nilai, Siswa $siswa, User $user)
40
    {
41
        $this->nilai         = new Nilai;
42
        $this->siswa         = new Siswa;
43
        $this->user          = new User;
44
        $this->admin_sekolah = new AdminSekolah;
45
    }
46
47
    /**
48
     * Display a listing of the resource.
49
     *
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function index(Request $request)
53
    {
54
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
55
56
        if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){
57
            $response = [];
58
            return response()->json($response)
59
            ->header('Access-Control-Allow-Origin', '*')
60
            ->header('Access-Control-Allow-Methods', 'GET');
61
        }
62
        
63
        if (request()->has('sort')) {
64
            list($sortCol, $sortDir) = explode('|', request()->sort);
65
66
            if($this->checkRole(['superadministrator'])){
67
                $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...
68
            }else{
69
                $query = $this->nilai->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir);
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...
70
            }
71
        } else {
72
            if($this->checkRole(['superadministrator'])){
73
                $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...
74
            }else{
75
                $query = $this->nilai->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc');            
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...
76
            }
77
        }
78
79
        if ($request->exists('filter')) {
80
            if($this->checkRole(['superadministrator'])){
81 View Code Duplication
                $query->where(function($q) use($request) {
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...
82
                    $value = "%{$request->filter}%";
83
84
                    $q->where('sekolah_id', 'like', $value)
85
                        ->orWhere('admin_sekolah_id', 'like', $value);
86
                });
87
            }else{
88 View Code Duplication
                $query->where(function($q) use($request, $admin_sekolah) {
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...
89
                    $value = "%{$request->filter}%";
90
91
                    $q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value);
92
                });
93
            }
94
95
        }
96
97
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
98
99
        $response   = $query->with(['siswa', 'user', 'nilai_akademik'])->paginate($perPage);
100
101
        return response()->json($response)
102
            ->header('Access-Control-Allow-Origin', '*')
103
            ->header('Access-Control-Allow-Methods', 'GET');
104
    }
105
106
    /**
107
     * Display a listing of the resource.
108
     *
109
     * @return \Illuminate\Http\Response
110
     */
111 View Code Duplication
    public function get()
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...
112
    {
113
        $nilais = $this->nilai->with(['siswa', 'user', 'nilai_akademik'])->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
114
115
        foreach ($nilais as $nilai) {
116
            if ($nilai->siswa !== null) {
117
                array_set($nilai, 'label', $nilai->siswa->nomor_un.' - '.$nilai->siswa->nama_siswa);
118
            } else {
119
                array_set($nilai, 'label', $nilai->nomor_un.' - ');
120
            }
121
        }
122
123
        $response['nilais']     = $nilais;
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...
124
        $response['error']      = false;
125
        $response['message']    = 'Success';
126
        $response['status']     = true;
127
128
        return response()->json($response);
129
    }
130
131
    /**
132
     * Show the form for creating a new resource.
133
     *
134
     * @return \Illuminate\Http\Response
135
     */
136
    public function create()
137
    {
138
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
139
        $nilai          = $this->nilai->getAttributes();
140
       // $siswas         = $this->siswa->getAttributes();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
        $users_special  = $this->user->all();
143
        $users_standar  = $this->user->findOrFail($user_id);
144
        $current_user   = Auth::User();
145
146
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
147
148
        if($this->checkRole(['superadministrator'])){
149
            $siswas = $this->siswa->all();
150
        }else{
151
            $sekolah_id = $admin_sekolah->sekolah_id;
152
            $siswas     = $this->siswa->where('sekolah_id', $sekolah_id)->get();
153
        }
154
155
        foreach ($siswas as $siswa) {
156
            array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa);
157
        }
158
159
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
160
161
        if ($role_check) {
162
            $user_special = true;
163
164
            foreach($users_special as $user){
165
                array_set($user, 'label', $user->name);
166
            }
167
168
            $users = $users_special;
169
        } else {
170
            $user_special = false;
171
172
            array_set($users_standar, 'label', $users_standar->name);
173
174
            $users = $users_standar;
175
        }
176
177
        array_set($current_user, 'label', $current_user->name);
178
179
        $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...
180
        $response['siswa']          = $siswas;
181
        $response['users']          = $users;
182
        $response['user_special']   = $user_special;
183
        $response['current_user']   = $current_user;
184
        $response['error']          = false;
185
        $response['message']        = 'Success';
186
        $response['status']         = true;
187
188
        return response()->json($response);
189
    }
190
191
    /**
192
     * Store a newly created resource in storage.
193
     *
194
     * @param  \Illuminate\Http\Request  $request
195
     * @return \Illuminate\Http\Response
196
     */
197
    public function store(Request $request)
198
    {
199
        $nilai = $this->nilai;
200
201
        $validator = Validator::make($request->all(), [
202
            'nomor_un'  => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->nilai->getTable()},nomor_un,NULL,id,deleted_at,NULL",
203
            'bobot'     => 'required|numeric|min:0|max:100',
204
            'akademik'  => 'required|numeric|min:0|max:100',
205
            'prestasi'  => 'required|numeric|min:0|max:100',
206
            'zona'      => 'required|numeric|min:0|max:100',
207
            'sktm'     => 'required|numeric|min:0|max:100',
208
            // 'total'      => 'required|numeric|min:0|max:100',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
            'user_id'   => "required|exists:{$this->user->getTable()},id",
210
        ]);
211
212 View Code Duplication
        if ($validator->fails()) {
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...
213
            $error      = true;
214
            $message    = $validator->errors()->first();
215
        } else {
216
            $nilai->nomor_un    = $request->input('nomor_un');
0 ignored issues
show
Documentation introduced by
The property nomor_un 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...
217
            $nilai->bobot       = $request->input('bobot');
0 ignored issues
show
Documentation introduced by
The property bobot 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...
218
            $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...
219
            $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...
220
            $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...
221
            $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...
222
            $nilai->total       = null; // $request->input('total');
0 ignored issues
show
Documentation introduced by
The property total 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...
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
223
            $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...
224
            $nilai->save();
225
226
            $error      = false;
227
            $message    = 'Success';
228
        }
229
230
        $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...
231
        $response['error']      = $error;
232
        $response['message']    = $message;
233
        $response['status']     = true;
234
235
        return response()->json($response);
236
    }
237
238
    /**
239
     * Display the specified resource.
240
     *
241
     * @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...
242
     * @return \Illuminate\Http\Response
243
     */
244 View Code Duplication
    public function show($id)
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...
245
    {
246
        $nilai = $this->nilai->with(['siswa', 'user', 'nilai_akademik'])->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
247
248
        $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...
249
        $response['error']      = false;
250
        $response['message']    = 'Success';
251
        $response['status']     = true;
252
253
        return response()->json($response);
254
    }
255
256
    /**
257
     * Show the form for editing the specified resource.
258
     *
259
     * @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...
260
     * @return \Illuminate\Http\Response
261
     */
262 View Code Duplication
    public function edit($id)
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...
263
    {
264
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
265
        $nilai          = $this->nilai->with(['siswa', 'user'])->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
266
        $siswas         = $this->siswa->getAttributes();
267
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
268
        $users_special  = $this->user->all();
269
        $users_standar  = $this->user->findOrFail($user_id);
270
        $current_user   = Auth::User();
271
272
        if ($nilai->siswa !== null) {
273
            array_set($nilai->siswa, 'label', $nilai->siswa->nomor_un.' - '.$nilai->siswa->nama_siswa);
274
        }
275
276
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
277
278
        if ($nilai->user !== null) {
279
            array_set($nilai->user, 'label', $nilai->user->name);
280
        }
281
282
        if ($role_check) {
283
            $user_special = true;
284
285
            foreach($users_special as $user){
286
                array_set($user, 'label', $user->name);
287
            }
288
289
            $users = $users_special;
290
        } else {
291
            $user_special = false;
292
293
            array_set($users_standar, 'label', $users_standar->name);
294
295
            $users = $users_standar;
296
        }
297
298
        array_set($current_user, 'label', $current_user->name);
299
300
        $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...
301
        $response['siswas']         = $siswas;
302
        $response['users']          = $users;
303
        $response['user_special']   = $user_special;
304
        $response['current_user']   = $current_user;
305
        $response['error']          = false;
306
        $response['message']        = 'Success';
307
        $response['status']         = true;
308
309
        return response()->json($response);
310
    }
311
312
    /**
313
     * Update the specified resource in storage.
314
     *
315
     * @param  \Illuminate\Http\Request  $request
316
     * @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...
317
     * @return \Illuminate\Http\Response
318
     */
319
    public function update(Request $request, $id)
320
    {
321
        $nilai = $this->nilai->with(['siswa', 'user'])->findOrFail($id);
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
322
323
        $validator = Validator::make($request->all(), [
324
            // 'nomor_un'  => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->nilai->getTable()},nomor_un,{$id},id,deleted_at,NULL",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
325
            'bobot'     => 'required|numeric|min:0|max:100',
326
            'akademik'  => 'required|numeric|min:0|max:100',
327
            'prestasi'  => 'required|numeric|min:0|max:100',
328
            'zona'      => 'required|numeric|min:0|max:100',
329
            'sktm'      => 'required|numeric|min:0|max:100',
330
            // 'total'     => 'required|numeric|min:0|max:100',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
331
            'user_id'   => "required|exists:{$this->user->getTable()},id",
332
        ]);
333
334 View Code Duplication
        if ($validator->fails()) {
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...
335
            $error      = true;
336
            $message    = $validator->errors()->first();
337
        } else {
338
            $nilai->nomor_un    = $nilai->nomor_un; // $request->input('nomor_un');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
339
            $nilai->bobot       = $request->input('bobot');
340
            $nilai->akademik    = $request->input('akademik');
341
            $nilai->prestasi    = $request->input('prestasi');
342
            $nilai->zona        = $request->input('zona');
343
            $nilai->sktm        = $request->input('sktm');
344
            $nilai->total       = null; // $request->input('total');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
345
            $nilai->user_id     = $request->input('user_id');
346
            $nilai->save();
347
348
            $error      = false;
349
            $message    = 'Success';
350
        }
351
352
        $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...
353
        $response['error']      = $error;
354
        $response['message']    = $message;
355
        $response['status']     = true;
356
357
        return response()->json($response);
358
    }
359
360
    /**
361
     * Remove the specified resource from storage.
362
     *
363
     * @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...
364
     * @return \Illuminate\Http\Response
365
     */
366 View Code Duplication
    public function destroy($id)
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...
367
    {
368
        $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...
369
370
        if ($nilai->delete()) {
371
            $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...
372
            $response['success']    = true;
373
            $response['status']     = true;
374
        } else {
375
            $response['message']    = 'Failed';
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...
376
            $response['success']    = false;
377
            $response['status']     = false;
378
        }
379
380
        return json_encode($response);
381
    }
382
383
    protected function checkRole($role = array())
384
    {
385
        return Auth::user()->hasRole($role);
386
    }
387
}
388