Passed
Push — master ( 781e53...d845af )
by Bruno
09:45
created

BelongsToManyDirective   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 15
c 1
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processSeedFieldDirective() 0 11 2
A makeManyToManySeed() 0 13 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Modelarium\Laravel\Targets\SeedGenerator;
6
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
7
8
class BelongsToManyDirective implements SeedDirectiveInterface
9
{
10
    public static function processSeedFieldDirective(
11
        SeedGenerator $generator,
12
        \GraphQL\Type\Definition\FieldDefinition $field,
13
        \GraphQL\Language\AST\Node $directive
14
    ): void {
15
        $type1 = $generator->lowerName;
0 ignored issues
show
Bug introduced by
The property lowerName is declared protected in Modelarium\BaseGenerator and cannot be accessed from this context.
Loading history...
16
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
17
18
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
19
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
20
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
21
        }
22
    }
23
24
    protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string
0 ignored issues
show
Unused Code introduced by
The parameter $sourceModel is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    protected static function makeManyToManySeed(/** @scrutinizer ignore-unused */ string $sourceModel, string $targetModel, string $relationship): string

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

Loading history...
25
    {
26
        return <<<EOF
27
28
        try {
29
            \${$targetModel}Items = App\\Models\\$targetModel::all();
30
            \$model->{$relationship}()->attach(
31
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
32
            );
33
        }
34
        catch (\InvalidArgumentException \$e) {
35
            \$model->{$relationship}()->attach(
36
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
37
            );
38
        }
39
EOF;
40
    }
41
}
42