Passed
Push — master ( 903d39...4779f1 )
by Jonathan
14:53
created

ArrayUtils   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 28
dl 0
loc 47
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A varExportToFile() 0 36 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Stdlib;
16
17
use Riimu\Kit\PHPEncoder\PHPEncoder;
18
19
/**
20
 * Class ArrayUtils
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
class ArrayUtils extends AbstractStdlib
26
{
27
    /**
28
     * Write the array of passed data to a file as a PHP structure
29
     *
30
     * @param string      $filename
31
     * @param array       $values
32
     * @param string|null $generator
33
     *
34
     * @return int
35
     */
36 6
    public static function varExportToFile(string $filename, array $values, ?string $generator = null): int
37
    {
38
        $options = [
39 6
            'array.indent'  => 4,
40
            'array.align'   => true,
41
            'array.omit'    => true,
42
            'array.short'   => true,
43
            'object.format' => 'export',
44
            'string.utf8'   => true,
45
            'whitespace'    => true,
46
        ];
47
48 6
        $encoder = new PHPEncoder($options);
49
50 6
        $format = '<?php';
51 6
        $format .= PHP_EOL;
52 6
        $format .= 'declare(strict_types=1);';
53 6
        $format .= PHP_EOL . PHP_EOL;
54 6
        $format .= '%s';
55 6
        $format .= 'return ';
56 6
        $format .= '%s';
57 6
        $format .= ';';
58 6
        $format .= PHP_EOL;
59
60 6
        if (empty($generator)) {
61 3
            $prefix = '';
62
        } else {
63 3
            $prefix = sprintf('// File generated by %s.', $generator);
64 3
            $prefix .= PHP_EOL;
65 3
            $prefix .= '// Do not edit.';
66 3
            $prefix .= PHP_EOL . PHP_EOL;
67
        }
68
69 6
        $buffer = sprintf($format, $prefix, $encoder->encode($values));
70
71 6
        return (int) file_put_contents($filename, $buffer);
72
    }
73
}
74