Security::normalize()   C
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 16

Importance

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