Completed
Push — master ( 5e0e31...9ce591 )
by Christopher
01:24
created

SetupCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 9
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 2
A seed() 9 24 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 Chriscreates\Blog\Console\Commands;
4
5
use Chriscreates\Blog\Comment;
6
use Chriscreates\Blog\Post;
7
use Chriscreates\Blog\Tag;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\DetectsApplicationNamespace;
10
11
class SetupCommand extends Command
12
{
13
    use DetectsApplicationNamespace;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'blog:setup {--data : Specifies that demo data should be seeded}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Scaffold basic blog views and routes';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        // TODO:
37
        // Controllers
38
        // Routes
39
        // Views?
40
41
        // Optionally seed the database with demo data
42
        if ($this->option('data')) {
43
            $this->seed();
44
        }
45
46
        $this->info('Setup complete');
47
    }
48
49
    /**
50
     * Run the demo data seeder.
51
     *
52
     * @return void
53
     */
54
    private function seed()
55
    {
56
        // TODO: Check that blog:install has been run before this blog:setup
57
58
        // Publish factories
59
        $files = collect(
0 ignored issues
show
Unused Code introduced by
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
            array_diff(scandir(__DIR__.'/../../../database/factories/'), array('.', '..'))
61
        )->each(function ($file) {
62
            copy(__DIR__."/../../../database/factories/{$file}", database_path('factories')."/{$file}");
63
        });
64
65
        // Create data
66
        factory(Post::class, 20)
67
         ->create()
68 View Code Duplication
         ->each(function ($post) {
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...
69
             $post->tags()->sync(
70
                 factory(Tag::class, 4)->create()->pluck('id')->toArray()
71
             );
72
73
             $post->comments()->save(factory(Comment::class)->make());
74
             $post->comments()->save(factory(Comment::class)->make());
75
             $post->comments()->save(factory(Comment::class)->make());
76
         });
77
    }
78
}
79