GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AttachmentsController::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Afrittella\BackProject\Http\Controllers;
4
5
use Afrittella\BackProject\Exceptions\NotFoundException;
6
use Afrittella\BackProject\Http\Requests\AttachmentAdd;
7
use Afrittella\BackProject\Repositories\Attachments;
8
use Afrittella\BackProject\Repositories\Criteria\Attachments\ByUser;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Prologue\Alerts\Facades\Alert;
12
13
class AttachmentsController extends Controller
14
{
15
    public function __construct()
16
    {
17
        $this->middleware('admin');
18
    }
19
20
    public function index(Attachments $attachments)
21
    {
22
        // pushCriteria is a method from Repository Pattern
23
        $attachments->pushCriteria(new ByUser());
24
        return view('back-project::attachments.index')->with('attachments', $attachments->all());
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
25
    }
26
27
28
    /**
29
     * @param Attachments $attachments
30
     * @param $id
31
     * @return $this
32
     */
33
    public function edit(Attachments $attachments, $id)
34
    {
35
        $attachment = $attachments->find($id);
36
37
        $this->bCAuthorize('update', $attachment);
38
39
        return view('back-project::attachments.edit')->with('attachment', $attachment);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
40
    }
41
42 View Code Duplication
    public function store(AttachmentAdd $request, Attachments $attachments)
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...
43
    {
44
        $user = Auth::user();
45
46
        $success = $attachments->create(array_merge($request->all(), ['user_id' => $user->id]));
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Unused Code introduced by
$success 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...
47
48
        Alert::add('success', trans('back-project::base.image_uploaded'))->flash();
49
50
        return response()->json([
51
            'success' => true,
52
            'message' => trans('back-project::base.image_uploaded')
53
        ]);
54
55
    }
56
57 View Code Duplication
    public function update(Request $request, Attachments $attachments, $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...
58
    {
59
        $attachment = $attachments->find($id);
60
61
        $this->bCAuthorize('update', $attachment);
62
63
        $attachment = $attachments->update($request->all(), $id);
0 ignored issues
show
Unused Code introduced by
$attachment 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...
64
65
        Alert::add('success', trans('back-project::crud.model_updated', ['model' => trans('back-project::media.image')]))->flash();
66
67
        return redirect(route('bp.attachments.index'));
68
    }
69
70
    public function delete(Attachments $attachments, $id)
71
    {
72
        $attachment = $attachments->find($id);
73
74
        $this->bCAuthorize('delete', $attachment);
75
76
        $attachments->delete($id);
77
78
        Alert::add('success', trans('back-project::crud.model_deleted', ['model' => trans('back-project::media.image')]))->flash();
79
80
        return back();
81
    }
82
83 View Code Duplication
    public function setMain(Attachments $attachments, $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...
84
    {
85
        $attachment = $attachments->find($id);
86
87
        $this->bCAuthorize('update', $attachment);
88
89
        $data = [
90
            'is_main' => 1
91
        ];
92
93
        $attachments->update($data, $id);
94
95
        Alert::add('success', trans('back-project::crud.model_updated', ['model' => trans('back-project::media.image')]))->flash();
96
97
        return redirect(route('bp.attachments.index'));
98
    }
99
}