CreateCategory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 76
loc 76
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntity() 10 10 2
B handle() 30 30 3

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 Moo\FlashCard\Command;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Eloquent\Model;
7
use Moo\FlashCard\Entity\Category;
8
use Moo\FlashCard\Traits\AskAndValidate;
9
10
/**
11
 * CreateCardCommand is a command line class for creating a new card.
12
 *
13
 * @author Mohamed Alsharaf <[email protected]>
14
 */
15 View Code Duplication
class CreateCategory extends Command
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    use AskAndValidate;
18
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'flashcard:category {--A|active= : If set, the category is going to be active.}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a category';
32
33
    /**
34
     * @var Category
35
     */
36
    protected $entity;
37
38
39
    /**
40
     * Instance of category model
41
     *
42
     * @return Model
43
     */
44 2
    protected function getEntity(): Model
45
    {
46 2
        if (is_null($this->entity)) {
47 2
            $this->entity = new Category([
48 2
                'active' => (bool) $this->option('active'),
49
            ]);
50
        }
51
52 2
        return $this->entity;
53
    }
54
55
    /**
56
     * Execute the command line to create a new category.
57
     *
58
     * @return int
59
     */
60 2
    public function handle()
61
    {
62
        // Ask for category title
63 2
        $this->askWithValidation('Please enter category title', 'title');
64
65
        // Ask for category content
66 2
        $this->askWithValidation('Please enter category description', 'description');
67
68
        // Ask for category color
69 2
        $this->askWithValidation('Please enter category color', 'color');
70
71
        // Ask for card category
72 2
        $choices = Category::all()->pluck('title', 'id')->prepend('Root Category')->toArray();
73 2
        if (count($choices) > 0) {
74 2
            $category = $this->choice('Please select category parent or enter "0" for root category', $choices, key($choices));
75
76
            // Set parent category id if value not "0"
77 2
            if ($category !== '0') {
78 2
                $this->getEntity()->fill([
79 2
                    'parent' => array_search($category, $choices, true),
80
                ]);
81
            }
82
        }
83
84
        // Save entity and display message
85 2
        $this->getEntity()->save();
86 2
        $this->info('Voila... You have created a new category.');
87
88 2
        return 0;
89
    }
90
}
91