HasEditor::newEditor()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 8
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Yajra\DataTables\Html\Editor\Editor;
6
7
trait HasEditor
8
{
9
    /**
10
     * Collection of Editors.
11
     *
12
     * @var null|Editor
13
     */
14
    protected $editors = [];
15
16
    /**
17
     * Attach multiple editors to builder.
18
     *
19
     * @param array|mixed ...$editors
20
     * @return $this
21
     * @see https://editor.datatables.net/
22
     * @throws \Exception
23
     */
24
    public function editors(...$editors)
25
    {
26
        if (is_array($editors[0])) {
27
            $editors = $editors[0];
28
        }
29
30
        foreach ($editors as $editor) {
31
            $this->editor($editor);
32
        }
33
34
        return $this;
35
    }
36
37
    /**
38
     * Integrate with DataTables Editor.
39
     *
40
     * @param array|Editor $fields
41
     * @return $this
42
     * @see https://editor.datatables.net/
43
     * @throws \Exception
44
     */
45
    public function editor($fields)
46
    {
47
        $this->setTemplate($this->config->get('datatables-html.editor', 'datatables::editor'));
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like setTemplate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
49
        $editor = $this->newEditor($fields);
50
51
        $this->editors[] = $editor;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param array|Editor $fields
58
     * @return array|Editor
59
     * @throws \Exception
60
     */
61
    protected function newEditor($fields)
62
    {
63
        if ($fields instanceof Editor) {
64
            $editor = $fields;
65
        } else {
66
            $editor = new Editor;
67
            $editor->fields($fields);
68
        }
69
70
        if (! $editor->table) {
71
            $editor->table($this->getTableAttribute('id'));
0 ignored issues
show
Bug introduced by
It seems like getTableAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
        }
73
74
        if (! $editor->ajax) {
75
            $editor->ajax($this->getAjaxUrl());
0 ignored issues
show
Bug introduced by
It seems like getAjaxUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
        }
77
78
        return $editor;
79
    }
80
}
81