Completed
Push — master ( 39887f...25839e )
by Dominik
02:21
created

NodeGenerator::structureCode()   D

Complexity

Conditions 9
Paths 33

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 33
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder;
6
7
use PhpParser\Error;
8
use PhpParser\Node\Arg;
9
use PhpParser\Node\Expr;
10
use PhpParser\Node\Expr\Assign;
11
use PhpParser\Node\Expr\ConstFetch;
12
use PhpParser\Node\Expr\MethodCall;
13
use PhpParser\Node\Expr\New_;
14
use PhpParser\Node\Expr\Variable;
15
use PhpParser\Node\Name;
16
use PhpParser\Node\Name\FullyQualified;
17
use PhpParser\Node\Scalar\DNumber;
18
use PhpParser\Node\Scalar\LNumber;
19
use PhpParser\Node\Scalar\String_;
20
use PhpParser\ParserFactory;
21
use PhpParser\PrettyPrinter\Standard as PhpGenerator;
22
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
23
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
24
use Saxulum\ElasticSearchQueryBuilder\Node\ScalarNode;
25
26
final class NodeGenerator
27
{
28
    /**
29
     * @var PhpGenerator
30
     */
31
    private $phpGenerator;
32
33
    /**
34
     * @param PhpGenerator $phpGenerator
35
     */
36
    public function __construct(PhpGenerator $phpGenerator)
37
    {
38
        $this->phpGenerator = $phpGenerator;
39
    }
40
41
    /**
42
     * @param $query
43
     * @return string
44
     */
45
    public function generateByJson($query): string
46
    {
47
48
//        $code = '<?php
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
//        $node = (new ObjectNode())
50
//            ->add(\'query\', (new ObjectNode())
51
//                ->add(\'bool\', (new ObjectNode())
52
//                    ->add(\'must\', (new ObjectNode())
53
//                        ->add(\'term\', (new ObjectNode())
54
//                            ->add(\'user\', (new ScalarNode(\'kimchy\')))
55
//                        )
56
//                    )
57
//                    ->add(\'filter\', (new ObjectNode())
58
//                        ->add(\'term\', (new ObjectNode())
59
//                            ->add(\'tag\', (new ScalarNode(\'tech\')))
60
//                        )
61
//                    )
62
//                )
63
//            );
64
//        ';
65
//
66
//        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
67
//
68
//        try {
69
//            $stmts = $parser->parse($code);
70
//            print_r($stmts); die;
71
//            // $stmts is an array of statement nodes
72
//        } catch (Error $e) {
73
//            echo 'Parse Error: ', $e->getMessage();
74
//        }
75
//
76
//        die;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        $data = json_decode($query, false);
95
        if (JSON_ERROR_NONE !== json_last_error()) {
96
            throw new \InvalidArgumentException(sprintf('Message: %s, query: %s', json_last_error_msg(), $query));
97
        }
98
99
        if ($data instanceof \stdClass) {
100
            $expr = $this->appendChildrenToObjectNode($data);
101
        } else {
102
            $expr = $this->appendChildrenToArrayNode($data);
103
        }
104
105
        $code = $this->phpGenerator->prettyPrint([new Assign(new Variable('node'), $expr)]);
106
107
        return $this->structureCode($code);
108
    }
109
110
    /**
111
     * @param string $code
112
     * @return string
113
     */
114
    private function structureCode(string $code): string
115
    {
116
        $codeWithLinebreaks = str_replace('->add', "\n->add", $code);
117
118
        $lines = explode("\n", $codeWithLinebreaks);
119
120
        $position = 0;
121
122
        $structuredLines = [];
123
124
        foreach ($lines as $i => $line) {
125
            if (0 === strpos($line, '->add') && false === strpos($structuredLines[count($structuredLines) - 1], ' )') && false === strpos($structuredLines[count($structuredLines) - 1], 'ScalarNode')) {
126
                $position++;
127
            }
128
            $lineLength = count($lines) - 1 !== $i ? strlen($line) -1 : strlen($line) - 2;
129
            $braceCount = 0;
130
            while(')' === $line[$lineLength--]) {
131
                $braceCount++;
132
            }
133
            $prefix = str_pad('', $position * 4);
134
            if ($braceCount > 2) {
135
                $structuredLines[] = $prefix . substr($line, 0, - ($braceCount - 2));
136
            } else {
137
                $structuredLines[] = $prefix . $line;
138
            }
139
140
            while ($braceCount-- > 2) {
141
                $position--;
142
                $structuredLines[] = str_pad('', $position * 4) . ')';
143
            }
144
        }
145
146
        $structuredLines[count($structuredLines) - 1] .= ';';
147
148
        return implode("\n", $structuredLines);
149
    }
150
151
    /**
152
     * @return Expr
153
     */
154
    private function createObjectNode(): Expr
155
    {
156
        return new New_(new Name('ObjectNode'));
157
    }
158
159
    /**
160
     * @return Expr
161
     */
162
    private function createArrayNode(): Expr
163
    {
164
        return new New_(new Name('ArrayNode'));
165
    }
166
167
    /**
168
     * @param string|float|int|bool|null $value
169
     * @return Expr
170
     */
171 View Code Duplication
    private function createScalarNode($value): Expr
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        if (is_string($value)) {
174
            $valueExpr = new String_($value);
175
        } elseif (is_int($value)) {
176
            $valueExpr = new LNumber($value);
177
        } elseif (is_float($value)) {
178
            $valueExpr = new DNumber($value);
179
        } elseif (is_bool($value)) {
180
            $valueExpr = new ConstFetch(new Name($value ? 'true' : 'false'));
181
        } else {
182
            $valueExpr = new ConstFetch(new Name('null'));
183
        }
184
185
        return new New_(new Name('ScalarNode'), [new Arg($valueExpr)]);
186
    }
187
188
    /**
189
     * @param array $data
190
     * @return Expr
191
     */
192
    private function appendChildrenToArrayNode(array $data)
193
    {
194
        $expr = $this->createArrayNode();
195
196
        foreach ($data as $key => $value) {
197 View Code Duplication
            if ($value instanceof \stdClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
                $nodeExpr = $this->createObjectNode();
199
            } elseif (is_array($value)) {
200
                $nodeExpr = $this->createArrayNode();
201
            } else {
202
                $nodeExpr = $this->createScalarNode($value);
203
            }
204
205 View Code Duplication
            if ($value instanceof \stdClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
                $nodeExpr = $this->appendChildrenToObjectNode($value);
207
            } elseif (is_array($value)) {
208
                $nodeExpr = $this->appendChildrenToArrayNode($value);
209
            }
210
211
            $expr = new MethodCall($expr, 'add', [new Arg($nodeExpr)]);
212
        }
213
214
        return $expr;
215
    }
216
217
    /**
218
     * @param \stdClass $data
219
     * @return Expr
220
     */
221
    private function appendChildrenToObjectNode(\stdClass $data)
222
    {
223
        $expr = $this->createObjectNode();
224
225
        foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass> is not traversable.
Loading history...
226 View Code Duplication
            if ($value instanceof \stdClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
                $nodeExpr = $this->createObjectNode();
228
            } elseif (is_array($value)) {
229
                $nodeExpr = $this->createArrayNode();
230
            } else {
231
                $nodeExpr = $this->createScalarNode($value);
232
            }
233
234 View Code Duplication
            if ($value instanceof \stdClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
                $nodeExpr = $this->appendChildrenToObjectNode($value);
236
            } elseif (is_array($value)) {
237
                $nodeExpr = $this->appendChildrenToArrayNode($value);
238
            }
239
240
            $expr = new MethodCall($expr, 'add', [new Arg(new String_($key)), new Arg($nodeExpr)]);
241
        }
242
243
        return $expr;
244
    }
245
}
246