Utils   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 82.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 50
ccs 14
cts 17
cp 0.8235
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B gzCompressFile() 0 24 5
A removeEmptyAndNullJson() 0 4 1
1
<?php
2
3
namespace ADiaz\AML\OpenList\utils;
4
5
/**
6
 * This file is part of the OpenList Parser utility.
7
 *
8
 * @category  PHP
9
 *
10
 * @author    Alberto Diaz <[email protected]>
11
 * @copyright 2016 Alberto Diaz <[email protected]>
12
 * @license   This source file is subject to the MIT license that is bundled
13
 *
14
 * @version Release: @package_version@
15
 *
16
 * @link http://tytem.com
17
 */
18
class Utils
19
{
20
    /**
21
     * GZIPs a file on disk (appending .gz to the name).
22
     *
23
     * Based on function by Simon East at:  http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
24
     *
25
     * @param string $source  Path to file that should be compressed
26
     * @param string $newFile new filename
27
     * @param int    $level   GZIP compression level (default: 9)
28
     *
29
     * @return string New filename (with .gz appended) if success, or false if operation fails
30
     */
31 1
    public static function gzCompressFile($source, $newFile, $level = 9)
32
    {
33 1
        $dest = $newFile.'.gz';
34 1
        $mode = 'wb'.$level;
35 1
        $error = false;
36 1
        if ($fp_out = gzopen($dest, $mode)) {
37 1
            if ($fp_in = fopen($source, 'rb')) {
38 1
                while (!feof($fp_in)) {
39 1
                    gzwrite($fp_out, fread($fp_in, 1024 * 512));
40
                }
41 1
                fclose($fp_in);
42
            } else {
43
                $error = true;
44
            }
45 1
            gzclose($fp_out);
46
        } else {
47
            $error = true;
48
        }
49 1
        if ($error) {
50
            return false;
51
        } else {
52 1
            return $dest;
53
        }
54
    }
55
56
    /**
57
     * Utility function to remove empty values and null from json strings.
58
     *
59
     * @param string $json in json
60
     *
61
     * @return mixed cleaned
62
     */
63 10
    public static function removeEmptyAndNullJson($json)
64
    {
65 10
        return preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', preg_replace('/\[\]/', 'null', $json));
66
    }
67
}
68