Passed
Push — master ( 6bf22a...09030e )
by Bruno
04:28
created

MigrationIndexDirective   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 30
ccs 5
cts 11
cp 0.4545
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processMigrationFieldDirective() 0 7 1
A processMigrationTypeDirective() 0 10 2
A processMigrationRelationshipDirective() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Modelarium\Exception\Exception;
6
use Modelarium\Laravel\Targets\MigrationGenerator;
7
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
8
use Modelarium\Laravel\Targets\MigrationCodeFragment;
9
use Modelarium\Parser;
10
11
class MigrationIndexDirective implements MigrationDirectiveInterface
12
{
13 1
    public static function processMigrationTypeDirective(
14
        MigrationGenerator $generator,
15
        \GraphQL\Language\AST\DirectiveNode $directive
16
    ): void {
17 1
        $indexFields = Parser::getDirectiveArgumentByName($directive, 'fields');
18
        
19 1
        if (!count($indexFields)) {
20
            throw new Exception("You must provide at least one field to an index");
21
        }
22 1
        $generator->createCode[] ='$table->index("' . implode('", "', $indexFields) .'");';
0 ignored issues
show
Bug introduced by
It seems like $indexFields can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

22
        $generator->createCode[] ='$table->index("' . implode('", "', /** @scrutinizer ignore-type */ $indexFields) .'");';
Loading history...
23 1
    }
24
25
    public static function processMigrationFieldDirective(
26
        MigrationGenerator $generator,
27
        \GraphQL\Type\Definition\FieldDefinition $field,
28
        \GraphQL\Language\AST\DirectiveNode $directive,
29
        MigrationCodeFragment $code
30
    ): void {
31
        $code->appendExtraLine('$table->index("' . $field->name . '");');
32
    }
33
34
    public static function processMigrationRelationshipDirective(
35
        MigrationGenerator $generator,
36
        \GraphQL\Type\Definition\FieldDefinition $field,
37
        \GraphQL\Language\AST\DirectiveNode $directive,
38
        MigrationCodeFragment $code
39
    ): void {
40
        $code->appendExtraLine('$table->index("' . $field->name . '");');
41
    }
42
}
43