Generator   F
last analyzed

Complexity

Total Complexity 66

Size/Duplication

Total Lines 455
Duplicated Lines 2.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 66
lcom 1
cbo 3
dl 13
loc 455
rs 3.12
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBaseAdminTemplate() 0 4 1
A setBaseAdminTemplate() 0 4 1
A addBuilder() 0 15 1
C mergeParameters() 0 56 17
B mergeConfiguration() 0 22 6
A recursiveReplace() 0 19 5
C applyActionsBuilderDefaults() 0 59 15
A setColumnClass() 0 4 1
A getColumnClass() 0 4 1
A setYamlConfig() 0 7 1
A getFromYaml() 0 8 1
A setFieldGuesser() 0 4 1
A getFieldGuesser() 0 4 1
A setBaseController() 0 4 1
A getBaseController() 0 4 1
A setBaseGeneratorName() 0 4 1
A getBaseGeneratorName() 0 4 1
A getGeneratedControllerFolder() 0 4 1
A setRouter() 0 4 1
A getRouter() 0 4 1
A setBundleConfig() 0 4 1
A getBundleConfig() 0 4 1
A getFromBundleConfig() 0 8 1
A getFromArray() 13 13 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Generator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Generator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Builder;
4
5
/**
6
 * @author Cedric LOMBARDOT
7
 * @author Piotr Gołębiewski <[email protected]>
8
 */
9
use Symfony\Component\Yaml\Yaml;
10
use TwigGenerator\Builder\Generator as TwigGeneratorGenerator;
11
use TwigGenerator\Builder\BuilderInterface;
12
use Symfony\Component\Routing\RouterInterface;
13
14
class Generator extends TwigGeneratorGenerator
15
{
16
    const TEMP_DIR_PREFIX = 'Admingenerator';
17
18
    /**
19
     * @var array $yaml The yaml array.
20
     */
21
    protected $yaml;
22
23
    /**
24
     * @var string $baseController The base controller.
25
     */
26
    protected $baseController;
27
28
    /**
29
     * @var string $columnClass The column class.
30
     */
31
    protected $columnClass = 'Admingenerator\GeneratorBundle\Generator\Column';
32
33
    /**
34
     * @var string $baseAdminTemplate The base admin template.
35
     */
36
    protected $baseAdminTemplate = 'AdmingeneratoroldThemeBundle::base.html.twig';
37
38
    /**
39
     * @var string $baseGeneratorName The base generator name.
40
     */
41
    protected $baseGeneratorName;
42
43
    /**
44
     * @var array $bundleConfig Generator bundle config.
45
     */
46
    protected $bundleConfig;
47
48
    /**
49
     * @var \Symfony\Component\Routing\RouterInterface
50
     */
51
    protected $router;
52
53
    /**
54
     * Init a new generator and automatically define the base of tempDir
55
     *
56
     * @param string $cacheDir
57
     * @param Filepath $yaml
58
     */
59
    public function __construct($cacheDir, $yaml)
60
    {
61
        parent::__construct($cacheDir);
62
        $this->setYamlConfig(Yaml::parse(file_get_contents($yaml)));
63
    }
64
65
    /**
66
     * @return string The base admin template.
67
     */
68
    public function getBaseAdminTemplate()
69
    {
70
        return $this->baseAdminTemplate;
71
    }
72
73
    /**
74
     * @param string $baseAdminTemplate The base admin template.
75
     * @return void
76
     */
77
    public function setBaseAdminTemplate($baseAdminTemplate)
78
    {
79
        $this->baseAdminTemplate = $baseAdminTemplate;
80
    }
81
82
    /**
83
     * Add a builder
84
     * @param BuilderInterface $builder
85
     *
86
     * @return void
87
     */
88
    public function addBuilder(BuilderInterface $builder)
89
    {
90
        parent::addBuilder($builder);
91
92
        $params = $this->getFromYaml('params', array());
93
        $params = $this->applyActionsBuilderDefaults($params);
94
95
        $params = $this->mergeParameters(
96
            $params,
97
            $this->getFromYaml(sprintf('builders.%s.params', $builder->getYamlKey()), array())
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TwigGenerator\Builder\BuilderInterface as the method getYamlKey() does only exist in the following implementations of said interface: Admingenerator\Generator...er\Admin\ActionsBuilder, Admingenerator\Generator...in\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...ilder\Admin\BaseBuilder, Admingenerator\Generator...ilder\Admin\EditBuilder, Admingenerator\Generator...Admin\EditBuilderAction, Admingenerator\Generator...min\EditBuilderTemplate, Admingenerator\Generator...r\Admin\EditBuilderType, Admingenerator\Generator...dmin\EmptyBuilderAction, Admingenerator\Generator...lder\Admin\ExcelBuilder, Admingenerator\Generator...dmin\ExcelBuilderAction, Admingenerator\Generator...dmin\FiltersBuilderType, Admingenerator\Generator...ilder\Admin\ListBuilder, Admingenerator\Generator...Admin\ListBuilderAction, Admingenerator\Generator...min\ListBuilderTemplate, Admingenerator\Generator...Admin\NestedListBuilder, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...uilder\Admin\NewBuilder, Admingenerator\Generator...\Admin\NewBuilderAction, Admingenerator\Generator...dmin\NewBuilderTemplate, Admingenerator\Generator...er\Admin\NewBuilderType, Admingenerator\Generator...ilder\Admin\ShowBuilder, Admingenerator\Generator...Admin\ShowBuilderAction, Admingenerator\Generator...min\ShowBuilderTemplate, Admingenerator\GeneratorBundle\Builder\BaseBuilder, Admingenerator\Generator...DM\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...neODM\EditBuilderAction, Admingenerator\Generator...ODM\EditBuilderTemplate, Admingenerator\Generator...rineODM\EditBuilderType, Admingenerator\Generator...eODM\ExcelBuilderAction, Admingenerator\Generator...eODM\FiltersBuilderType, Admingenerator\Generator...neODM\ListBuilderAction, Admingenerator\Generator...ODM\ListBuilderTemplate, Admingenerator\Generator...ineODM\NewBuilderAction, Admingenerator\Generator...eODM\NewBuilderTemplate, Admingenerator\Generator...trineODM\NewBuilderType, Admingenerator\Generator...neODM\ShowBuilderAction, Admingenerator\Generator...ODM\ShowBuilderTemplate, Admingenerator\Generator...ne\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...trine\EditBuilderAction, Admingenerator\Generator...ine\EditBuilderTemplate, Admingenerator\Generator...octrine\EditBuilderType, Admingenerator\Generator...rine\ExcelBuilderAction, Admingenerator\Generator...rine\FiltersBuilderType, Admingenerator\Generator...trine\ListBuilderAction, Admingenerator\Generator...ine\ListBuilderTemplate, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...ctrine\NewBuilderAction, Admingenerator\Generator...rine\NewBuilderTemplate, Admingenerator\Generator...Doctrine\NewBuilderType, Admingenerator\Generator...trine\ShowBuilderAction, Admingenerator\Generator...ine\ShowBuilderTemplate, Admingenerator\Generator...el\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...ropel\EditBuilderAction, Admingenerator\Generator...pel\EditBuilderTemplate, Admingenerator\Generator...\Propel\EditBuilderType, Admingenerator\Generator...opel\ExcelBuilderAction, Admingenerator\Generator...opel\FiltersBuilderType, Admingenerator\Generator...ropel\ListBuilderAction, Admingenerator\Generator...pel\ListBuilderTemplate, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...Propel\NewBuilderAction, Admingenerator\Generator...opel\NewBuilderTemplate, Admingenerator\Generator...r\Propel\NewBuilderType, Admingenerator\Generator...ropel\ShowBuilderAction, Admingenerator\Generator...pel\ShowBuilderTemplate.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
        );
99
100
        $builder->setVariables($params);
101
        $builder->setColumnClass($this->getColumnClass());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TwigGenerator\Builder\BuilderInterface as the method setColumnClass() does only exist in the following implementations of said interface: Admingenerator\Generator...er\Admin\ActionsBuilder, Admingenerator\Generator...in\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...ilder\Admin\BaseBuilder, Admingenerator\Generator...ilder\Admin\EditBuilder, Admingenerator\Generator...Admin\EditBuilderAction, Admingenerator\Generator...min\EditBuilderTemplate, Admingenerator\Generator...r\Admin\EditBuilderType, Admingenerator\Generator...dmin\EmptyBuilderAction, Admingenerator\Generator...lder\Admin\ExcelBuilder, Admingenerator\Generator...dmin\ExcelBuilderAction, Admingenerator\Generator...dmin\FiltersBuilderType, Admingenerator\Generator...ilder\Admin\ListBuilder, Admingenerator\Generator...Admin\ListBuilderAction, Admingenerator\Generator...min\ListBuilderTemplate, Admingenerator\Generator...Admin\NestedListBuilder, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...uilder\Admin\NewBuilder, Admingenerator\Generator...\Admin\NewBuilderAction, Admingenerator\Generator...dmin\NewBuilderTemplate, Admingenerator\Generator...er\Admin\NewBuilderType, Admingenerator\Generator...ilder\Admin\ShowBuilder, Admingenerator\Generator...Admin\ShowBuilderAction, Admingenerator\Generator...min\ShowBuilderTemplate, Admingenerator\Generator...DM\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...neODM\EditBuilderAction, Admingenerator\Generator...ODM\EditBuilderTemplate, Admingenerator\Generator...rineODM\EditBuilderType, Admingenerator\Generator...eODM\ExcelBuilderAction, Admingenerator\Generator...eODM\FiltersBuilderType, Admingenerator\Generator...neODM\ListBuilderAction, Admingenerator\Generator...ODM\ListBuilderTemplate, Admingenerator\Generator...ineODM\NewBuilderAction, Admingenerator\Generator...eODM\NewBuilderTemplate, Admingenerator\Generator...trineODM\NewBuilderType, Admingenerator\Generator...neODM\ShowBuilderAction, Admingenerator\Generator...ODM\ShowBuilderTemplate, Admingenerator\Generator...ne\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...trine\EditBuilderAction, Admingenerator\Generator...ine\EditBuilderTemplate, Admingenerator\Generator...octrine\EditBuilderType, Admingenerator\Generator...rine\ExcelBuilderAction, Admingenerator\Generator...rine\FiltersBuilderType, Admingenerator\Generator...trine\ListBuilderAction, Admingenerator\Generator...ine\ListBuilderTemplate, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...ctrine\NewBuilderAction, Admingenerator\Generator...rine\NewBuilderTemplate, Admingenerator\Generator...Doctrine\NewBuilderType, Admingenerator\Generator...trine\ShowBuilderAction, Admingenerator\Generator...ine\ShowBuilderTemplate, Admingenerator\Generator...el\ActionsBuilderAction, Admingenerator\Generator...\ActionsBuilderTemplate, Admingenerator\Generator...ropel\EditBuilderAction, Admingenerator\Generator...pel\EditBuilderTemplate, Admingenerator\Generator...\Propel\EditBuilderType, Admingenerator\Generator...opel\ExcelBuilderAction, Admingenerator\Generator...opel\FiltersBuilderType, Admingenerator\Generator...ropel\ListBuilderAction, Admingenerator\Generator...pel\ListBuilderTemplate, Admingenerator\Generator...NestedListBuilderAction, Admingenerator\Generator...stedListBuilderTemplate, Admingenerator\Generator...Propel\NewBuilderAction, Admingenerator\Generator...opel\NewBuilderTemplate, Admingenerator\Generator...r\Propel\NewBuilderType, Admingenerator\Generator...ropel\ShowBuilderAction, Admingenerator\Generator...pel\ShowBuilderTemplate.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
    }
103
104
    /**
105
     * Merge parameters from global definition with builder definition
106
     * Fields and actions have special behaviors:
107
     *     - fields are merged and all global fields are still available
108
     *     from a builder
109
     *     - actions depend of builder. List of available actions come
110
     *     from builder, configuration is a merge between builder configuration
111
     *     and global configuration
112
     *
113
     * @param  array $global
114
     * @param  array $builder
115
     *
116
     * @return array
117
     */
118
    protected function mergeParameters(array $global, array $builder)
119
    {
120
        foreach ($global as $param => &$value) {
121
            if (!array_key_exists($param, $builder)) {
122
                continue;
123
            }
124
125
            if (!in_array($param, array('fields', 'actions', 'object_actions', 'batch_actions'))) {
126
                if (is_array($value)) {
127
                    $value = $this->recursiveReplace($value, $builder[$param]);
128
                } else {
129
                    $value = $builder[$param];
130
                }
131
132
                continue;
133
            }
134
135
            // Grab builder configuration only if defined
136
            if (is_null($builder[$param])) {
137
                continue;
138
            }
139
140
            $configurations = array();
141
            foreach ($builder[$param] as $name => $configuration) {
142
                if (!is_array($configuration) && !is_null($configuration)) {
143
                    throw new \InvalidArgumentException(
144
                        sprintf('Invalid %s "%s" builder definition for %s', $param, $name, $this->getFromYaml('params.model'))
145
                    );
146
                }
147
148
                if (!is_null($value) && array_key_exists($name, $value)) {
149
                    $configurations[$name] = $configuration
150
                        ? $this->mergeConfiguration($value[$name], $configuration) // Override definition
151
                        : $value[$name]; // Configuration is null => use global definition
152
                } else {
153
                    // New definition (new field, new action) from builder
154
                    $configurations[$name] = $configuration;
155
                }
156
            }
157
158
            if (in_array($param, array('actions', 'object_actions', 'batch_actions'))) {
159
                // Actions list comes from builder
160
                $value = $configurations;
161
            } else {
162
                // All fields are still available in a builder
163
                $value = array_merge($value ?:array(), $configurations);
164
            }
165
        }
166
167
        // If builder doesn't have actions/object_actions/batch_actions remove it from merge.
168
        $global['actions'] = array_key_exists('actions', $builder) ? $global['actions'] : array();
169
        $global['object_actions'] = array_key_exists('object_actions', $builder) ? $global['object_actions'] : array();
170
        $global['batch_actions'] = array_key_exists('batch_actions', $builder) ? $global['batch_actions'] : array();
171
172
        return array_merge($global, array_diff_key($builder, $global));
173
    }
174
175
    /**
176
     * Merge configuration on a single level
177
     *
178
     * @param  array $global
179
     * @param  array $builder
180
     *
181
     * @return array
182
     */
183
    protected function mergeConfiguration(array $global, array $builder)
184
    {
185
        foreach ($global as $name => &$value) {
186
            if (!array_key_exists($name, $builder) || is_null($builder[$name])) {
187
                continue;
188
            }
189
190
            if (!is_array($value)) {
191
                $value = $builder[$name];
192
193
                continue;
194
            }
195
196
            if (!is_array($builder[$name])) {
197
                throw new \InvalidArgumentException('Invalid generator');
198
            }
199
200
            $value = array_replace($value, $builder[$name]);
201
        }
202
203
        return array_merge($global, array_diff_key($builder, $global));
204
    }
205
206
    /**
207
     * Recursively replaces Base array values with Replacement array values
208
     * while keeping indexes of Replacement array
209
     *
210
     * @param array $base        Base array
211
     * @param array $replacement Replacement array
212
     *
213
     * @return array
214
     */
215
    protected function recursiveReplace($base, $replacement)
216
    {
217
        $replace_values_recursive = function (array $array, array $order) use (&$replace_values_recursive) {
218
            $array = array_replace($order, array_replace($array, $order));
219
220
            foreach ($array as $key => &$value) {
221
                if (is_array($value)) {
222
                    $value = (array_key_exists($key, $order) && is_array($order[$key]))
223
                        ? $replace_values_recursive($value, $order[$key])
224
                        : $value
225
                    ;
226
                }
227
            }
228
229
            return $array;
230
        };
231
232
        return $replace_values_recursive($base, $replacement);
233
    }
234
235
    /**
236
     * Inject default batch and object actions settings
237
     *
238
     * @return array
239
     */
240
    protected function applyActionsBuilderDefaults(array $params)
241
    {
242
        if (!array_key_exists('namespace_prefix', $params) || !array_key_exists('bundle_name', $params)) {
243
            return $params;
244
        }
245
246
        $routeBase = $params['namespace_prefix'].'_'.$params['bundle_name'].'_'.$this->getBaseGeneratorName();
247
248
        if (array_key_exists('object_actions', $params) && is_array($params['object_actions'])) {
249
            foreach ($params['object_actions'] as $name => $config) {
250
                $baseKey = 'object_actions.'.$name;
251
252
                if (is_array($config) && array_key_exists('route', $config) && $config['route'] === 'inject_object_defaults') {
253
                    $customized = $params['object_actions'][$name];
254
                    unset($customized['route']);
255
256
                    $params['object_actions'][$name] = $this->recursiveReplace(array(
257
                        'route'         => $routeBase.'_object',
258
                        'label'         => $baseKey.'.label',
259
                        'csrfProtected' => true,
260
                        'params'        => array(
261
                            'pk'        => '{{ '.$this->getBaseGeneratorName().'.id }}',
262
                            'action'    => $name,
263
                        ),
264
                        'options'       => array(
265
                            'title'     => $baseKey.'.title',
266
                            'success'   => $baseKey.'.success',
267
                            'error'     => $baseKey.'.error',
268
                        ),
269
                    ), $customized);
270
                }
271
            }
272
        }
273
274
        if (array_key_exists('batch_actions', $params) && is_array($params['batch_actions'])) {
275
            foreach ($params['batch_actions'] as $name => $config) {
276
                $baseKey = 'batch_actions.'.$name;
277
278
                if (is_array($config) && array_key_exists('route', $config) && $config['route'] === 'inject_batch_defaults') {
279
                    $params['batch_actions'][$name] = $this->recursiveReplace($params['batch_actions'][$name], array(
280
                        'route'         => $routeBase.'_batch',
281
                        'label'         => $baseKey.'.label',
282
                        'csrfProtected' => true,
283
                        'params'        => array(
284
                            'action'    => $name,
285
                        ),
286
                        'options'       => array(
287
                            'title'     => $baseKey.'.title',
288
                            'success'   => $baseKey.'.success',
289
                            'error'     => $baseKey.'.error',
290
                            'notfound'  => $baseKey.'.notfound',
291
                        ),
292
                    ));
293
                }
294
            }
295
        }
296
297
        return $params;
298
    }
299
300
    /**
301
     * @param string $columnClass
302
     * @return void
303
     */
304
    public function setColumnClass($columnClass)
305
    {
306
        $this->columnClass = $columnClass;
307
    }
308
309
    /**
310
     * @return string The column class.
311
     */
312
    protected function getColumnClass()
313
    {
314
        return $this->columnClass;
315
    }
316
317
    /**
318
     * Set the yaml to pass all the vars to the builders
319
     *
320
     * @param array $yaml
321
     * @return void
322
     */
323
    protected function setYamlConfig(array $yaml)
324
    {
325
        $this->yaml = array_replace_recursive(
326
            Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/default.yml')),
327
            $yaml
328
        );
329
    }
330
331
    /**
332
     * @param string    $yamlPath   Path string with point for levels.
333
     * @param mixed     $default    Value to default to, path key not found.
334
     * @return mixed
335
     */
336
    public function getFromYaml($yamlPath, $default = null)
337
    {
338
        return $this->getFromArray(
339
            $this->yaml,
340
            $yamlPath,
341
            $default
342
        );
343
    }
344
345
    /**
346
     * @param object $fieldGuesser The fieldguesser.
347
     * @return void
348
     */
349
    public function setFieldGuesser($fieldGuesser)
350
    {
351
        $this->fieldGuesser = $fieldGuesser;
0 ignored issues
show
Bug introduced by
The property fieldGuesser 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...
352
    }
353
354
    /**
355
     * @return object The fieldguesser.
356
     */
357
    public function getFieldGuesser()
358
    {
359
        return $this->fieldGuesser;
360
    }
361
362
    /**
363
     * @param string $baseController
364
     */
365
    public function setBaseController($baseController)
366
    {
367
        $this->baseController = $baseController;
368
    }
369
370
    /**
371
     * @return string Base controller.
372
     */
373
    public function getBaseController()
374
    {
375
        return $this->baseController;
376
    }
377
378
    /**
379
     * @param string $baseGeneratorName
380
     */
381
    public function setBaseGeneratorName($baseGeneratorName)
382
    {
383
        $this->baseGeneratorName = $baseGeneratorName;
384
    }
385
386
    /**
387
     * @return string Base generator name.
388
     */
389
    public function getBaseGeneratorName()
390
    {
391
        return $this->baseGeneratorName;
392
    }
393
394
    /**
395
     * @return string Generated controller directory.
396
     */
397
    public function getGeneratedControllerFolder()
398
    {
399
        return 'Base'.$this->baseGeneratorName.'Controller';
400
    }
401
402
    /**
403
     * @param \Symfony\Component\Routing\RouterInterface $router
404
     * @return void
405
     */
406
    public function setRouter(RouterInterface $router)
407
    {
408
        $this->router = $router;
409
    }
410
411
    /**
412
     * @return \Symfony\Component\Routing\RouterInterface $router
413
     */
414
    public function getRouter()
415
    {
416
        return $this->router;
417
    }
418
419
    /**
420
     * @param array $bundleConfig
421
     */
422
    public function setBundleConfig(array $bundleConfig)
423
    {
424
        $this->bundleConfig = $bundleConfig;
425
    }
426
427
    /**
428
     * @return array
429
     */
430
    public function getBundleConfig()
431
    {
432
        return $this->bundleConfig;
433
    }
434
435
    /**
436
     * @param string    $configPath Path string with point for levels.
437
     * @param mixed     $default    Value to default to, path key not found.
438
     * @return mixed
439
     */
440
    public function getFromBundleConfig($configPath, $default = null)
441
    {
442
        return $this->getFromArray(
443
            $this->bundleConfig,
444
            $configPath,
445
            $default
446
        );
447
    }
448
449
    /**
450
     * @param array     $array      The array to traverse.
451
     * @param string    $path       Path string with point for levels.
452
     * @param mixed     $default    Value to default to, path key not found.
453
     * @return mixed
454
     */
455 View Code Duplication
    protected function getFromArray($array, $path, $default = null)
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...
456
    {
457
        $search_in = $array;
458
        $path = explode('.', $path);
459
        foreach ($path as $key) {
460
            if (!isset($search_in[$key])) {
461
                return $default;
462
            }
463
            $search_in = $search_in[$key];
464
        }
465
466
        return $search_in;
467
    }
468
}
469