Passed
Push — master ( c0a95b...c5e7d6 )
by Bruno
04:51
created

processModelRelationshipDirective()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 4
nop 3
dl 0
loc 57
ccs 31
cts 31
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

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 declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use Illuminate\Support\Str;
7
use Modelarium\Exception\Exception;
8
use Modelarium\Parser;
9
use Modelarium\Datatypes\RelationshipFactory;
10
use Modelarium\Laravel\Targets\ModelGenerator;
11
use Modelarium\Laravel\Targets\SeedGenerator;
12
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
13
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
14
15
class MorphedByManyDirective implements ModelDirectiveInterface, SeedDirectiveInterface
16
{
17
    public static function processModelTypeDirective(
18
        ModelGenerator $generator,
19
        \GraphQL\Language\AST\DirectiveNode $directive
20
    ): void {
21
        // nothing
22
    }
23
24 1
    public static function processModelFieldDirective(
25
        ModelGenerator $generator,
26
        \GraphQL\Type\Definition\FieldDefinition $field,
27
        \GraphQL\Language\AST\DirectiveNode $directive
28
    ): void {
29
        // nothing
30 1
    }
31
32 1
    public static function processModelRelationshipDirective(
33
        ModelGenerator $generator,
34
        \GraphQL\Type\Definition\FieldDefinition $field,
35
        \GraphQL\Language\AST\DirectiveNode $directive
36
    ): string {
37 1
        $name = $directive->name->value;
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
38 1
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
39 1
        $typeName = $type->name;
0 ignored issues
show
Unused Code introduced by
The assignment to $typeName is dead and can be removed.
Loading history...
40
41 1
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
42 1
        $lowerNamePlural = $generator->getInflector()->pluralize($lowerName);
0 ignored issues
show
Unused Code introduced by
The assignment to $lowerNamePlural is dead and can be removed.
Loading history...
43
44 1
        $sourceTypeName = $generator->getLowerName();
45 1
        $targetTypeName = $lowerName;
46 1
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
47 1
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
48 1
        $targetClass = '\\App\\Models\\' . Str::studly($generator->getInflector()->singularize($field->name));
0 ignored issues
show
Unused Code introduced by
The assignment to $targetClass is dead and can be removed.
Loading history...
49 1
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
50
51 1
        $relationship = RelationshipFactory::MORPH_MANY_TO_MANY; // TODO
52 1
        $isInverse = true;
53 1
        $typeMap = $generator->parser->getSchema()->getTypeMap();
54
55 1
        foreach ($typeMap as $name => $object) {
56 1
            if (!($object instanceof ObjectType) || $name === 'Query' || $name === 'Mutation' || $name === 'Subscription') {
57 1
                continue;
58
            }
59
60
            /**
61
             * @var ObjectType $object
62
             */
63
64 1
            if (str_starts_with((string)$name, '__')) {
65
                // internal type
66 1
                continue;
67
            }
68
69 1
            foreach ($object->getFields() as $subField) {
70 1
                $subDirectives = Parser::getDirectives($subField->astNode->directives);
71
72 1
                if (!array_key_exists('morphToMany', $subDirectives)) {
73 1
                    continue;
74
                }
75
76 1
                $methodName = $generator->getInflector()->pluralize(mb_strtolower((string)$name));
77 1
                $generator->class->addMethod($methodName)
78 1
                        ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany')
79 1
                        ->setPublic()
80 1
                        ->setBody("return \$this->morphedByMany($name::class, '$lowerName');");
81
            }
82
        }
83
84 1
        return $generator->getRelationshipDatatypeName(
85 1
            $relationship,
86
            $isInverse,
87
            $sourceTypeName,
88
            $targetTypeName
89
        );
90
    }
91
92 1
    public static function processSeedFieldDirective(
93
        SeedGenerator $generator,
94
        \GraphQL\Type\Definition\FieldDefinition $field,
95
        \GraphQL\Language\AST\DirectiveNode $directive
96
    ): void {
97 1
        $type1 = $generator->getLowerName();
98 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
99
100 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
101 1
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
102 1
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
103
        }
104 1
    }
105
106 1
    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

106
    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...
107
    {
108
        return <<<EOF
109
110
        try {
111 1
            \${$targetModel}Items = App\\Models\\$targetModel::all();
112 1
            \$model->{$relationship}()->attach(
113 1
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
114
            );
115
        }
116
        catch (\InvalidArgumentException \$e) {
117 1
            \$model->{$relationship}()->attach(
118 1
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
119
            );
120
        }
121
EOF;
122
    }
123
}
124