Passed
Push — master ( ba9ea6...f4d366 )
by Thomas
02:08 queued 11s
created

BackpackCrudRequestService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 8
loc 52
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5
ccs 0
cts 30
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A call() 0 12 1
A setRules() 0 6 1
A insertRulesInGeneratedRequest() 8 8 1
A getRulesAsString() 0 4 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 Webfactor\Laravel\Generators\Services;
4
5
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
6
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8
9
class BackpackCrudRequestService extends ServiceAbstract implements ServiceInterface
10
{
11
    protected $relativeToBasePath = 'app/Http/Requests/Admin';
12
13
    private $rules = [];
14
15
    public function call()
16
    {
17
        $this->command->call('make:crud-request', [
18
            'name' => $this->getName($this->command->entity),
19
        ]);
20
21
        $this->addLatestFileToIdeStack();
22
23
        $this->setRules();
24
25
        $this->insertRulesInGeneratedRequest();
26
    }
27
28
    /**
29
     * @param string $entity
30
     * @return string
31
     */
32
    public function getName(string $entity): string
33
    {
34
        return ucfirst($entity);
35
    }
36
37
    private function setRules(): void
38
    {
39
        $this->command->schema->getStructure()->each(function ($fieldType) {
40
            $this->rules[$fieldType->getName()] = $fieldType->getRule();
41
        });
42
    }
43
44 View Code Duplication
    private function insertRulesInGeneratedRequest(): void
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...
45
    {
46
        $requestFile = end($this->command->filesToBeOpened);
47
48
        $request = $this->filesystem->get($requestFile);
49
        $request = str_replace('__rules__', $this->getRulesAsString(), $request);
50
        $this->filesystem->put($requestFile, $request);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    private function getRulesAsString(): string
57
    {
58
        return ShortSyntaxArray::parse($this->rules);
59
    }
60
}
61