Completed
Push — master ( acc0c8...339298 )
by Patrick
03:05
created

ModelFactory::make()   C

Complexity

Conditions 12
Paths 34

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 12.2812

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 21
cts 24
cp 0.875
rs 6.446
c 0
b 0
f 0
cc 12
nc 34
nop 1
crap 12.2812

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
3
namespace Kunnu\Dropbox\Models;
4
5
class ModelFactory
6
{
7
8
    /**
9
     * Make a Model Factory
10
     *
11
     * @param  array $data Model Data
12
     *
13
     * @return \Kunnu\Dropbox\Models\ModelInterface
14
     */
15 22
    public static function make(array $data = array())
16
    {
17
        //Some endpoints return metadata files or folder as sub-nodes of a `metadata` node
18
        //i.e. /files/move_v2
19 22
        if (isset($data['metadata']['.tag'])) {
20 2
            $data = $data['metadata'];
21
        }
22
23 22
        if (static::isFileOrFolder($data)) {
24 20
            $tag = $data['.tag'];
25
26
            //File
27 20
            if (static::isFile($tag)) {
28 13
                return new FileMetadata($data);
29
            }
30
31
            //Folder
32 11
            if (static::isFolder($tag)) {
33 10
                return new FolderMetadata($data);
34
            }
35
36
            //Deleted File/Folder
37 1
            if (static::isDeletedFileOrFolder($tag)) {
38 1
                return new DeletedMetadata($data);
39
            }
40
        }
41
42
        //Temporary Link
43 9
        if (static::isTemporaryLink($data)) {
44 1
            return new TemporaryLink($data);
45
        }
46
47
        //List
48 8
        if (static::isList($data)) {
49 4
            return new MetadataCollection($data);
50
        }
51
52
        //Search Results
53 4
        if (static::isSearchResult($data)) {
54 3
            return new SearchResults($data);
55
        }
56
57
        //File Requests List
58 1
        if (static::isFileRequestList($data)) {
59
            return new FileRequestCollection($data);
60
        }
61
62
        //File Request
63 1
        if (static::isFileRequest($data)) {
64
            return new FileRequest($data);
65
        }
66
67
        //Deleted File/Folder
68 1
        if (static::isDeletedFileOrFolder($data)) {
69
            return new DeletedMetadata($data);
70
        }
71
72
        //Base Model
73 1
        return new BaseModel($data);
74
    }
75
76
    /**
77
     * @param array $data
78
     *
79
     * @return bool
80
     */
81 22
    protected static function isFileOrFolder(array $data)
82
    {
83 22
        return isset($data['.tag']);
84
    }
85
86
    /**
87
     * @param string $tag
88
     *
89
     * @return bool
90
     */
91 20
    protected static function isFile($tag)
92
    {
93 20
        return $tag === 'file';
94
    }
95
96
    /**
97
     * @param string $tag
98
     *
99
     * @return bool
100
     */
101 11
    protected static function isFolder($tag)
102
    {
103 11
        return $tag === 'folder';
104
    }
105
106
    /**
107
     * @param array $data
108
     *
109
     * @return bool
110
     */
111 9
    protected static function isTemporaryLink(array $data)
112
    {
113 9
        return isset($data['metadata']) && isset($data['link']);
114
    }
115
116
    /**
117
     * @param array $data
118
     *
119
     * @return bool
120
     */
121 8
    protected static function isList(array $data)
122
    {
123 8
        return isset($data['entries']);
124
    }
125
126
    /**
127
     * @param array $data
128
     *
129
     * @return bool
130
     */
131 4
    protected static function isSearchResult(array $data)
132
    {
133 4
        return isset($data['matches']);
134
    }
135
136
    /**
137
     * @param string $tag
0 ignored issues
show
Bug introduced by
There is no parameter named $tag. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
138
     *
139
     * @return bool
140
     */
141 1
    protected static function isFileRequestList(array $data)
142
    {
143 1
        return isset($data['file_requests']);
144
    }
145
146
    /**
147
     * @param array $data
148
     *
149
     * @return bool
150
     */
151 1
    protected static function isFileRequest(array $data)
152
    {
153 1
        return isset($data['id']) && isset($data['is_open']);
154
    }
155
156
    /**
157
     * @param string $tag
158
     *
159
     * @return bool
160
     */
161 2
    protected static function isDeletedFileOrFolder($tag)
162
    {
163 2
        return 'deleted' === $tag;
164
    }
165
}
166