ParseDefinitions   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 32
c 1
b 0
f 0
dl 0
loc 131
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A extractSchema() 0 11 1
A extractRawRowsFromSchema() 0 5 1
A indentRows() 0 5 1
A parseCustomMutationsFrom() 0 3 1
A parseCustomSchemaFrom() 0 10 2
A removeWhitespaceFromRows() 0 5 1
A __construct() 0 3 1
A parseCustomQueriesFrom() 0 3 1
A getGraphqlDefinitionFilePaths() 0 8 2
A getSchemaFrom() 0 9 1
1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils\Generators\Classes;
4
5
class ParseDefinitions
6
{
7
    /** @var ParsePermissions */
8
    private $permissionsParser;
9
10
    /**
11
     * ParseDefinitions constructor.
12
     *
13
     * @param ParsePermissions $permissionsParser
14
     */
15
    public function __construct(ParsePermissions $permissionsParser)
16
    {
17
        $this->permissionsParser = $permissionsParser;
18
    }
19
20
    /**
21
     * @param string $path
22
     * @return array
23
     */
24
    public function parseCustomQueriesFrom(string $path)
25
    {
26
        return $this->parseCustomSchemaFrom($path, 'Query');
27
    }
28
29
    /**
30
     * @param string $path
31
     * @return array
32
     */
33
    public function parseCustomMutationsFrom(string $path)
34
    {
35
        return $this->parseCustomSchemaFrom($path, 'Mutation');
36
    }
37
38
    /**
39
     * @param string $typePath
40
     * @return array
41
     */
42
    public function getGraphqlDefinitionFilePaths(string $typePath): array
43
    {
44
        $files = [];
45
        foreach (glob(sprintf('%s/%s/*.graphql', base_path(), $typePath)) as $file) {
46
            $files[] = $file;
47
        }
48
49
        return $files;
50
    }
51
52
    /**
53
     * @param string $customSchemaPath
54
     * @param string $type
55
     * @return array
56
     */
57
    private function parseCustomSchemaFrom(string $customSchemaPath, string $type)
58
    {
59
        $customSchema = [];
60
        foreach ($this->getGraphqlDefinitionFilePaths($customSchemaPath) as $path) {
61
            $returnData = $this->extractSchema($type, $this->getSchemaFrom($path));
62
63
            $customSchema = array_merge($customSchema, $returnData);
64
        }
65
66
        return $customSchema;
67
    }
68
69
    /**
70
     * @param string $path
71
     * @return bool|string
72
     */
73
    private function getSchemaFrom(string $path)
74
    {
75
        $file = fopen($path, "r");
76
77
        $fileContents = fread($file, filesize($path));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fread() does only seem to accept resource, 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

77
        $fileContents = fread(/** @scrutinizer ignore-type */ $file, filesize($path));
Loading history...
78
79
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

79
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
80
81
        return $fileContents;
82
    }
83
84
    /**
85
     * @param string $type
86
     * @param string $fileContents
87
     * @return array
88
     * @throws \GraphQL\Error\SyntaxError
89
     */
90
    private function extractSchema(string $type, string $fileContents): array
91
    {
92
        $this->permissionsParser->register($fileContents);
93
94
        $rawSchemaRows = $this->extractRawRowsFromSchema($type, $fileContents);
95
96
        $whitespaceRemovedRows = $this->removeWhitespaceFromRows($rawSchemaRows);
97
98
        $cleared = array_filter($whitespaceRemovedRows);
99
100
        return $this->indentRows($cleared);
101
    }
102
103
    /**
104
     * @param array $rawSchemaRows
105
     * @return array
106
     */
107
    private function removeWhitespaceFromRows(array $rawSchemaRows): array
108
    {
109
        return array_map(
110
            'trim',
111
            $rawSchemaRows
112
        );
113
    }
114
115
    /**
116
     * @param array $cleared
117
     * @return array
118
     */
119
    private function indentRows(array $cleared): array
120
    {
121
        return array_map(function ($row) {
122
            return '    ' . $row;
123
        }, $cleared);
124
    }
125
126
    /**
127
     * @param string $type
128
     * @param string $fileContents
129
     * @return array
130
     */
131
    private function extractRawRowsFromSchema(string $type, string $fileContents): array
132
    {
133
        return explode(
134
            "\n",
135
            str_replace(["type", $type, "{", "}"], "", $fileContents)
136
        );
137
    }
138
}
139