Completed
Push — master ( 19b0aa...464faa )
by Nate
02:40
created

MethodBodyBuilder   D

Complexity

Total Complexity 75

Size/Duplication

Total Lines 668
Duplicated Lines 12.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 28
Bugs 1 Features 2
Metric Value
wmc 75
c 28
b 1
f 2
lcom 1
cbo 1
dl 83
loc 668
rs 4.5772

34 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setBaseUrl() 0 4 1
A setRequestMethod() 0 4 1
A setUri() 0 4 1
A setQueries() 0 4 1
A setQueryMap() 0 4 1
A setHeaders() 0 4 1
A setBody() 0 4 1
A setBodyParts() 0 4 1
A setBodyIsObject() 0 4 1
A setBodyIsOptional() 0 4 1
A setBodyIsJsonSerializable() 0 4 1
A setBodyIsArray() 0 4 1
A setJsonEncode() 0 4 1
A setFormUrlEncoded() 0 4 1
A setMultipartEncoded() 0 4 1
A setBoundaryId() 0 4 1
A setReturnType() 0 4 1
A setSerializationContext() 0 4 1
A setDeserializationContext() 0 4 1
A setCallback() 0 4 1
A setCallbackOptional() 0 4 1
A build() 0 12 2
B createRequestUrl() 0 30 5
A createHeaders() 0 10 2
B createBody() 0 20 5
C doCreateBody() 0 42 8
B createBodyJson() 27 27 5
B createBodyArray() 27 27 4
A serializeObject() 0 9 2
B createResponse() 11 30 5
C createReturns() 13 42 7
C createContext() 5 24 7
A arrayToString() 0 7 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MethodBodyBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MethodBodyBuilder, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * Copyright (c) 2015 Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Retrofit\Generation\Builder;
8
9
use Tebru;
10
use Tebru\Dynamo\Model\Body;
11
12
/**
13
 * Class MethodBodyBuilder
14
 *
15
 * @author Nate Brunette <[email protected]>
16
 */
17
class MethodBodyBuilder
18
{
19
    /**
20
     * @var Body
21
     */
22
    private $methodBody;
23
    
24
    /**
25
     * @var string
26
     */
27
    private $baseUrl;
28
29
    /**
30
     * Request method
31
     *
32
     * @var string
33
     */
34
    private $requestMethod;
35
36
    /**
37
     * Request uri
38
     *
39
     * @var string
40
     */
41
    private $uri;
42
43
    /**
44
     * Array of query parameters
45
     *
46
     * @var array
47
     */
48
    private $queries = [];
49
50
    /**
51
     * Variable name of query map argument
52
     *
53
     * @var string
54
     */
55
    private $queryMap;
56
57
    /**
58
     * Request headers
59
     *
60
     * @var array
61
     */
62
    private $headers = [];
63
64
    /**
65
     * A string of php code to describe how to create the body
66
     *
67
     * @var string
68
     */
69
    private $body;
70
71
    /**
72
     * An array of body parts
73
     *
74
     * @var array
75
     */
76
    private $bodyParts = [];
77
78
    /**
79
     * True if the body is an object
80
     *
81
     * @var bool
82
     */
83
    private $bodyIsObject = false;
84
85
    /**
86
     * True if the body is an optional paramter
87
     *
88
     * @var bool
89
     */
90
    private $bodyIsOptional = false;
91
92
    /**
93
     * True if the body implements \JsonSerializable
94
     *
95
     * @var boolean
96
     */
97
    private $bodyIsJsonSerializable;
98
99
    /**
100
     * True if the body is an array
101
     *
102
     * @var bool
103
     */
104
    private $bodyIsArray = false;
105
106
    /**
107
     * If we should json encode the body
108
     *
109
     * @var bool
110
     */
111
    private $jsonEncode = false;
112
113
    /**
114
     * If the body should be sent form urlencoded
115
     *
116
     * @var bool
117
     */
118
    private $formUrlEncoded = false;
119
120
    /**
121
     * If the body should be sent as multipart
122
     *
123
     * @var bool
124
     */
125
    private $multipartEncoded = false;
126
127
    /**
128
     * Method return type
129
     *
130
     * @var string
131
     */
132
    private $returnType = 'array';
133
134
    /**
135
     * JMS Serializer serialization context
136
     *
137
     * @var array
138
     */
139
    private $serializationContext = [];
140
141
    /**
142
     * JMS Serializer deserialization attributes
143
     *
144
     * @var array
145
     */
146
    private $deserializationContext = [];
147
148
    /**
149
     * Request callback variable name
150
     *
151
     * @var string
152
     */
153
    private $callback;
154
155
    /**
156
     * Async callback is optional
157
     *
158
     * @var bool
159
     */
160
    private $callbackOptional = false;
161
162
    /**
163
     * Boundary Id for multipart requests
164
     *
165
     * @var string
166
     */
167
    private $boundaryId;
168
169
    /**
170
     * Constructor
171
     */
172
    public function __construct()
173
    {
174
        $this->methodBody = new Body();
175
    }
176
177
    /**
178
     * @param string $baseUrl
179
     */
180
    public function setBaseUrl($baseUrl)
181
    {
182
        $this->baseUrl = $baseUrl;
183
    }
184
185
    /**
186
     * @param string $requestMethod
187
     */
188
    public function setRequestMethod($requestMethod)
189
    {
190
        $this->requestMethod = $requestMethod;
191
    }
192
193
    /**
194
     * @param string $uri
195
     */
196
    public function setUri($uri)
197
    {
198
        $this->uri = $uri;
199
    }
200
201
    /**
202
     * @param array $queries
203
     */
204
    public function setQueries(array $queries)
205
    {
206
        $this->queries = $queries;
207
    }
208
209
    /**
210
     * @param string $queryMap
211
     */
212
    public function setQueryMap($queryMap)
213
    {
214
        $this->queryMap = $queryMap;
215
    }
216
217
    /**
218
     * @param array $headers
219
     */
220
    public function setHeaders(array $headers)
221
    {
222
        $this->headers = $headers;
223
    }
224
225
    /**
226
     * @param string $body
227
     */
228
    public function setBody($body)
229
    {
230
        $this->body = $body;
231
    }
232
233
    /**
234
     * @param array $bodyParts
235
     */
236
    public function setBodyParts(array $bodyParts)
237
    {
238
        $this->bodyParts = $bodyParts;
239
    }
240
241
    /**
242
     * @param boolean $bodyIsObject
243
     */
244
    public function setBodyIsObject($bodyIsObject)
245
    {
246
        $this->bodyIsObject = $bodyIsObject;
247
    }
248
249
    /**
250
     * @param boolean $bodyIsOptional
251
     */
252
    public function setBodyIsOptional($bodyIsOptional)
253
    {
254
        $this->bodyIsOptional = $bodyIsOptional;
255
    }
256
257
    /**
258
     * @param boolean $bodyIsJsonSerializable
259
     */
260
    public function setBodyIsJsonSerializable($bodyIsJsonSerializable)
261
    {
262
        $this->bodyIsJsonSerializable = $bodyIsJsonSerializable;
263
    }
264
265
    /**
266
     * @param boolean $bodyIsArray
267
     */
268
    public function setBodyIsArray($bodyIsArray)
269
    {
270
        $this->bodyIsArray = $bodyIsArray;
271
    }
272
273
    /**
274
     * @param boolean $jsonEncode
275
     */
276
    public function setJsonEncode($jsonEncode)
277
    {
278
        $this->jsonEncode = $jsonEncode;
279
    }
280
281
    /**
282
     * @param boolean $formUrlEncoded
283
     */
284
    public function setFormUrlEncoded($formUrlEncoded)
285
    {
286
        $this->formUrlEncoded = $formUrlEncoded;
287
    }
288
289
    /**
290
     * @param boolean $multipartEncoded
291
     */
292
    public function setMultipartEncoded($multipartEncoded)
293
    {
294
        $this->multipartEncoded = $multipartEncoded;
295
    }
296
297
    /**
298
     * @param string $boundaryId
299
     */
300
    public function setBoundaryId($boundaryId)
301
    {
302
        $this->boundaryId = $boundaryId;
303
    }
304
305
    /**
306
     * @param string $returnType
307
     */
308
    public function setReturnType($returnType)
309
    {
310
        $this->returnType = $returnType;
311
    }
312
313
    /**
314
     * @param array $serializationContext
315
     */
316
    public function setSerializationContext(array $serializationContext)
317
    {
318
        $this->serializationContext = $serializationContext;
319
    }
320
321
    /**
322
     * @param array $deserializationContext
323
     */
324
    public function setDeserializationContext(array $deserializationContext)
325
    {
326
        $this->deserializationContext = $deserializationContext;
327
    }
328
329
    /**
330
     * @param string $callback
331
     */
332
    public function setCallback($callback)
333
    {
334
        $this->callback = $callback;
335
    }
336
337
    /**
338
     * @param boolean $callbackOptional
339
     */
340
    public function setCallbackOptional($callbackOptional)
341
    {
342
        $this->callbackOptional = $callbackOptional;
343
    }
344
345
    /**
346
     * Build the method body
347
     *
348
     * @return string
349
     */
350
    public function build()
351
    {
352
        Tebru\assertThat(null === $this->body || empty($this->bodyParts), 'Cannot have both @Body and @Part annotations');
353
354
        $this->createRequestUrl();
355
        $this->createHeaders();
356
        $this->createBody();
357
        $this->createResponse();
358
        $this->createReturns();
359
360
        return (string) $this->methodBody;
361
    }
362
363
    /**
364
     * Build the request url
365
     */
366
    private function createRequestUrl()
367
    {
368
        Tebru\assertNotNull($this->uri, 'Request annotation not found (e.g. @GET, @POST)');
369
370
        $baseUrl = (null !== $this->baseUrl) ? $this->baseUrl : '$this->baseUrl';
371
372
        // request request params using http_build_query if we have a query map
373
        if (null !== $this->queryMap) {
374
            // if we have regular queries, add them to the query builder
375
            if (!empty($this->queries)) {
376
                $queryArray = $this->arrayToString($this->queries);
377
                $this->methodBody->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s + %s);', $queryArray, $this->queryMap);
378
                $this->methodBody->add('$queryString = http_build_query($queryArray);');
379
            } else {
380
                $this->methodBody->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s);', $this->queryMap);
381
                $this->methodBody->add('$queryString = http_build_query($queryArray);');
382
            }
383
384
            $this->methodBody->add('$requestUrl = %s . "%s?" . $queryString;', $baseUrl, $this->uri);
385
386
            // if we have queries, add them to the request url
387
        } elseif (!empty($this->queries)) {
388
            $queryArray = $this->arrayToString($this->queries);
389
            $this->methodBody->add('$queryArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString(%s);', $queryArray);
390
            $this->methodBody->add('$queryString = http_build_query($queryArray);');
391
            $this->methodBody->add('$requestUrl = %s . "%s" . "?" . $queryString;', $baseUrl, $this->uri);
392
        } else {
393
            $this->methodBody->add('$requestUrl = %s . "%s";', $baseUrl, $this->uri);
394
        }
395
    }
396
397
    /**
398
     * Build the headers
399
     */
400
    private function createHeaders()
401
    {
402
        if (empty($this->headers)) {
403
            $this->methodBody->add('$headers = [];');
404
405
            return;
406
        }
407
408
        $this->methodBody->add('$headers = %s;', $this->arrayToString($this->headers));
409
    }
410
411
    /**
412
     * Build the request body
413
     */
414
    private function createBody()
415
    {
416
        // body should be null if there isn't a body or parts
417
        if (null === $this->body && empty($this->bodyParts)) {
418
            $this->methodBody->add('$body = null;');
419
420
            return;
421
        }
422
423
        // if body is optional and 'empty' it should be null
424
        if ($this->bodyIsOptional) {
425
            $this->methodBody->add('if (empty(%s)) { $body = null; } else {', $this->body);
426
        }
427
428
        $this->doCreateBody();
429
430
        if ($this->bodyIsOptional) {
431
            $this->methodBody->add('}');
432
        }
433
    }
434
435
    /**
436
     * Logic to actually create body
437
     */
438
    private function doCreateBody()
439
    {
440
        // check if body is a string, set variable if it doesn't already equal '$body'
441
        if (!$this->bodyIsArray && !$this->bodyIsObject && empty($this->bodyParts)) {
442
            if ('$body' !== $this->body) {
443
                $this->methodBody->add('$body = %s;', $this->body);
444
            }
445
446
            return;
447
        }
448
449
        // if we're json encoding, we don't need the body as an array first
450
        if ($this->jsonEncode) {
451
            $this->createBodyJson();
452
453
            return;
454
        }
455
456
        // otherwise, we do need to convert the body to an array
457
        $this->createBodyArray();
458
459
        if ($this->formUrlEncoded) {
460
            $this->methodBody->add('$bodyArray = \Tebru\Retrofit\Generation\Manipulator\QueryManipulator::boolToString($bodyArray);');
461
            $this->methodBody->add('$body = http_build_query($bodyArray);');
462
463
            return;
464
        }
465
466
        // body is multipart
467
        if ($this->multipartEncoded) {
468
            $this->methodBody->add('$bodyParts = [];');
469
            $this->methodBody->add('foreach ($bodyArray as $key => $value) {');
470
            $this->methodBody->add('$file = null;');
471
            $this->methodBody->add('if (is_resource($value)) { $file = $value; }');
472
            $this->methodBody->add('if (is_string($value)) { $file = fopen($value, "r"); }');
473
            $this->methodBody->add('if (!is_resource($file)) { throw new \LogicException("Expected resource or file path"); }');
474
            $this->methodBody->add('$bodyParts[] = ["name" => $key, "contents" => $file];');
475
            $this->methodBody->add('}');
476
477
            $this->methodBody->add('$body = new \GuzzleHttp\Psr7\MultipartStream($bodyParts, "%s");', $this->boundaryId);
478
        }
479
    }
480
481
    /**
482
     * Create json body
483
     */
484 View Code Duplication
    private function createBodyJson()
1 ignored issue
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...
485
    {
486
        // json encode arrays
487
        if ($this->bodyIsArray) {
488
            $this->methodBody->add('$body = json_encode(%s);', $this->body);
489
490
            return;
491
        }
492
493
        // if parts exist, json encode the parts
494
        if (!empty($this->bodyParts)) {
495
            $this->methodBody->add('$body = json_encode(%s);', $this->arrayToString($this->bodyParts));
496
497
            return;
498
        }
499
500
        // if it's an object, serialize it unless it implements \JsonSerializable
501
        if ($this->bodyIsObject) {
502
            if ($this->bodyIsJsonSerializable) {
503
                $this->methodBody->add('$body = json_encode(%s);', $this->body);
504
505
                return;
506
            }
507
508
            $this->serializeObject('$bodySerializationContext', '$body', $this->body);
509
        }
510
    }
511
512
    /**
513
     * Normalize body as array
514
     */
515 View Code Duplication
    private function createBodyArray()
1 ignored issue
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...
516
    {
517
        // if it's already an array, set to variable
518
        if ($this->bodyIsArray) {
519
            $this->methodBody->add('$bodyArray = %s;', $this->body);
520
521
            return;
522
        }
523
524
        // if parts exist, set to variable
525
        if (!empty($this->bodyParts)) {
526
            $this->methodBody->add('$bodyArray = %s;', $this->arrayToString($this->bodyParts));
527
528
            return;
529
        }
530
531
        // if it implements \JsonSerializable, call jsonSerialize() to get array
532
        if ($this->bodyIsJsonSerializable) {
533
            $this->methodBody->add('$bodyArray = %s->jsonSerialize();', $this->body);
534
535
            return;
536
        }
537
538
        // otherwise, serialize and json_decode()
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
539
        $this->serializeObject('$bodySerializationContext', '$serializedBody', $this->body);
540
        $this->methodBody->add('$bodyArray = json_decode($serializedBody, true);');
541
    }
542
543
    /**
544
     * Helper method to serialize an object
545
     *
546
     * @param string $contextVar
547
     * @param string $bodyVar
548
     * @param string $object
549
     */
550
    private function serializeObject($contextVar, $bodyVar, $object)
551
    {
552
        $this->methodBody->add('%s = \JMS\Serializer\SerializationContext::create();', $contextVar);
553
        if (!empty($this->serializationContext)) {
554
            $this->createContext($contextVar, $this->serializationContext);
555
        }
556
557
        $this->methodBody->add('%s = $this->serializer->serialize(%s, "json", %s);', $bodyVar, $object, $contextVar);
558
    }
559
560
    /**
561
     * Build the response
562
     */
563
    private function createResponse()
564
    {
565
        $this->methodBody->add('$request = new \GuzzleHttp\Psr7\Request("%s", $requestUrl, $headers, $body);', strtoupper($this->requestMethod));
566
        $this->methodBody->add('$beforeSendEvent = new \Tebru\Retrofit\Event\BeforeSendEvent($request);');
567
        $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.beforeSend", $beforeSendEvent);');
568
        $this->methodBody->add('$request = $beforeSendEvent->getRequest();');
569
        $this->methodBody->add('try {');
570
571 View Code Duplication
        if ($this->callback !== null && $this->callbackOptional) {
1 ignored issue
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...
572
            $this->methodBody->add('if (%s !== null) {', $this->callback);
573
            $this->methodBody->add('$response = $this->client->sendAsync($request, %s);', $this->callback);
574
            $this->methodBody->add('} else {');
575
            $this->methodBody->add('$response = $this->client->send($request);');
576
            $this->methodBody->add('}');
577
        } elseif ($this->callback !== null && !$this->callbackOptional) {
578
            $this->methodBody->add('$response = $this->client->sendAsync($request, %s);', $this->callback);
579
        } else {
580
            $this->methodBody->add('$response = $this->client->send($request);');
581
        }
582
583
        $this->methodBody->add('} catch (\Exception $exception) {');
584
        $this->methodBody->add('$apiExceptionEvent = new \Tebru\Retrofit\Event\ApiExceptionEvent($exception, $request);');
585
        $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.apiException", $apiExceptionEvent);');
586
        $this->methodBody->add('$exception = $apiExceptionEvent->getException();');
587
        $this->methodBody->add('throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);');
588
        $this->methodBody->add('}');
589
        $this->methodBody->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);');
590
        $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);');
591
        $this->methodBody->add('$response = $afterSendEvent->getResponse();');
592
    }
593
594
    /**
595
     * Build the return
596
     */
597
    private function createReturns()
598
    {
599 View Code Duplication
        if ($this->callback !== null && $this->callbackOptional) {
1 ignored issue
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...
600
            $this->methodBody->add('if (%s !== null) {', $this->callback);
601
            $this->methodBody->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);');
602
            $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);');
603
            $this->methodBody->add('return $returnEvent->getReturn();');
604
            $this->methodBody->add('}');
605
        } elseif ($this->callback !== null && !$this->callbackOptional) {
606
            $this->methodBody->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);');
607
            $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);');
608
            $this->methodBody->add('return $returnEvent->getReturn();');
609
610
            return;
611
        }
612
613
        $matches = [];
614
        $returnType = $this->returnType;
615
        $responseReturn = false;
616
        preg_match('/^Response<(.+)>$/', $returnType, $matches);
617
618
        if (isset($matches[1])) {
619
            $returnType = $matches[1];
620
            $responseReturn = true;
621
        }
622
623
        $this->methodBody->add(
624
            '$retrofitResponse = new \Tebru\Retrofit\Http\Response($response, "%s", $this->serializer, %s);',
625
            $returnType,
626
            $this->arrayToString($this->deserializationContext)
627
        );
628
629
        if ($responseReturn) {
630
            $this->methodBody->add('$return = $retrofitResponse;');
631
        } else {
632
            $this->methodBody->add('$return = $retrofitResponse->body();');
633
        }
634
635
        $this->methodBody->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent($return);');
636
        $this->methodBody->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);');
637
        $this->methodBody->add('return $returnEvent->getReturn();');
638
    }
639
640
    /**
641
     * Build the serialization context
642
     *
643
     * @param $contextVar
644
     * @param $context
645
     */
646
    private function createContext($contextVar, &$context)
647
    {
648
        if (!empty($context['groups'])) {
649
            $this->methodBody->add('%s->setGroups(%s);', $contextVar, $this->arrayToString($context['groups']));
650
        }
651
652
        if (!empty($context['version'])) {
653
            $this->methodBody->add('%s->setVersion(%d);', $contextVar, (int) $context['version']);
654
        }
655
656
        if (!empty($context['serializeNull'])) {
657
            $this->methodBody->add('%s->setSerializeNull(%d);', $contextVar, (bool) $context['serializeNull']);
658
        }
659
660
        if (!empty($context['enableMaxDepthChecks'])) {
661
            $this->methodBody->add('%s->enableMaxDepthChecks();', $contextVar);
662
        }
663
664 View Code Duplication
        if (!empty($context['attributes'])) {
1 ignored issue
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...
665
            foreach ($context['attributes'] as $key => $value) {
666
                $this->methodBody->add('%s->setAttribute("%s", "%s");', $contextVar, $key, $value);
667
            }
668
        }
669
    }
670
671
    /**
672
     * Create a string representation of an array
673
     *
674
     * @param array $array
675
     * @return string
676
     */
677
    private function arrayToString(array $array)
678
    {
679
        $string = var_export($array, true);
680
        $string = preg_replace('/\'\$(.+)\'/', '$' . '\\1', $string);
681
682
        return $string;
683
    }
684
}
685