ShortSyntaxArray   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 16 2
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