Completed
Push — master ( 89c1e6...70f342 )
by Patrick
03:41
created

SpreadSheetSerializer::getArray()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 9
eloc 31
c 2
b 0
f 1
nc 16
nop 1
dl 0
loc 51
rs 6.2727

How to fix   Long Method   

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
namespace Serialize;
3
4
abstract class SpreadSheetSerializer implements ISerializer
5
{
6 View Code Duplication
    public function supportsType($type)
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...
7
    {
8
        foreach($this->types as $t)
0 ignored issues
show
Bug introduced by
The property types does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
9
        {
10
            if(strcasecmp($t, $type) === 0)
11
            {
12
                return true;
13
            }
14
        }
15
        return false;
16
    }
17
18
    private function getKeysFromData($array)
19
    {
20
        $first = reset($array);
21
        if(is_array($first))
22
        {
23
            return array_keys($first);
24
        }
25
        else if(is_object($first))
26
        {
27
            return array_keys(get_object_vars($first));
28
        }
29
        return false;
30
    }
31
32
    private function getRowArray($row)
33
    {
34
        if(is_object($row))
35
        {
36
            return get_object_vars($row);
37
        }
38
        if(!is_array($row))
39
        {
40
            return array($row);
41
        }
42
        return $row;
43
    }
44
45
    protected function getArray(&$array)
46
    {
47
        $res = array();
48
        if(is_object($array))
49
        {
50
            $array = array(get_object_vars($array));
51
        }
52
        $keys = $this->getKeysFromData($array);
53
        if($keys === false)
54
        {
55
            return $array;
56
        }
57
        $colCount = count($keys);
58
        $res[] = $keys;
59
        foreach($array as $row)
60
        {
61
            $tmp = array();
62
            $row = $this->getRowArray($row);
63
            for($j = 0; $j < $colCount; $j++)
64
            {
65
                $colName = $keys[$j];
66
                if(isset($row[$colName]))
67
                {
68
                    $value = $row[$colName];
69
                    if(is_object($value))
70
                    {
71
                        switch($colName)
72
                        {
73
                            case '_id':
74
                                $value = $value->{'$id'};
75
                                break;
76
                            default:
77
                                $value = json_encode($value);
78
                                break;
79
                        }
80
                    }
81
                    else if(is_array($value))
82
                    {
83
                        $value = implode(',', $value);
84
                    }
85
                    $tmp[] = $value;
86
                }
87
                else
88
                {
89
                    $tmp[] = false;
90
                }
91
            }
92
            $res[] = $tmp;
93
        }
94
        return $res;
95
    }
96
}
97
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
98