Completed
Push — master ( 796a9c...f750b3 )
by Arjay
118:10 queued 75:35
created

Options   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 17.48 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 18
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A yesNo() 9 9 1
A model() 0 13 2
A table() 0 12 2
A trueFalse() 9 9 1
A append() 0 4 1
A prepend() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\DataTables\Html\Editor;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Database\Eloquent\Builder;
8
9
class Options extends Collection
10
{
11
    /**
12
     * Return a Yes/No options.
13
     *
14
     * @return Options
15
     */
16 View Code Duplication
    public static function yesNo()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        $data = [
19
            ['label' => __('Yes'), 'value' => true],
20
            ['label' => __('No'), 'value' => false],
21
        ];
22
23
        return new static($data);
24
    }
25
26
    /**
27
     * Get options from a model.
28
     *
29
     * @param mixed $model
30
     * @param string $value
31
     * @param string $key
32
     * @return Collection
33
     */
34
    public static function model($model, $value, $key = 'id')
35
    {
36
        if (! $model instanceof Builder) {
37
            $model = $model::query();
38
        }
39
40
        return $model->get()->map(function ($model) use ($value, $key) {
41
            return [
42
                'value' => $model->{$key},
43
                'label' => $model->{$value},
44
            ];
45
        });
46
    }
47
48
    /**
49
     * Get options from a table.
50
     *
51
     * @param mixed $table
52
     * @param string $value
53
     * @param string $key
54
     * @param \Closure $whereCallback
55
     * @param string|null $key
56
     * @return Collection
57
     */
58
    public static function table($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null)
59
    {
60
        $query = DB::connection($connection)
61
                   ->table($table)
62
                   ->select("{$value} as label", "{$key} as value");
63
64
        if ($whereCallback) {
65
            $query->where($whereCallback);
66
        }
67
68
        return $query->get();
69
    }
70
71
    /**
72
     * Return a True/False options.
73
     *
74
     * @return Options
75
     */
76 View Code Duplication
    public static function trueFalse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $data = [
79
            ['label' => __('True'), 'value' => true],
80
            ['label' => __('False'), 'value' => false],
81
        ];
82
83
        return new static($data);
84
    }
85
86
    /**
87
     * Push an item onto the end of the collection.
88
     *
89
     * @param mixed $value
90
     * @param mixed $key
91
     * @return Options
92
     */
93
    public function append($value, $key)
94
    {
95
        return $this->push(['label' => $value, 'value' => $key]);
96
    }
97
98
    /**
99
     * Push an item onto the beginning of the collection.
100
     *
101
     * @param  mixed $value
102
     * @param  mixed $key
103
     * @return $this
104
     */
105
    public function prepend($value, $key = null)
106
    {
107
        $data = ['label' => $value, 'value' => $key];
108
109
        return parent::prepend($data);
110
    }
111
}
112