ShortSyntaxArray::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 6
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Helper;
4
5
class ShortSyntaxArray
6
{
7
    /**
8
     * Parses an array and returns a string representation of this array with short syntax []
9
     *
10
     * @param array $expression
11
     * @param bool $removeNumericIndex
12
     * @param int $indent
13
     * @return string
14
     */
15
    public static function parse(array $expression, $removeNumericIndex = true, $indent = 4): string
16
    {
17
        $object = json_decode(str_replace(['(', ')'], ['&#40', '&#41'], json_encode($expression)), true);
18
        $export = str_replace(['array (', ')', '&#40', '&#41'], ['[', ']', '(', ')'], var_export($object, true));
19
        $export = preg_replace("/ => \n[^\S\n]*\[/m", ' => [', $export);
20
        $export = preg_replace("/ => \[\n[^\S\n]*\]/m", ' => []', $export);
21
        $spaces = str_repeat(' ', $indent);
22
        $export = preg_replace("/([ ]{2})(?![^ ])/m", $spaces, $export);
23
        $export = preg_replace("/^([ ]{2})/m", $spaces, $export);
24
25
        if ($removeNumericIndex) {
26
            $export = preg_replace("/([0-9]+) => /m", '', $export);
27
        }
28
29
        return $export;
30
    }
31
}
32