Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 43ed90...3d32ec )
by Mark
02:23
created

BackendController::save()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mark
5
 * Date: 05/03/2016
6
 * Time: 20:30.
7
 */
8
9
namespace App\Plugins\Pages;
10
11
use App\Model\Menu;
12
use App\Model\Page;
13
use Illuminate\Http\Request;
14
use App\Plugins\PluginEngine;
15
use Illuminate\Support\Facades\DB;
16
use App\Classes\Repositories\PageRepository;
17
18
/**
19
 * Class Controller.
20
 */
21
class BackendController extends PluginEngine
22
{
23
    /**
24
     * @var PageRepository
25
     */
26
    private $repository;
27
28
    /**
29
     * AdminController constructor.
30
     * @param PageRepository $repository
31
     */
32
    public function __construct(PageRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * Display a listing of the resource.
39
     */
40
    public function index()
41
    {
42
        return $this->make('index')->with('pages', $this->repository->all());
43
    }
44
45
    /**
46
     * Show the form for creating a new resource.
47
     *
48
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
49
     */
50
    public function create()
51
    {
52
        return $this->make('create');
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Illuminate\Http\Request $request
59
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61
    public function store(Request $request)
62
    {
63
        $this->validate($request, ['title' => 'required|unique:pages,seo_title,NULL,id,deleted_at,NULL|min:3|max:255']);
64
65
        $this->save($request, new Page);
66
67
        return redirect()->route('admin.pages.index');
68
    }
69
70
    /**
71
     * Display the specified resource.
72
     *
73
     * @param string $name
74
     * @return bool
75
     */
76
    public function show(string $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param string $name
85
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\Response
86
     */
87
    public function edit(string $name)
88
    {
89
        return $this->make('edit')->with('page', $this->repository->whereName($name));
90
    }
91
92
    /**
93
     * Update the specified resource in storage.
94
     *
95
     * @param  \Illuminate\Http\Request $request
96
     * @param string $name
97
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    public function update(Request $request, string $name)
100
    {
101
        $page = $this->repository->whereName($name);
102
103
        $this->validate($request, ['title'=>'required|min:3|max:255|unique:pages,seo_title,'.$page->id.',id,deleted_at,NULL']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104
105
        $this->save($request, $page);
0 ignored issues
show
Bug introduced by
It seems like $page defined by $this->repository->whereName($name) on line 101 can also be of type array or object<stdClass>; however, App\Plugins\Pages\BackendController::save() does only seem to accept object<App\Model\Page>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106
107
        return redirect()->route('admin.pages.index');
108
    }
109
110
    /**
111
     * Remove the specified resource from storage.
112
     *
113
     * @param $slug
114
     * @param PageRepository $repository
115
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
116
     * @throws \Exception
117
     */
118
    public function destroy($slug, PageRepository $repository)
119
    {
120
        $page = $repository->whereName($slug);
121
122
        if ($page->editable) {
123
            $repository->whereName($slug)->delete();
0 ignored issues
show
Bug introduced by
The method delete does only exist in App\Model\Page, but not in stdClass.

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...
124
        }
125
126
        return redirect()->route('admin.pages.index');
127
    }
128
129
    /**
130
     * Resource data for saving.
131
     *
132
     * @param Request $request
133
     * @param Page $page
134
     */
135
    private function save(Request $request, Page $page)
136
    {
137
        $page->seo_title = $request['title'];
138
        $page->creator_id = $request['creator'] ?: account()->id;
139
        $page->seo_keywords = $request['keywords'];
140
        $page->seo_description = $request['description'];
141
        $page->content = $request['content'];
142
        $page->sitemap = true; //$request['sitemap'] ? true : false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
143
        $page->enabled = true; //$request['enabled'] ? true : false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
144
145
        // we should not allow important slugs to be changed.
146
        if ($page->editable == true) {
147
            $page->slug = str_slug($page->seo_title);
148
        }
149
150
        $page->save();
151
    }
152
}
153