Completed
Push — master ( 3f5d51...85b3b0 )
by Arjay
01:13
created

HasEditor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A editors() 0 8 2
A editor() 0 10 1
A newEditor() 0 19 4
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 mixed ...$editors
20
     * @return $this
21
     * @see https://editor.datatables.net/
22
     * @throws \Exception
23
     */
24
    public function editors(...$editors)
25
    {
26
        foreach ($editors as $editor) {
27
            $this->editor($editor);
28
        }
29
30
        return $this;
31
    }
32
33
    /**
34
     * Integrate with DataTables Editor.
35
     *
36
     * @param array|Editor $fields
37
     * @return $this
38
     * @see https://editor.datatables.net/
39
     * @throws \Exception
40
     */
41
    public function editor($fields)
42
    {
43
        $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...
44
45
        $editor = $this->newEditor($fields);
46
47
        $this->editors[] = $editor;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param array|Editor $fields
54
     * @return array|Editor
55
     * @throws \Exception
56
     */
57
    protected function newEditor($fields)
58
    {
59
        if ($fields instanceof Editor) {
60
            $editor = $fields;
61
        } else {
62
            $editor = new Editor;
63
            $editor->fields($fields);
64
        }
65
66
        if (! $editor->table) {
0 ignored issues
show
Documentation introduced by
The property table does not exist on object<Yajra\DataTables\Html\Editor\Editor>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
            $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...
68
        }
69
70
        if (! $editor->ajax) {
0 ignored issues
show
Documentation introduced by
The property ajax does not exist on object<Yajra\DataTables\Html\Editor\Editor>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
            $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...
72
        }
73
74
        return $editor;
75
    }
76
}
77