Completed
Pull Request — master (#11)
by Lucas
33:48 queued 07:22
created

DneHelper::find()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 8.8571
cc 5
eloc 16
nc 1
nop 1
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))) {
44
45
            return true;
46
        }
47
48
        foreach ($filters as $i => $filter) {
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
class DneHelper
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

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...
134
{
135
136
    protected $url = 'http://dne.procergs.reders/dne/controller.jsp';
137
138
    protected $key = 'h3d8s74gf5';
139
140
    protected $ch;
141
142
    protected $cookie;
143
144
    protected $proxy;
145
146
    public function setProxy($var)
147
    {
148
        $this->proxy = $var;
149
    }
150
151
    private function _common($header = array())
0 ignored issues
show
Coding Style introduced by
function _common() 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...
152
    {
153
        $this->ch = curl_init();
154
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
155
        curl_setopt($this->ch, CURLOPT_HEADER, 0);
156
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
157
        if (ini_get('open_basedir')) {
158
159
        } else {
160
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
161
        }
162
        $proxy = $this->proxy;
163
        if (isset($proxy['type'], $proxy['host'], $proxy['port'])) {
164
            curl_setopt($this->ch, CURLOPT_PROXYTYPE, $proxy['type']);
165
            curl_setopt($this->ch, CURLOPT_PROXY, $proxy['host']);
166
            curl_setopt($this->ch, CURLOPT_PROXYPORT, $proxy['port']);
167
            if (isset($proxy['auth'])) {
168
                curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $proxy['auth']);
169
            }
170
        }
171
        $headApp = array(
172
            'Accept: */*',
173
            'Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
174
            'Connection: keep-alive',
175
            'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
176
            'Host: dne.procergs.reders',
177
            'Pragma: no-cache',
178
            'Referer: http://dne.procergs.reders/dne/',
179
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
180
            'X-Requested-With: XMLHttpRequest'
181
        );
182
        $headApp = array_merge($headApp, $header);
183
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headApp);
184
        if (! $this->cookie) {
185
            $tmp = getenv('TMPDIR');
186
            if ($tmp && @is_writable($tmp)) {
187
                $tmpDir = $tmp;
188
            } elseif (function_exists('sys_get_temp_dir') && @is_writable(sys_get_temp_dir())) {
189
                $tmpDir = sys_get_temp_dir();
190
            }
191
            $this->cookie = tempnam($tmpDir, "dne");
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...
192
        }
193
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie);
194
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie);
195
    }
196
197
    public function findByCep($var)
198
    {
199
        $this->_common();
200
        $data = http_build_query(array(
201
            'action' => 'buscaporcep',
202
            'key' => $this->key,
203
            'cep' => $var
204
        ));
205
        $url = $this->url . '?' . $data;
206
        curl_setopt($this->ch, CURLOPT_URL, $url);
207
        $result = curl_exec($this->ch);
208
        curl_close($this->ch);
209
        return $this->_decode($result);
210
    }
211
212
    public function find($var = array())
213
    {
214
        $this->_common();
215
        $data = http_build_query(array(
216
            'logradouro' => isset($var['logradouro']) ? $var['logradouro'] : '',
217
            'localidade' => isset($var['localidade']) ? $var['localidade'] : '',
218
            'state' => isset($var['state']) ? $var['state'] : '',
219
            'numero' => isset($var['numero']) ? $var['numero'] : ''
220
        ));
221
        $dataGet = http_build_query(array(
222
            'action' => 'pesquisa',
223
            'key' => $this->key
224
        ));
225
        curl_setopt($this->ch, CURLOPT_URL, $this->url . '?' . $dataGet);
226
        curl_setopt($this->ch, CURLOPT_POST, 1);
227
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
228
        $result = curl_exec($this->ch);
229
        curl_close($this->ch);
230
        return $this->_decode($result);
231
    }
232
233
    private function _decode(&$result) {
0 ignored issues
show
Coding Style introduced by
function _decode() 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...
234
        if ($result === false) {
235
            return false;
236
        }
237
        $dom = new \DOMDocument();
238
        if (! @$dom->loadXML($result)) {
239
            return false;
240
        }
241
        $node = $dom->getElementsByTagName('valor');
242
        if (! $node) {
243
            return false;
244
        }
245
        return json_decode($node->item(0)->nodeValue, true);
246
    }
247
248
}