Completed
Pull Request — master (#340)
by
unknown
05:31
created

WriteAddonSeeder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 127
Duplicated Lines 17.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 127
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B handle() 0 25 1
A getUses() 11 11 1
A getCalls() 11 11 1
A getName() 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 namespace Anomaly\Streams\Platform\Addon\Console\Command;
2
3
use Illuminate\Filesystem\Filesystem;
4
use Anomaly\Streams\Platform\Support\Parser;
5
use Anomaly\Streams\Platform\Stream\StreamCollection;
6
7
/**
8
 * Class WriteAddonSeeder
9
 *
10
 * @author PyroCMS, Inc. <[email protected]>
11
 * @author Ryan Thompson <[email protected]>
12
 *
13
 * @link   http://pyrocms.com/
14
 */
15
class WriteAddonSeeder
16
{
17
18
    /**
19
     * The addon path.
20
     *
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * The addon type.
27
     *
28
     * @var string
29
     */
30
    private $type;
31
32
    /**
33
     * The addon slug.
34
     *
35
     * @var string
36
     */
37
    private $slug;
38
39
    /**
40
     * The vendor slug.
41
     *
42
     * @var string
43
     */
44
    private $vendor;
45
46
    /**
47
     * Create a new WriteAddonSeeder instance.
48
     *
49
     * @param string           $path    The path
50
     * @param string           $type    The type
51
     * @param string           $slug    The slug
52
     * @param string           $vendor  The vendor
53
     * @param StreamCollection $streams The streams
54
     */
55
    public function __construct($path, $type, $slug, $vendor, StreamCollection $streams)
56
    {
57
        $this->path    = $path;
58
        $this->slug    = $slug;
59
        $this->type    = $type;
60
        $this->vendor  = $vendor;
61
        $this->streams = $streams;
0 ignored issues
show
Bug introduced by
The property streams 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...
62
    }
63
64
    /**
65
     * Handle the command.
66
     *
67
     * @param Parser     $parser
68
     * @param Filesystem $filesystem
69
     */
70
    public function handle(Parser $parser, Filesystem $filesystem)
71
    {
72
        $slug   = ucfirst(camel_case($this->slug));
73
        $type   = ucfirst(camel_case($this->type));
74
        $vendor = ucfirst(camel_case($this->vendor));
75
76
        $addon     = $slug.$type;
77
        $class     = $addon.'Seeder';
78
        $namespace = "{$vendor}\\{$addon}";
79
        $uses      = $this->getUses($namespace)->implode("\n");
80
        $calls     = $this->getCalls()->implode("\n        ");
81
82
        $path = "{$this->path}/src/{$class}.php";
83
84
        $template = $filesystem->get(
85
            base_path('vendor/anomaly/streams-platform/resources/stubs/addons/seeder.stub')
86
        );
87
88
        $filesystem->makeDirectory(dirname($path), 0755, true, true);
89
90
        $filesystem->put($path, $parser->parse(
91
            $template,
92
            compact('namespace', 'class', 'uses', 'calls')
93
        ));
94
    }
95
96
    /**
97
     * Gets the uses.
98
     *
99
     * @param  string     $namespace The namespace
100
     * @return Collection The uses.
101
     */
102 View Code Duplication
    public function getUses($namespace)
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...
103
    {
104
        return $this->streams->map(
105
            function ($stream) use ($namespace)
106
            {
107
                $name = ucfirst(str_singular($stream->getSlug()));
108
109
                return "use {$namespace}\\{$name}\\{$name}Seeder;";
110
            }
111
        );
112
    }
113
114
    /**
115
     * Gets the calls.
116
     *
117
     * @return Collection The calls.
118
     */
119 View Code Duplication
    public function getCalls()
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...
120
    {
121
        return $this->streams->map(
122
            function ($stream)
123
            {
124
                $name = ucfirst(str_singular($stream->getSlug()));
125
126
                return "\$this->call({$name}Seeder::class);";
127
            }
128
        );
129
    }
130
131
    /**
132
     * Gets the name.
133
     *
134
     * @param  string $slug The slug
135
     * @return string The name.
136
     */
137
    public function getName($slug)
138
    {
139
        return ucfirst(str_singular($slug));
140
    }
141
}
142