getList()   F
last analyzed

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 141
Code Lines 89

Duplication

Lines 47
Ratio 33.33 %

Code Coverage

Tests 0
CRAP Score 930

Importance

Changes 0
Metric Value
cc 30
eloc 89
nc 315000
nop 1
dl 47
loc 141
rs 2
c 0
b 0
f 0
ccs 0
cts 106
cp 0
crap 930

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
 * @package Intraface_FileManager
4
 */
5
require_once 'Intraface/modules/filemanager/FileHandler.php';
6
7
class Intraface_modules_filemanager_FileManager extends FileHandler
8
{
9
    /**
10
     * @var object
11
     */
12
    public $keywords;
13
14
    /**
15
     * @var object
16
     */
17
    public $dbquery;
18
19
    /**
20
     * Constructor
21
     *
22
     * @param object  $kernel  Kernel object
23
     * @param integer $file_id Specific file id
24
     *
25
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
     */
27 6
    function __construct($kernel, $file_id = 0)
28
    {
29 6
        parent::__construct($kernel, $file_id);
30 6
    }
31
32
    /**
33
     * Creates the dbquery object so it can be used in the class
34
     *
35
     * @return void
36
     */
37 1
    public function getDBQuery()
38
    {
39 1
        if ($this->dbquery) {
40
            return $this->dbquery;
41
        }
42 1
        $this->dbquery = new Ilib_DBQuery("file_handler", "file_handler.temporary = 0 AND file_handler.active = 1 AND file_handler.intranet_id = ".$this->kernel->intranet->get("id"));
43 1
        $this->dbquery->createStore($this->kernel->getSessionId(), 'intranet_id = '.intval($this->kernel->intranet->get('id')));
44 1
        $this->dbquery->useErrorObject($this->error);
45
46 1
        return $this->dbquery;
47
    }
48
49
50
    /**
51
     * Creates the keywords object
52
     *
53
     * @return object
54
     */
55
    public function getKeywords()
56
    {
57
        return ($this->keywords = new Keyword($this));
58
    }
59
60 4
    public function getKeywordAppender()
61
    {
62 4
        return new Intraface_Keyword_Appender($this);
63
    }
64
65
66
    /**
67
     * Gets a list
68
     *
69
     * @param string $debug Can be nothing or debug
70
     *
71
     * @return array
72
     */
73
    public function getList($debug = '')
74
    {
75
        // we load the mime types as they are going to be used a couple of times
76
        $this->loadMimeTypes();
77
78
        $this->dbquery = $this->getDBQuery();
79
80 View Code Duplication
        if ($this->dbquery->checkFilter("uploaded_from_date")) {
81
            $date_parts = explode(" ", $this->dbquery->getFilter("uploaded_from_date"));
82
            // Der kontrolleres ikke for gyldig tidsformat
83
            if (isset($date_parts[1]) && $date_parts[1] != "") {
84
                $time = " ".$date_parts[1];
85
            }
86
            $date = new Intraface_Date($date_parts[0]);
87
            if ($date->convert2db()) {
88
                $this->dbquery->setCondition("file_handler.date_created >= \"".$date->get().$time."\"");
0 ignored issues
show
Bug introduced by
The variable $time does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
            } else {
90
                $this->error->set("error in uploaded from date");
91
            }
92
        }
93
94 View Code Duplication
        if ($this->dbquery->checkFilter("uploaded_to_date")) {
95
            $date_parts = explode(" ", $this->dbquery->getFilter("uploaded_to_date"));
96
            // Der kontrolleres ikke for gyldig tidsformat
97
            if (isset($date_parts[1]) && $date_parts[1] != "") {
98
                $time = " ".$date_parts[1];
99
            }
100
            $date = new Intraface_Date($date_parts[0]);
101
            if ($date->convert2db()) {
102
                $this->dbquery->setCondition("file_handler.date_created <= \"".$date->get().$time."\"");
103
            } else {
104
                $this->error->set("error in uploaded to date");
105
            }
106
        }
107
108 View Code Duplication
        if ($this->dbquery->checkFilter("edited_from_date")) {
109
            $date_parts = explode(" ", $this->dbquery->getFilter("edited_from_date"));
110
            // Der kontrolleres ikke for gyldig tidsformat
111
            if (isset($date_parts[1]) && $date_parts[1] != "") {
112
                $time = " ".$date_parts[1];
113
            }
114
            $date = new Intraface_Date($date_parts[0]);
115
            if ($date->convert2db()) {
116
                $this->dbquery->setCondition("file_handler.date_changed >= \"".$date->get().$time."\"");
117
            } else {
118
                $this->error->set("error in edited from date");
119
            }
120
        }
121
122 View Code Duplication
        if ($this->dbquery->checkFilter("edited_to_date")) {
123
            $date_parts = explode(" ", $this->dbquery->getFilter("edited_to_date"));
124
            // Der kontrolleres ikke for gyldig tidsformat
125
            if (isset($date_parts[1]) && $date_parts[1] != "") {
126
                $time = " ".$date_parts[1];
127
            }
128
            $date = new Intraface_Date($date_parts[0]);
129
            if ($date->convert2db()) {
130
                $this->dbquery->setCondition("file_handler.date_changed <= \"".$date->get().$time."\"");
131
            } else {
132
                $this->error->set("error in edited to date");
133
            }
134
        }
135
136
        if ($this->dbquery->checkFilter("accessibility")) {
137
            $accessibility_key = array_search($this->dbquery->getFilter("accessibility"), $this->accessibility_types);
138
            if ($accessibility_key !== false) {
139
                $this->dbquery->setCondition("file_handler.accessibility_key = ".intval($accessibility_key)."");
140
            }
141
        }
142
143 View Code Duplication
        if ($this->dbquery->checkFilter("text")) {
144
            $this->dbquery->setCondition("file_handler.file_name LIKE \"%".safeToDb($this->dbquery->getFilter("text"))."%\" OR file_handler.description LIKE \"%".safeToDb($this->dbquery->getFilter("text"))."%\"");
145
        }
146
147
        if ($this->dbquery->checkFilter('images')) {
148
            $keys = array();
149
            foreach ($this->file_types as $key => $mime_type) {
150
                if ($mime_type['image'] == 1) {
151
                    $keys[] = $key;
152
                }
153
            }
154
155
            if (count($keys) > 0) {
156
                $this->dbquery->setCondition("file_handler.file_type_key IN (".implode(',', $keys).")");
157
            }
158
        }
159
160
161
        if (!$this->dbquery->checkSorting()) {
162
            $this->dbquery->setSorting('file_handler.file_name');
163
        }
164
165
        $file = array();
166
        $i = 0;
167
168
169
        if ($debug == 'debug') {
170
            $debug = true;
171
        } else {
172
            $debug = false;
173
        }
174
175
        $db = $this->dbquery->getRecordset("file_handler.*, DATE_FORMAT(file_handler.date_created, '%d-%m-%Y') AS dk_date_created", "", $debug);
176
177
        //$db->query("SELECT * FROM file_handler WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND active = 1 AND tmp = 0 ORDER BY date_created DESC");
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
178
        while ($db->nextRecord()) {
179
            $file[$i]['id'] = $db->f('id');
180
            $file[$i]['date_created'] = $db->f('date_created');
181
            $file[$i]['dk_date_created'] = $db->f('dk_date_created');
182
            $file[$i]['description'] = $db->f('description');
183
            //$file[$i]['date_updated'] = $db->f('date_updated');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
184
            $file[$i]['file_name'] = $db->f('file_name');
185
            $file[$i]['server_file_name'] = $db->f('server_file_name');
186
            $file[$i]['file_size'] = $db->f('file_size');
187
            $file[$i]['file_type'] = $this->_getMimeType((int)$db->f('file_type_key'));
188
            $file[$i]['is_picture'] = $this->file_types[$db->f('file_type_key')]['image'];
189
            if ($file[$i]['file_size'] >= 1000000) {
190
                $file[$i]['dk_file_size'] = number_format(($file[$i]['file_size']/1000000), 2, ",", ".")." Mb";
191
            } elseif ($file[$i]['file_size'] >= 1000) {
192
                $file[$i]['dk_file_size'] = number_format(($file[$i]['file_size']/1000), 2, ",", ".")." Kb";
193
            } else {
194
                $file[$i]['dk_file_size'] = number_format($file[$i]['file_size'], 2, ",", ".")." byte";
195
            }
196
            $file[$i]['file_uri'] = FILE_VIEWER.'?/'.$this->kernel->intranet->get('public_key').'/'.$db->f('access_key').'/'.urlencode($db->f('file_name'));
197
198
            $file[$i]['accessibility'] = $this->accessibility_types[$db->f('accessibility_key')];
199
200
201
            if ($file[$i]['is_picture'] == 1) {
202
                $file[$i]['icon_uri'] = FILE_VIEWER.'?/'.$this->kernel->intranet->get('public_key').'/'.$db->f('access_key').'/system-square/'.urlencode($db->f('file_name'));
203
                $file[$i]['icon_width'] = 75;
204
                $file[$i]['icon_height'] = 75;
205
            } else {
206
                $file[$i]['icon_uri'] = '/images/mimetypes/'.$file[$i]['file_type']['icon'];
207
                $file[$i]['icon_width'] = 75;
208
                $file[$i]['icon_height'] = 75;
209
            }
210
            $i++;
211
        }
212
        return $file;
213
    }
214
215
    /**
216
     * Find out whether any fils is uploaded
217
     *
218
     * @return integer
219
     */
220
    public function isFilledIn()
221
    {
222
        $db = new DB_Sql;
223
        $db->query("SELECT id FROM file_handler WHERE file_handler.temporary = 0 AND file_handler.active = 1 AND file_handler.intranet_id = ".$this->kernel->intranet->get("id"));
224
        return $db->numRows();
225
    }
226
}
227