Completed
Push — master ( 06667f...42818b )
by Marcio
04:24
created

Uploads::file()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 6.4179
c 0
b 0
f 0
cc 11
eloc 36
nc 18
nop 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
3
/**
4
 *
5
 * APWEB Framework (http://framework.artphoweb.com/)
6
 * APWEB FW(tm) : Rapid Development Framework (http://framework.artphoweb.com/)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @link      http://github.com/zebedeu/artphoweb for the canonical source repository
13
 * @copyright (c) 2016.  APWEB  Software Technologies AO Inc. (http://www.artphoweb.com)
14
 * @license   http://framework.artphoweb.com/license/new-bsd New BSD License
15
 * @author    Marcio Zebedeu - [email protected]
16
 * @version   1.0.0
17
 */
18
19
20
class Uploads{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
21
22
   
23
    private $files;
0 ignored issues
show
Unused Code introduced by
The property $files is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
    public $path;
25
    private $tmp;
26
    public $name;
27
    public $size;
28
    public $type;
29
    private $dir;
0 ignored issues
show
Unused Code introduced by
The property $dir is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    public function file($dir = null) {
0 ignored issues
show
Coding Style introduced by
file uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
32
        if (!empty($dir)) {
33
            if (isset($_FILES['archive'])) {
34
                foreach ($_FILES['archive']['name'] as $i => $name) {
35
                    $this->name = $_FILES['archive']['name'][$i];
36
                    $this->size = $_FILES['archive']['size'][$i];
37
                    $this->type = $_FILES['archive']['type'][$i];
38
                    $this->tmp = $_FILES['archive']['tmp_name'][$i];
39
40
                    $explode = explode('.', $name);
41
                    $ext = end($explode);
42
                   
43
                    $this->path = DIR_FILE . 'Upload' . DS . 'Default' . DS . $dir . DS;
0 ignored issues
show
Bug introduced by
The constant DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DIR_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
                
45
                    $this->path .= basename($explode[0] . time() . '.' . $ext);
46
                    $explode = explode('.', $name);
0 ignored issues
show
Unused Code introduced by
The assignment to $explode is dead and can be removed.
Loading history...
47
48
                    // echo $ext . "<br/>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
49
50
51
52
                    if (empty($this->type)) {
53
                        return $errors[] = '<div class="btn btn-danger"> Por favor, escolhe 1 ficheiro para ser carregado</div>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$errors was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errors = array(); before regardless.
Loading history...
54
                    } else {
55
                        $allowed = array('jpg', 'JPG', 'jpeg', 'gif', 'btm', 'png', 'txt', 'docx', 'doc', 'pdf', 'mp3');
56
57
                        if (in_array($ext, $allowed) === false)
58
                            return $errors[] = '<div class="btn btn-danger">A extensao do ficheiro nao foi permitido </div>';
59
                    }
60
//                              100000000
61
                    $max_size = 100000000;
62
                    if ($this->size > $max_size) {
63
                        return $errors[] = '<div class="btn btn-danger"> O tamanho do ficheiro é muito grande</div>';
64
                    }
65
                }// end foreach
66
            } // end if
67
68
            if (empty($errors)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errors seems to never exist and therefore empty should always be true.
Loading history...
69
              
70
                if (!file_exists(DIR_FILE . 'Upload'  . DS . $dir . DS)) {
71
                    mkdir(DIR_FILE . 'Upload' .  DS . $dir . DS, 0777, true);
72
                  
73
                  }  // chmod('uploads/', 0755);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
74
                else if (!file_exists(DIR_FILE . 'Upload' . DS . 'Default' . DS . $dir . DS)) {
75
                    mkdir(DIR_FILE . 'Upload' . DS . 'Default' . DS . $dir . DS, 0777, true);
76
                  
77
                  }  // chmod('uploads/', 0755);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
78
                
79
                if (move_uploaded_file($this->tmp, $this->path )) {
80
                    $output = '<div class="btn btn-success">';
81
                    $output .= 'Ficheiro carregado  com sucesso';
82
                    $output .= '</div>';
83
                    return $output;
84
                } else {
85
                    $output = '<div class="btn btn-warning">';
86
                    $output .= 'Nenhum ficheiro foi carregado';
87
                    $output .= '</div>';
88
                    return $output;
89
                }
90
            }
91
        }
92
    }
93
94
95
    public function formUploadFiles($dir_rec = null, $type = "hidden", $name = null, $value = null) {
96
        $var = "";
97
        $var .= "<form method='POST' enctype='multipart/form-data' action='" . $dir_rec . "'>";
98
        $var .= "<input type='hidden' name=' ini_get('session.upload_progress.name'); ' value='123' />";
99
100
        $var .= "<input type='file' name='archive[]' multiple ><br/>";
101
        $var .= "<input type='$type' name='$name' value='$value'> <br/>";
102
        $var .= "<button type='submit'  class=' btn btn-lg btn-default glyphicon glyphicon-upload'></button></form>";
103
        return $var;
104
    }
105
106
}
107