Completed
Branch master (ca1420)
by Guilherme
07:28 queued 32s
created

Zip_Manager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B filteredExtractTo() 0 23 4
A createDir() 0 8 3
B matchFileToFilter() 0 16 5
1
<?php
2
class Zip_Manager extends \ZipArchive
0 ignored issues
show
Coding Style introduced by
Zip_Manager does not seem to conform to the naming convention (^[A-Z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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)) {
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)
0 ignored issues
show
Coding Style introduced by
function matchFileToFilter() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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)) {
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
Bug introduced by
The variable $tmpDir does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
67
}
68
69
function file_get_contents_curl($url, $file, $referer = null)
0 ignored issues
show
Coding Style introduced by
function file_get_contents_curl() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
70
{
71
    $ch = curl_init();
72
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73
    $f = fopen($file, 'wb');
74
    if (! $f) {
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 );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
88
    }
89
    curl_close($ch);
90
    fclose($f);
91
    return $result;
92
}
93
94
function getPDOConnection($config)
95
{
96
    $db_driver = $config['database_driver'];
0 ignored issues
show
Coding Style introduced by
$db_driver does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
97
    $db_host = $config['database_host'];
0 ignored issues
show
Coding Style introduced by
$db_host does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
98
    $db_name = $config['database_name'];
0 ignored issues
show
Coding Style introduced by
$db_name does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
99
    $db_user = $config['database_user'];
0 ignored issues
show
Coding Style introduced by
$db_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
100
    $db_pass = $config['database_password'];
0 ignored issues
show
Coding Style introduced by
$db_pass does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
101
    $db_port = $config['database_port'];
0 ignored issues
show
Coding Style introduced by
$db_port does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
102
    $pdo = new PDO("$db_driver:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass);
0 ignored issues
show
Coding Style introduced by
$db_driver does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
function utf8_encode_recursivo() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
117
{
118
    if (is_array($in)) {
119
        foreach ($in as $key => $value) {
120
            $out[utf8_encode_recursivo($key)] = utf8_encode_recursivo($value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
        }
122
    } elseif(is_string($in)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSEIF keyword; 0 found
Loading history...
123
        if(!mb_check_encoding($in, 'UTF-8')
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
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
            }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
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
Bug introduced by
The variable $out does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
132
}
133