Completed
Pull Request — master (#29)
by
unknown
03:46
created

ExportStateTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 20
loc 50
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExport() 20 20 1
A postExport() 0 19 2

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 namespace Distilleries\Expendable\States;
2
3
use \FormBuilder;
4
use Illuminate\Http\Request;
5
use Distilleries\Expendable\Exports\BaseExport;
6
7
trait ExportStateTrait {
8
9
    protected $export_form = 'Distilleries\Expendable\Http\Forms\Export\ExportForm';
10
    // ------------------------------------------------------------------------------------------------
11
    // ------------------------------------------------------------------------------------------------
12
    // ------------------------------------------------------------------------------------------------
13
14 2 View Code Duplication
    public function getExport()
15
    {
16 2
        $form = FormBuilder::create($this->export_form, [
17 2
            'model' => $this->model
0 ignored issues
show
Bug introduced by
The property model 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...
18
        ]);
19
20 2
        $form_content = view('expendable::admin.form.components.formgenerator.export', [
21 2
            'form' => $form
22
        ]);
23 2
        $content      = view('expendable::admin.form.state.form', [
24
25 2
        ]);
26
27 2
        $this->layoutManager->add([
0 ignored issues
show
Bug introduced by
The property layoutManager 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...
28 2
            'form'=>$form_content,
29 2
            'content'=>$content,
30
        ]);
31
32 2
        return $this->layoutManager->render();
33
    }
34
35
    // ------------------------------------------------------------------------------------------------
36
37 10
    public function postExport(Request $request)
38
    {
39
40 10
        $form = FormBuilder::create($this->export_form, [
41 10
            'model' => $this->model
42
        ]);
43
44
45 10
        if ($form->hasError())
46
        {
47 6
            return $form->validateAndRedirectBack();
48
        }
49
50
51 4
        $data = $request->all();
52 4
        $filename = $data['range']['start'] . ' ' . $data['range']['end'] . '.' . $data['type'];
53
54 4
        return (new BaseExport($this->model, $data))->export($filename);
55
    }
56
}