Passed
Push — master ( 57f4f6...f7a67d )
by Bruno
10:20
created

Formularium::graphqlDirectives()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
nc 3
nop 1
dl 0
loc 39
ccs 16
cts 16
cp 1
crap 3
rs 9.584
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
/**
6
 * Abstract base class for frameworks. Each framework should have a class inheriting
7
 * from this class.
8
 */
9
class Formularium
10
{
11
    /**
12
     * Returns a list of datatype class names
13
     *
14
     * @return array
15
     */
16 2
    public static function getDatatypeNames(): array
17
    {
18 2
        $datatypes = array_map(
19
            function ($x) {
20 2
                return str_replace('Datatype_', '', str_replace('.php', '', $x));
21 2
            },
22 2
            array_diff(scandir(__DIR__ . '/Datatype/'), array('.', '..'))
0 ignored issues
show
Bug introduced by
It seems like scandir(__DIR__ . '/Datatype/') can also be of type false; however, parameter $array1 of array_diff() 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
            array_diff(/** @scrutinizer ignore-type */ scandir(__DIR__ . '/Datatype/'), array('.', '..'))
Loading history...
23
        );
24
    
25
        // TODO: avoid abstract classes
26 2
        $datatypes = array_filter(
27 2
            $datatypes,
28
            function ($t) {
29 2
                return ($t !== 'number' && $t !== 'choice' && $t !== 'association');
30 2
            }
31
        );
32
33 2
        return $datatypes;
34
    }
35
36 1
    public static function getDatatypeValidators(array $extraDatatypes = []): array
37
    {
38 1
        $datatypes = self::getDatatypeNames();
39
40 1
        $validators = [];
41
42 1
        foreach ($datatypes as $name) {
43 1
            $datatype = Datatype::factory($name);
44 1
            $validators = array_merge($validators, $datatype->getValidatorMetadata());
45
        }
46 1
        foreach ($extraDatatypes as $name) {
47
            $datatype = Datatype::factory($name);
48
            $validators = array_merge($validators, $datatype->getValidatorMetadata());
49
        }
50 1
        return $validators;
51
    }
52
53 1
    public static function graphqlDirectives(array $extraDatatypes = []): string
54
    {
55 1
        $validators = static::getDatatypeValidators($extraDatatypes);
56
57 1
        foreach ($validators as $name => $v) {
58 1
            $args = array_map(
59
                function ($a) {
60
                    return <<<EOF
61
    """
62 1
    {$a['comment']}
63
    """
64 1
    {$a['name']}: {$a['type']}
65
66
EOF;
67 1
                },
68 1
                $v['args']
69
            );
70
71 1
            $argString = '';
72 1
            if ($args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73 1
                $argString = "(\n" . join("\n", $args) . ')';
74
            }
75 1
            $graphql[$name] = <<< EOF
76
"""
77 1
{$v['comment']}
78
"""
79 1
directive @{$name}{$argString} on FIELD_DEFINITION
80
81
EOF;
82
        }
83
84 1
        unset($graphql['required']); // ! already does this
85
86
87
        return '
88
# File generated by Formularium.
89
# Do not edit this file directly.
90
91 1
' . join("\n", $graphql);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $graphql seems to be defined by a foreach iteration on line 57. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
92
    }
93
}
94