Completed
Pull Request — master (#4330)
by xiaohui
11:24
created

Delete::handle()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 14
nop 1
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Actions;
4
5
use Encore\Admin\Actions\Response;
6
use Encore\Admin\Actions\RowAction;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Delete extends RowAction
10
{
11
    /**
12
     * @return array|null|string
13
     */
14
    public function name()
15
    {
16
        return __('admin.delete');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __('admin.delete'); (null|Illuminate\Contract...Translator|string|array) is incompatible with the return type of the parent method Encore\Admin\Actions\Action::name of type string.

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...
17
    }
18
19
    /**
20
     * @param Model $model
21
     *
22
     * @return Response
23
     */
24
    public function handle(Model $model)
25
    {
26
        $trans = [
27
            'failed'    => trans('admin.delete_failed'),
28
            'succeeded' => trans('admin.delete_succeeded'),
29
        ];
30
31
        try {
32
            $controller = $request->input('_controller');
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
33
            $destroy = false;
34
            if ($controller) {
35
                $controller = str_replace('_', '\\', $controller);
36
                if (class_exists($controller) && method_exists($controller, 'destroy')) {
37
                    $controller_obj = app($controller);
38
                    $controller_obj->destroy($model->getKey());
39
                    $destroy = true;
40
                }
41
            }
42
43
            if (!$destroy) {
44
                $model->delete();
45
            }
46
        } catch (\Exception $exception) {
47
            return $this->response()->error("{$trans['failed']} : {$exception->getMessage()}");
48
        }
49
50
        return $this->response()->success($trans['succeeded'])->refresh();
51
    }
52
53
    /**
54
     * @return void
55
     */
56
    public function dialog()
57
    {
58
        $this->question(trans('admin.delete_confirm'), '', ['confirmButtonColor' => '#d33']);
59
    }
60
}
61