Completed
Push — master ( fe643b...2e3f04 )
by Patrick
03:45
created

SpreadSheetSerializer::getArray()   C

Complexity

Conditions 12
Paths 150

Size

Total Lines 63
Code Lines 37

Duplication

Lines 8
Ratio 12.7 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 12
eloc 37
nc 150
nop 1
dl 8
loc 63
rs 5.5796
c 1
b 0
f 1

How to fix   Long Method    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
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
    protected function getArray(&$array)
19
    {
20
        $res = array();
21
        if(is_object($array))
22
        {
23
            $array = get_object_vars($array);
24
        }
25
        $first = reset($array);
26
        $keys = false;
27 View Code Duplication
        if(is_array($first))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
        {
29
            $keys = array_keys($first);
30
        }
31
        else if(is_object($first))
32
        {
33
            $keys = array_keys(get_object_vars($first));
34
        }
35
        $colCount = count($keys);
36
        $res[] = $keys;
37
        foreach($array as $row)
38
        {
39
            $tmp = array();
40
            if(is_object($row))
41
            {
42
                $row = get_object_vars($row);
43
            }
44
            if(!is_array($row))
45
            {
46
                $row = array($row);
47
            }
48
            for($j = 0; $j < $colCount; $j++)
49
            {
50
                $colName = $keys[$j];
51
                if(isset($row[$colName]))
52
                {
53
                    $value = $row[$colName];
54
                    if(is_object($value))
55
                    {
56
                        switch($colName)
57
                        {
58
                            case '_id':
59
                                $value = $value->{'$id'};
60
                                break;
61
                            default:
62
                                $value = json_encode($value);
63
                                break;
64
                        }
65
                    }
66
                    else if(is_array($value))
67
                    {
68
                        $value = implode(',', $value);
69
                    }
70
                    $tmp[] = $value;
71
                }
72
                else
73
                {
74
                    $tmp[] = false;
75
                }
76
            }
77
            $res[] = $tmp;
78
        }
79
        return $res;
80
    }
81
}
82
?>
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...
83