Failed Conditions
Push — issue#764 ( 39afc8 )
by Guilherme
11:43
created

utf8_encode_recursivo()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
class Zip_Manager extends \ZipArchive
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
3
{
4
5
    const CHMOD = 0755;
6
7
    public function filteredExtractTo($directory, array $filters = null)
8
    {
9
        if (count($filters) === 0) {
10
            return $this->extractTo($directory);
11
        }
12
13
        $this->createDir($directory);
14
15
        $copySource = 'zip://'.$this->filename.'#';
16
        for ($i = 0; $i < $this->numFiles; $i++) {
17
            $entry = $this->getNameIndex($i);
18
            $filename = basename($entry);
19
20
            if ($this->matchFileToFilter($filename, $filters)) {
0 ignored issues
show
Bug introduced by
It seems like $filters can also be of type null; however, parameter $filters of Zip_Manager::matchFileToFilter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
            if ($this->matchFileToFilter($filename, /** @scrutinizer ignore-type */ $filters)) {
Loading history...
21
                $base = dirname($entry);
22
                $newPath = $directory.DIRECTORY_SEPARATOR.$base.DIRECTORY_SEPARATOR;
23
                $this->createDir($newPath);
24
25
                // extract file
26
                copy($copySource.$entry, $newPath.$filename);
27
            }
28
        }
29
    }
30
31
    protected function createDir($path)
32
    {
33
        if (!is_dir($path)) {
34
            if (!mkdir($path, self::CHMOD, true)) {
35
                throw new Exception('unable to create path '.$path);
36
            }
37
        }
38
    }
39
40
    protected function matchFileToFilter($filename, array $filters)
41
    {
42
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
43
        if (in_array($ext, array_map('strtolower', $filters))) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
44
45
            return true;
46
        }
47
48
        foreach ($filters as $i => $filter) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
49
50
            if (!ctype_alnum($filter[0]) && preg_match($filter, $filename)) {
51
                return true;
52
            }
53
        }
54
        return false;
55
    }
56
}
57
58
function getTempNam($prefix = "php")
59
{
60
    $tmp = getenv('TMPDIR');
61
    if ($tmp && @is_writable($tmp)) {
0 ignored issues
show
Bug introduced by
It seems like $tmp can also be of type array; however, parameter $filename of is_writable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    if ($tmp && @is_writable(/** @scrutinizer ignore-type */ $tmp)) {
Loading history...
62
        $tmpDir = $tmp;
63
    } elseif (function_exists('sys_get_temp_dir') && @is_writable(sys_get_temp_dir())) {
64
        $tmpDir = sys_get_temp_dir();
65
    }
66
    return tempnam($tmpDir, $prefix);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tmpDir does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
}
68
69
function file_get_contents_curl($url, $file, $referer = null)
70
{
71
    $ch = curl_init();
72
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73
    $f = fopen($file, 'wb');
74
    if (!$f) {
0 ignored issues
show
introduced by
The condition $f is always false.
Loading history...
75
        return false;
76
    }
77
    curl_setopt($ch, CURLOPT_FILE, $f);
78
    curl_setopt($ch, CURLOPT_HEADER, 0);
79
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
80
    curl_setopt($ch, CURLOPT_ENCODING, '');
81
    curl_setopt($ch, CURLOPT_URL, $url);
82
    if ($referer) {
83
        curl_setopt($ch, CURLOPT_REFERER, $referer);
84
    }
85
    $result = curl_exec($ch);
86
    if (!$result) {
87
        echo curl_error($ch);
88
    }
89
    curl_close($ch);
90
    fclose($f);
91
    return $result;
92
}
93
94
function getPDOConnection($config)
95
{
96
    $db_driver = $config['database_driver'];
97
    $db_host = $config['database_host'];
98
    $db_name = $config['database_name'];
99
    $db_user = $config['database_user'];
100
    $db_pass = $config['database_password'];
101
    $db_port = $config['database_port'];
102
    $pdo = new PDO("$db_driver:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass);
103
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
104
    $pdo->query("SET NAMES 'UTF8'");
105
    return $pdo;
106
}
107
function trim2($s)
108
{
109
    $s = trim($s, " \t\n\r\0\x0B-");
110
    if ('' === $s) {
111
        return null;
112
    } else {
113
        return $s;
114
    }
115
}
116
function utf8_encode_recursivo($in)
117
{
118
    if (is_array($in)) {
119
        foreach ($in as $key => $value) {
120
            $out[utf8_encode_recursivo($key)] = utf8_encode_recursivo($value);
121
        }
122
    } elseif (is_string($in)) {
123
        if (!mb_check_encoding($in, 'UTF-8')
124
            OR !($in === mb_convert_encoding(mb_convert_encoding($in, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32'))) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
125
                $in = mb_convert_encoding($in, 'UTF-8');
126
            }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 12
Loading history...
127
            return $in;
128
    } else {
129
        return $in;
130
    }
131
    return $out;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $out seems to be defined by a foreach iteration on line 119. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
132
}
133