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

SpreadSheetSerializer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 11.83 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 11
loc 93
rs 10
wmc 18
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsType() 11 11 3
A getKeysFromData() 0 13 3
A getRowArray() 0 12 3
C getArray() 0 51 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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