Completed
Push — master ( 1a9d0b...536152 )
by
unknown
30s
created

Security::normalize()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 45
Code Lines 28

Duplication

Lines 45
Ratio 100 %

Code Coverage

Tests 27
CRAP Score 16

Importance

Changes 0
Metric Value
dl 45
loc 45
ccs 27
cts 27
cp 1
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 28
nc 16
nop 1
crap 16

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Zewa;
4
5
class Security
6
{
7
    /**
8
     * Normalizes data
9
     *
10
     * @access public
11
     * @TODO:  expand functionality, set/perform based on configuration
12
     */
13
    //@TODO clean up
14
15 7 View Code Duplication
    public function normalize($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17 7
        if (!isset($data)) {
18 1
            return null;
19
        }
20
21 6
        if (is_array($data)) {
22 1
            foreach ($data as $key => $value) {
23 1
                unset($data[$key]);
24 1
                $data[$this->normalize($key)] = $this->normalize($value);
25
            }
26 6
        } elseif (is_object($data)) {
27 1
            $new = new \stdClass();
28 1
            foreach ($data as $k => $v) {
29 1
                $key = $this->normalize($k);
30 1
                $new->{$key} = $this->normalize($v);
31
            }
32 1
            $data = $new;
33
        } else {
34 6
            $data = trim($data);
35
            //we need to review this.
36 6
            if (function_exists('iconv') && function_exists('mb_detect_encoding')) {
37 6
                $current_encoding = mb_detect_encoding($data);
38
39 6
                if ($current_encoding != 'UTF-8' && $current_encoding != 'UTF-16') {
40 6
                    $data = iconv($current_encoding, 'UTF-8', $data);
41
                }
42
            }
43
44 6
            if (is_numeric($data)) {
45 3
                $int = intval($data);
46 3
                $float = floatval($data);
47 3
                $re = "~^-?[0-9]+(\.[0-9]+)?$~xD";
48
                //@TODO this will not accept all float values, this validates /against/ syntax
49
50 3
                if (($int === (int)trim($data, '-')) && strlen((string)(int)$data) === strlen($data)) {
51 2
                    $data = (int) $data;
52 1
                } elseif ($int !== $float && preg_match($re, $data) === 1 && strlen($data) === strlen($float)) {
53 1
                    $data = $float;
54
                }
55
            }
56
        }
57
58 6
        return $data;
59
    }
60
}
61