Completed
Pull Request — master (#107)
by Tim
48:08 queued 39:33
created

SettingsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 11
Bugs 5 Features 4
Metric Value
wmc 5
c 11
b 5
f 4
lcom 0
cbo 3
dl 8
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B basicView() 0 30 1
A backupView() 0 9 1
A generalUpdate() 8 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
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use App\Http\Requests;
8
use App\Http\Controllers\Controller;
9
10
11
class SettingsController extends Controller
12
{
13
    /**
14
     * SettingsController Constructor 
15
     */ 
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('lang');
20
    }
21
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function basicView()
28
    {
29
30
        $data['date_formats'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
31
            'Y-m-d' => '2010-12-23',
32
            'm-d-Y' => '12-23-2010',
33
            'd-m-Y' => '23-12-2010',
34
            'Y/m/d' => '2010/12/23',
35
            'm/d/Y' => '12/23/2010',
36
            'd/m/Y' => '23/12/2010',
37
            'Y.m.d' => '2010.12.23',
38
            'd.m.Y' => '23.12.2010',
39
            'm.d.Y' => '12.23.2010',
40
        );
41
42
        $data['time_formats'] = array (
43
            'H:i' => '23:00',
44
            'h:ia' => '11:00pm',
45
            'h:iA' => '11:00PM',
46
            'h:i a' => '11:00 pm',
47
            'h:i A' => '11:00 PM',
48
        );
49
    
50
        $data['title'] = config('timecontrol.title');
51
        $data['email'] = config('timecontrol.email');
52
        $data['date'] = config('timecontrol.date');
53
        $data['time'] = config('timecontrol.time');
54
55
        return view('settings/basic', $data);
56
    }
57
58
    /**
59
     * Update the basic settings.
60
     *
61
     * @param  \Illuminate\Http\Request  $request     
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function generalUpdate(Request $request)
65
    {
66
67
        $config = new \Larapack\ConfigWriter\Repository('timecontrol');
68
        $config->set('title', $request->get('title')); 
69
        $config->set('email', $request->get('email')); 
70
        $config->set('date', $request->get('date')); 
71
        $config->set('time', $request->get('time'));
72
        $config->save(); 
73
74 View Code Duplication
        if ($config) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            sleep(3);
76
            session()->flash('message', 'Settings have been saved');
77
            return redirect('settings/general');
78
        } else {
79
            session()->flash('message', 'Settings have not been saved, please verify');
80
            return redirect('settings/general');
81
        }
82
    }
83
84
    /**
85
     * Display a form to configure the backup settings.
86
     *
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function backupView()
90
    {
91
        $data['include'] = config('laravel-backup.backup.source.files.include');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
92
        $data['exclude'] = config('laravel-backup.backup.source.files.exclude');
93
94
        $data['keepAllBackupsForDays'] = config('laravel-backup.cleanup.defaultStrategy.keepAllBackupsForDays');
95
96
        return view('settings/backup', $data);
97
    }
98
}
99