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.
Completed
Push — master ( b864c9...ed9166 )
by butschster
12:41
created

DisplayController::async()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 7
nop 4
dl 0
loc 28
ccs 0
cts 25
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\Controller;
8
use SleepingOwl\Admin\Display\DisplayTree;
9
use SleepingOwl\Admin\Display\DisplayTabbed;
10
use Illuminate\Contracts\Foundation\Application;
11
use SleepingOwl\Admin\Display\DisplayDatatablesAsync;
12
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
13
14
class DisplayController extends Controller
15
{
16
    /**
17
     * @param ModelConfigurationInterface $model
18
     * @param Request $request
19
     * @param Application $application
20
     * @param null $name
21
     *
22
     * @return JsonResponse
23
     */
24
    public function async(ModelConfigurationInterface $model, Request $request, Application $application, $name = null)
25
    {
26
        $display = $model->fireDisplay();
27
28
        if ($display instanceof DisplayTabbed) {
29
            $tabs = $display->getTabs();
30
            foreach ($tabs as $tab) {
31
                $content = $tab->getContent();
32
                if ($content instanceof DisplayDatatablesAsync && $content->getName() === $name) {
33
                    return $content;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $content; (SleepingOwl\Admin\Display\DisplayDatatablesAsync) is incompatible with the return type documented by SleepingOwl\Admin\Http\C...isplayController::async of type Illuminate\Http\JsonResponse|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
                }
35
            }
36
        }
37
38
        if ($display instanceof DisplayDatatablesAsync) {
39
            try {
40
                return $display->renderAsync($request);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $display->renderAsync($request); (array<string,string|array|integer>) is incompatible with the return type documented by SleepingOwl\Admin\Http\C...isplayController::async of type Illuminate\Http\JsonResponse|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
            } catch (\Exception $exception) {
42
                return new JsonResponse([
43
                    'message'  => $application->isLocal()
44
                        ? $exception->getMessage()
45
                        : trans('sleeping_owl::lang.table.error'),
46
                ], 403);
47
            }
48
        }
49
50
        abort(404);
51
    }
52
53
    /**
54
     * @param ModelConfigurationInterface $model
55
     * @param Request $request
56
     */
57
    public function treeReorder(ModelConfigurationInterface $model, Request $request)
58
    {
59
        $display = $model->fireDisplay();
60
61
        if ($display instanceof DisplayTabbed) {
62
            $display->getTabs()->each(function ($tab) use ($request) {
63
                $content = $tab->getContent();
64
                if ($content instanceof DisplayTree) {
65
                    $content->getRepository()->reorder(
66
                        $request->input('data')
0 ignored issues
show
Bug introduced by
It seems like $request->input('data') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type string; however, SleepingOwl\Admin\Contra...oryInterface::reorder() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
                    );
68
                }
69
            });
70
        } else {
71
            $display->getRepository()->reorder(
72
                $request->input('data')
73
            );
74
        }
75
    }
76
}
77