Completed
Push — user-management ( d87e21 )
by Tony
03:32
created

BaseDataTable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 59
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A html() 0 7 1
getColumns() 0 1 ?
query() 0 1 ?
A getBuilderParameters() 0 13 1
A getAjax() 0 4 1
1
<?php
2
/**
3
 * BaseDataTable.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\DataTables;
27
28
use Yajra\Datatables\Services\DataTable;
29
30
abstract class BaseDataTable extends DataTable
31
{
32
    /**
33
     * Optional method if you want to use html builder.
34
     *
35
     * @return \Yajra\Datatables\Html\Builder
36
     */
37
    public function html()
38
    {
39
        return $this->builder()
40
            ->columns($this->getColumns())
41
            ->parameters($this->getBuilderParameters())
42
            ->ajax($this->getAjax());
43
    }
44
45
    /**
46
     * Get columns.
47
     *
48
     * @return array
49
     */
50
    abstract public function getColumns();
51
52
    /**
53
     * Get the query object to be processed by datatables.
54
     *
55
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
56
     */
57
    abstract public function query();
58
59
    /**
60
     * Get Builder Params
61
     *
62
     * @return array
63
     */
64
    protected function getBuilderParameters()
65
    {
66
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('dom' => '<... 'autoWidth' => false); seems to be an array, but some of its elements' types (string|false) are incompatible with the return type of the parent method Yajra\Datatables\Service...e::getBuilderParameters of type array<string,array>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
67
            'dom' => "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>".
68
                "<'row'<'col-sm-12'tr>>".
69
                "<'row'<'col-sm-5'i><'col-sm-7'p>>",
70
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
71
            'buttons' => [
72
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
73
            ],
74
            'autoWidth' => false,
75
        ];
76
    }
77
78
    /**
79
     * Get ajax url.
80
     *
81
     * @return string
82
     */
83
    public function getAjax()
84
    {
85
        return '';
86
    }
87
88
}
89