AddonPaths   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 225
Duplicated Lines 23.56 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 53
loc 225
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A eager() 0 9 1
A deferred() 0 9 1
A __construct() 0 5 1
B all() 0 39 6
A core() 10 10 2
A vendorAddons() 0 12 3
A shared() 10 10 2
A application() 10 10 2
A testing() 10 10 3
A directory() 10 10 2
A configured() 0 15 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 namespace Anomaly\Streams\Platform\Addon;
2
3
use Anomaly\Streams\Platform\Application\Application;
4
use Illuminate\Contracts\Config\Repository;
5
6
/**
7
 * Class AddonPaths
8
 *
9
 * @link    http://pyrocms.com/
10
 * @author  PyroCMS, Inc. <[email protected]>
11
 * @author  Ryan Thompson <[email protected]>
12
 */
13
class AddonPaths
14
{
15
16
    /**
17
     * The config repository.
18
     *
19
     * @var Repository
20
     */
21
    protected $config;
22
23
    /**
24
     * The stream application.
25
     *
26
     * @var Application
27
     */
28
    protected $application;
29
30
    /**
31
     * Create a new AddonPaths instance.
32
     *
33
     * @param Application $application
34
     * @param Repository $config
35
     */
36
    public function __construct(Application $application, Repository $config)
37
    {
38
        $this->config      = $config;
39
        $this->application = $application;
40
    }
41
42
43
    /**
44
     * Return all addon paths in a given folder.
45
     *
46
     * @return array
47
     */
48
    public function all()
49
    {
50
        $eager    = $this->eager();
51
        $deferred = $this->deferred();
52
53
        $core        = $this->core() ?: [];
54
        $shared      = $this->shared() ?: [];
55
        $application = $this->application() ?: [];
56
57
        // Testing only addons.
58
        $testing = $this->testing() ?: [];
59
60
        // Other configured addons.
61
        $configured = $this->configured() ?: [];
62
63
        /*
64
         * Merge the eager and deferred
65
         * onto the front and back of
66
         * the paths respectively.
67
         */
68
69
        return array_unique(
70
            array_merge(
71
                $eager,
72
                array_reverse(
73
                    array_unique(
74
                        array_reverse(
75
                            array_merge(
76
                                array_filter(
77
                                    array_merge($core, $shared, $application, $configured, $testing)
78
                                ),
79
                                $deferred
80
                            )
81
                        )
82
                    )
83
                )
84
            )
85
        );
86
    }
87
88
    /**
89
     * Return paths to eager loaded addons.
90
     *
91
     * @return array
92
     */
93
    protected function eager()
94
    {
95
        return array_map(
96
            function ($path) {
97
                return base_path($path);
98
            },
99
            $this->config->get('streams::addons.eager', [])
100
        );
101
    }
102
103
    /**
104
     * Return paths to deferred addons.
105
     *
106
     * @return array
107
     */
108
    protected function deferred()
109
    {
110
        return array_map(
111
            function ($path) {
112
                return base_path($path);
113
            },
114
            $this->config->get('streams::addons.deferred', [])
115
        );
116
    }
117
118
    /**
119
     * Return all core addon paths in a given folder.
120
     *
121
     * @return bool
122
     */
123 View Code Duplication
    public function core()
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...
124
    {
125
        $path = base_path('core');
126
127
        if (!is_dir($path)) {
128
            return false;
129
        }
130
131
        return $this->vendorAddons(glob("{$path}/*", GLOB_ONLYDIR));
132
    }
133
134
    /**
135
     * Return vendor addons of a given type.
136
     *
137
     * @param $directories
138
     * @return array
139
     */
140
    protected function vendorAddons($directories)
141
    {
142
        $paths = [];
143
144
        foreach ($directories as $vendor) {
145
            foreach (glob("{$vendor}/*", GLOB_ONLYDIR) as $addon) {
146
                $paths[] = $addon;
147
            }
148
        }
149
150
        return $paths;
151
    }
152
153
    /**
154
     * Return all shared addon paths in a given folder.
155
     *
156
     * @return bool
157
     */
158 View Code Duplication
    public function shared()
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...
159
    {
160
        $path = base_path('addons/shared');
161
162
        if (!is_dir($path)) {
163
            return false;
164
        }
165
166
        return $this->vendorAddons(glob("{$path}/*", GLOB_ONLYDIR));
167
    }
168
169
    /**
170
     * Return all application addon paths in a given folder.
171
     *
172
     * @return bool
173
     */
174 View Code Duplication
    public function application()
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...
175
    {
176
        $path = base_path('addons/' . $this->application->getReference());
177
178
        if (!is_dir($path)) {
179
            return false;
180
        }
181
182
        return $this->vendorAddons(glob("{$path}/*", GLOB_ONLYDIR));
183
    }
184
185
    /**
186
     * Return paths to testing only addons.
187
     *
188
     * @return array|bool
189
     */
190 View Code Duplication
    protected function testing()
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...
191
    {
192
        $path = base_path('vendor/anomaly/streams-platform/addons');
193
194
        if (!is_dir($path) || env('APP_ENV') !== 'testing') {
195
            return false;
196
        }
197
198
        return $this->vendorAddons(glob("{$path}/*", GLOB_ONLYDIR));
199
    }
200
201
    /**
202
     * Return paths to testing only addons.
203
     *
204
     * @return array|bool
205
     */
206 View Code Duplication
    protected function directory($directory)
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...
207
    {
208
        $path = base_path($directory);
209
210
        if (!is_dir($path)) {
211
            return false;
212
        }
213
214
        return $this->vendorAddons(glob("{$path}/*", GLOB_ONLYDIR));
215
    }
216
217
    /**
218
     * Return paths to configured addons.
219
     *
220
     * @return array|bool
221
     */
222
    protected function configured()
223
    {
224
        $paths = array_map(
225
            function ($path) {
226
                return base_path($path);
227
            },
228
            $this->config->get('streams::addons.paths', [])
229
        );
230
231
        foreach ($this->config->get('streams::addons.directories', []) as $directory) {
232
            $paths = array_merge($paths, (array)$this->directory(trim($directory, '\\/')));
233
        }
234
235
        return array_unique(array_filter($paths));
236
    }
237
}
238