Completed
Pull Request — master (#66)
by
unknown
01:45
created

ResourcesCommand::checkPivotRelations()   C

Complexity

Conditions 16
Paths 18

Size

Total Lines 25

Duplication

Lines 6
Ratio 24 %

Importance

Changes 0
Metric Value
dl 6
loc 25
rs 5.5666
c 0
b 0
f 0
cc 16
nc 18
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Wn\Generators\Commands;
2
3
use InvalidArgumentException;
4
use Symfony\Component\Yaml\Yaml;
5
6
7
class ResourcesCommand extends BaseCommand {
8
9
    protected $signature = 'wn:resources
10
        {files* : Paths to the files containing resources declarations}
11
        {--path= : where to store the model files.}
12
        {--routes= : where to store the routes.}
13
        {--no-routes : do not add routes.}
14
        {--controllers= : where to store the controllers.}
15
        {--no-controllers : do not generate controllers.}
16
        {--no-migration : do not migrate.}
17
        {--check-only : only check supplied files for valide relationships.}
18
        {--skip-check : skip validity check before processing.}
19
        {--force= : override the existing files}
20
        {--force-redefine : Force model redefinition.}
21
        {--laravel= : Use Laravel style route definitions}
22
    ';
23
24
    protected $description = 'Generates multiple resources from a couple of files';
25
26
    protected $pivotTables = [];
27
    protected $morphTables = [];
28
29
    private $checkedErrors = 0;
30
    private $checkErrors = [];
31
    private $checkInfo = [];
32
33
    public function handle()
34
    {
35
        $files = $this->argument('files');
36
        $nodes = [];
37
        foreach ($files as $file) {
0 ignored issues
show
Bug introduced by
The expression $files of type array|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
38
            $nodes = $this->mergeNodes($nodes, $this->readFile($file), $this->option('force-redefine'));
39
        }
40
41
        $this->line('');
42
        $this->info('Bringing models to order...');
43
44
        $nodes = $this->sortDependencies($nodes);
45
        $pivotTables = $this->uniqueArray($this->getTables($nodes, 'pivotTables'));
46
        $morphTables = $this->uniqueArray($this->getTables($nodes, 'morphTables'));
47
48
        if (! $this->option('skip-check')) {
49
        	$this->info('Checking Relationships...');
50
        	$keys = array_keys($nodes);
51
        	foreach ($nodes as $model => $i) {
52
        		$this->checkRelations($i['belongsTo'], 'belongsTo', $i['filename'], $i['uniquename'], $keys);
53
        		// $this->checkRelations($i['hasManyThrough'], 'hasManyThrough', $file, $model);
54
        	}
55
        	$this->checkPivotRelations($nodes, $pivotTables, 'pivot');
56
        	$this->checkPivotRelations($nodes, $morphTables, 'morph');
57
        }
58
59 View Code Duplication
        if ($this->checkedErrors > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
        	$this->line('');
61
        	if ($this->option('check-only')) {
62
        		$this->info('Checking only, we have found ' . $this->checkedErrors . ' errors.');
63
        	}
64
        	$this->printErrors();
65
        }
66
67
        $proceed = (! $this->option('check-only') && $this->checkedErrors == 0) || $this->option('skip-check');
68 View Code Duplication
        if (! $this->option('check-only') && $this->checkedErrors > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
        	$this->line('');
70
        	$proceed = $this->confirm("We have found " . $this->checkedErrors . " errors. Are you sure you want to continue?");
71
        }
72
        if ($proceed) {
73
        	$this->buildResources($nodes);
74
75
        	// if (!$this->option('no-migration')) {
76
        	// 	$this->call('migrate'); // actually needed for pivot seeders !
77
        	// }
78
79
        	$this->line('');
80
            $this->buildTables('Pivot-Table', 'wn:pivot-table', 'model1', 'model2', $pivotTables);
81
82
        	$this->line('');
83
            $this->buildTables('Morph-Table', 'wn:morph-table', 'model', 'morphable', $morphTables);
84
85
        	if (!$this->option('no-migration')) {
86
        		$this->call('migrate');
87
        	}
88
        }
89
    }
90
91
    protected function uniqueArray($array)
92
    {
93
        return array_map(
94
            'unserialize',
95
            array_unique(array_map('serialize', $array))
96
        );
97
    }
98
99
    protected function readFile($file)
100
    {
101
        $this->info("Reading file ".$file);
102
103
        $content = $this->fs->get($file);
104
        $content = Yaml::parse($content);
105
106
        $nodes = [];
107
108
        foreach ($content as $model => $i){
109
            /*
110
                $i['modelname'] = as originally in YAML defined
111
                $i['name']      = as originally defined in snake_case
112
                $i['uniquename']= for key in singular studly_case
113
            */
114
            $i['filename'] = $file;
115
            $i['modelname'] = $model;
116
            $model = studly_case(str_singular($model));
117
            $i['uniquename'] = $model;
118
119
            $nodes[] = $this->getResourceParams($i);
120
        }
121
122
        return $nodes;
123
    }
124
125
    protected function mergeNodes($nodes, $toMerge, $forceRedefinition = false) {
126
        foreach($toMerge as $node) {
127
            $nodes = $this->mergeNode($nodes, $node, $forceRedefinition);
128
        }
129
130
        return $nodes;
131
    }
132
133
    protected function mergeNode($nodes, $toMerge, $forceRedefinition = false) {
134
        if (empty($nodes[$toMerge['uniquename']]) || $forceRedefinition) {
135
            if (!empty($nodes[$toMerge['uniquename']])) {
136
                $this->checkError($toMerge['uniquename'] . ": forced to redefine (in file " . $nodes[$toMerge['uniquename']]['filename'] . ", redefined from file ".$file.")");
0 ignored issues
show
Bug introduced by
The variable $file does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
137
            }
138
            $nodes[$toMerge['uniquename']] = $toMerge;
139
        } else {
140
            $this->checkError($toMerge['uniquename'] . ": already defined (in file " . $nodes[$toMerge['uniquename']]['filename'] . ", trying to redefine from file ".$file."; Use --force-redefine to force redefinition)");
141
        }
142
143
        return $nodes;
144
    }
145
146
    protected function getTables($nodes, $key) {
147
        $tables = [];
148
        foreach($nodes as $node) {
149
            if (!empty($node[$key])) {
150
                $tables = array_merge($tables, $node[$key]);
151
            }
152
        }
153
154
        return $tables;
155
    }
156
157
    protected function buildResources($nodes)
158
    {
159
        $modelIndex = 0;
160
        $migrationIdLength = strlen((string)count($nodes));
161
        foreach ($nodes as $i) {
162
            $migrationName = 'Create' .  ucwords(str_plural($i['name']));
163
            $migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , $migrationIdLength, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table';
164
165
            $this->line('');
166
            $this->info('Building Model ' . $i['uniquename']);
167
168
            $options = [
169
                'name' => $i['name'],
170
                'fields' => $i['fields'],
171
                '--add' => $i['add'],
172
                '--has-many' => $i['hasMany'],
173
                '--has-one' => $i['hasOne'],
174
                '--belongs-to' => $i['belongsTo'],
175
                '--belongs-to-many' => $i['belongsToMany'],
176
                '--has-many-through' => $i['hasManyThrough'],
177
                '--morph-to' => $i['morphTo'],
178
                '--morph-many' => $i['morphMany'],
179
                '--morph-to-many' => $i['morphToMany'],
180
                '--morphed-by-many' => $i['morphedByMany'],
181
                '--no-routes' => $this->option('no-routes'),
182
                '--no-controller' => $this->option('no-controllers'),
183
                '--force' => $this->option('force'),
184
                '--migration-file' => $migrationFile,
185
            ];
186
            if ($this->option('laravel')) {
187
                $options['--laravel'] = true;
188
            }
189
            if ($this->option('routes')) {
190
                $options['--routes'] = $this->option('routes');
191
            }
192
            if ($this->option('controllers')) {
193
                $options['--controller'] = $this->option('controllers');
194
            }
195
            if ($this->option('path')) {
196
                $options['--path'] = $this->option('path');
197
            }
198
199
            $this->call('wn:resource', $options);
200
            $modelIndex++;
201
        }
202
    }
203
204
    protected function buildTables($type, $command, $model1, $model2, $tableAssignment)
205
    {
206
        foreach ($tableAssignment as $tables) {
207
            $this->info('Building '.$type.' ' . $tables[0] . ' - ' . $tables[1]);
208
            $this->call($command, [
209
                $model1 => $tables[0],
210
                $model2 => $tables[1],
211
                '--force' => $this->option('force')
212
            ]);
213
214
            // $this->call('wn:pivot-seeder', [
215
            //     'model1' => $tables[0],
216
            //     'model2' => $tables[1],
217
            //     '--force' => $this->option('force')
218
            // ]);
219
        }
220
    }
221
222
    protected function getResourceParams($i)
223
    {
224
        $modelName = $i['modelname'];
225
226
        $i['filename'] = $i['filename'];
227
        $i['name'] = snake_case($modelName);
228
        $i['modelname'] = $i['modelname'];
229
        $i['uniquename'] = $i['uniquename'];
230
231
        foreach(['hasMany', 'hasOne', 'add', 'belongsTo', 'belongsToMany', 'hasManyThrough', 'morphTo', 'morphMany', 'morphToMany', 'morphedByMany'] as $relation){
232
            if(isset($i[$relation])){
233
                $i[$relation] = $this->convertArray($i[$relation], ' ', ',');
234
            } else {
235
                $i[$relation] = false;
236
            }
237
        }
238
239
        if($i['belongsToMany']){
240
            $i['pivotTables'] = $this->belongsTo($i['name'], $modelName, $i['belongsToMany']);
241
        }
242
243
        if($i['morphToMany']){
244
            $i['morphTables'] = $this->morphToMany($modelName, $i['morphToMany']);
245
        }
246
247
        if($i['morphedByMany']){
248
            $i['morphTables'] = array_merge($i['morphTables'], $this->morphedByMany($i['name'], $modelName, $i['morphedByMany']));
249
        }
250
251
        $fields = [];
252
        foreach($i['fields'] as $name => $value) {
253
            $value['name'] = $name;
254
            $fields[] = $this->serializeField($value);
255
        }
256
        $i['fields'] = implode(' ', $fields);
257
258
        return $i;
259
    }
260
261 View Code Duplication
    protected function belongsTo($name, $modelName, $belongsTo)
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...
262
    {
263
        $parsedRelations = [];
264
        $relations = $this->getArgumentParser('relations')->parse($belongsTo);
265
        foreach ($relations as $relation){
266
            if(! $relation['model']){
267
                $table = snake_case($relation['name']);
268
            } else {
269
                $table = snake_case($this->extractClassName($relation['model']));
270
            }
271
272
            $tables = [ str_singular($table), $name ];
273
            sort($tables);
274
            $tables[] = $modelName;
275
            $parsedRelations[] = $tables;
276
        }
277
278
        return $parsedRelations;
279
    }
280
281
    protected function morphToMany($modelName, $morphToMany)
0 ignored issues
show
Unused Code introduced by
The parameter $morphToMany is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
282
    {
283
        $parsedRelations = [];
284
        $relations = $this->getArgumentParser('relations-morphMany')->parse();
0 ignored issues
show
Bug introduced by
The call to parse() misses a required argument $args.

This check looks for function calls that miss required arguments.

Loading history...
285
        foreach ($relations as $relation){
286
            if(! $relation['through']){
287
                $morphable = snake_case($this->extractClassName($relation['model']));
288
                $model = snake_case($relation['name']);
289
            } else {
290
                $morphable = snake_case($this->extractClassName($relation['through']));
291
                $model = snake_case($this->extractClassName($relation['model']));
292
            }
293
294
            $tables = [ str_singular($model), str_singular($morphable), $modelName ];
295
            $parsedRelations[] = $tables;
296
        }
297
298
        return $parsedRelations;
299
    }
300
301 View Code Duplication
    protected function morphedByMany($name, $modelName, $morphedByMany)
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...
302
    {
303
        $parsedRelations = [];
304
        $relations = $this->getArgumentParser('relations-morphMany')->parse($morphedByMany);
305
        foreach ($relations as $relation){
306
            $table = '';
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
307
308
            if(! $relation['through']){
309
                $morphable = snake_case($this->extractClassName($relation['model']));
310
            } else {
311
                $morphable = snake_case($this->extractClassName($relation['through']));
312
            }
313
314
            $tables = [ str_singular($name), str_singular($morphable), $modelName ];
315
            $parsedRelations[] = $tables;
316
        }
317
318
        return $parsedRelations;
319
    }
320
321
    protected function serializeField($field)
322
    {
323
        $name = $field['name'];
324
        $schema = $this->convertArray(str_replace(':', '.', $field['schema']), ' ', ':');
325
        $rules = (isset($field['rules'])) ? $this->convertArray(trim($field['rules']), ' ', '|') : '';
326
        $tags = !empty($field['tags']) ? $this->convertArray($field['tags'], ' ', ',') : '';
327
328
        $string = "{$name};{$schema};{$rules};{$tags}";
329
330
        if(isset($field['factory']) && !empty($field['factory'])){
331
            $string .= ';' . $field['factory'];
332
        }
333
334
        return $string;
335
    }
336
337
    protected function convertArray($list, $old, $new)
338
    {
339
        return implode($new, array_filter(explode($old, $list), function($item){
340
            return !empty($item);
341
        }));
342
    }
343
344
    private function sortDependencies($nodes) {
345
        $load_order = array();
346
        $seen       = array();
347
348
        foreach($nodes as $key => $item) {
349
            $tmp = $this->getDependencies($nodes, $key, $seen);
350
351
            // if($tmp[2] === false) {
352
            $load_order = array_merge($load_order, $tmp[0]);
353
            $seen       = $tmp[1];
354
            // }
355
        }
356
357
        return $load_order;
358
    }
359
360
    private function getDependencies($nodes, $key, $seen = array()) {
361
        if(array_key_exists($key, $seen) === true) {
362
            return array(array(), $seen);
363
        }
364
365
366
        if(!empty($nodes[$key])) {
367
            $order = array();
368
            // $failed         = array();
369
370
            if($nodes[$key]['belongsTo']) {
371
                $deps = $this->getArgumentParser('relations')->parse($nodes[$key]['belongsTo']);
372
                foreach($deps as $dependency) {
373
                    if(! $dependency['model']){
374
	                    $dependency['model'] = $dependency['name'];
375
	                } else if(strpos($dependency['model'], '\\') !== false ){
376
	                    $dependency['model'] = substr($dependency['model'], strpos($dependency['model'], '\\')+1); // Cut offs first namespace part
377
	                }
378
                    $dependency['model'] = studly_case(str_singular($dependency['model']));
379
                    if ($dependency['model'] != $key) {
380
                        $tmp = $this->getDependencies($nodes, $dependency['model'], $seen);
381
382
                        $order  = array_merge($order, $tmp[0]);
383
                        $seen   = $tmp[1];
384
                    }
385
386
                    // if($tmp[2] !== false) {
387
                    //     $failed = array_merge($tmp[2], $failed);
388
                    // }
389
                }
390
            }
391
            $seen[$key]  = true;
392
            $order[$key] = $nodes[$key];
393
            // $failed     = (count($failed) > 0) ? $failed : false;
394
395
            return array($order, $seen);//, $failed
396
        }
397
398
        return array(array(), $seen);//, array($item)
399
    }
400
401
    protected function checkError($message, $model = "", $file = "") {
402
        $this->checkErrors[] = array("message" => $message, "model" => $model, "file" => $file);
403
        $this->checkedErrors++;
404
    }
405
406
    protected function checkInfo($message, $model = "", $file = "") {
407
        $this->checkInfo[] = array("message" => $message, "model" => $model, "file" => $file);
408
    }
409
410
    protected function printErrors() {
411
        foreach ($this->checkErrors as $error) {
412
            $this->error($error['message']);
413
        }
414
        foreach ($this->checkInfo as $info) {
415
            $this->info($info['message']);
416
        }
417
    }
418
419
    protected function checkRelations($relations, $type, $file, $model, $keys) {
420
        if ($relations) {
421
            $position = array_search($model, $keys);
422
            $relations = $this->getArgumentParser('relations')->parse($relations);
423
            foreach($relations as $relation) {
424
                switch($type) {
425
                    case "belongsTo":
426
                        $rModel = $relation['model'] ? $relation['model'] : $relation['name'];
427
                        break;
428
                    default: continue;
429
                }
430
431
                $search = array_search(studly_case(str_singular($rModel)), $keys);
0 ignored issues
show
Bug introduced by
The variable $rModel does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
432
                if (($search === false || $search > $position) && !class_exists($this->prependNamespace($rModel)) && !class_exists($this->prependNamespace($rModel, 'App'))) {
0 ignored issues
show
Documentation introduced by
'App' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
433
                    $this->checkError(studly_case(str_singular($rModel)) . ": undefined (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")");
434
                } else if (class_exists($this->prependNamespace($rModel))) {
435
                    $this->checkInfo(studly_case(str_singular($rModel)) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")");
436
                } else if (class_exists($this->prependNamespace($rModel, 'App'))) {
0 ignored issues
show
Documentation introduced by
'App' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
437
                    $this->checkInfo(studly_case(str_singular($rModel)) . ": already defined in Namespace App\\ (used in " . $type . "-relationship of model " . $model . " in file " . $file . ")");
438
                }
439
            }
440
        }
441
    }
442
443
    protected function checkPivotRelations($nodes, $relations, $rType) {
444
        if ($relations) {
445
            foreach($relations as $relation) {
446
                $relation['0'] = studly_case(str_singular($relation['0']));
447
                $relation['1'] = studly_case(str_singular($relation['1']));
448
                $relation['2'] = studly_case(str_singular($relation['2']));
449
450
                if (empty($nodes[$relation['0']]) && !class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['0']))) && !class_exists('App\\' . ucwords(camel_case($relation['0'])))) {
451
                    $this->checkError($relation['0'] . ": undefined (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
452
                } else if (class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['0'])))) {
453
                    $this->checkInfo(studly_case(str_singular($relation['0'])) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
454 View Code Duplication
                } else if (class_exists('App\\' . ucwords(camel_case($relation['0'])))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
455
                    $this->checkInfo(studly_case(str_singular($relation['0'])) . ": already defined in Namespace App\\ (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
456
                }
457
458
                if ($rType == "pivot" && empty($nodes[$relation['1']]) && !class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['1']))) && !class_exists('App\\' . ucwords(camel_case($relation['1'])))) {
459
                    $this->checkError($relation['1'] . ": undefined (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
460
                } else if ($rType == "pivot" && class_exists($this->getNamespace() . '\\' . ucwords(camel_case($relation['1'])))) {
461
                    $this->checkInfo(studly_case(str_singular($relation['1'])) . ": already defined in Namespace " . $this->getNamespace() . " (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
462 View Code Duplication
                } else if ($rType == "pivot" && class_exists('App\\' . ucwords(camel_case($relation['1'])))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
463
                    $this->checkInfo(studly_case(str_singular($relation['1'])) . ": already defined in Namespace App\\ (used in " . $rType . "-based relationship of model " . $relation['2'] . " in file " . $nodes[$relation['2']]['filename'] . ")");
464
                }
465
            }
466
        }
467
    }
468
469
}
470