GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Nip_File::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Nip_File_Exception extends Exception
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...
4
{
5
}
6
7
class Nip_File extends Nip_Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
8
{
9
10
    protected $_path;
11
    protected $_name;
12
    protected $_extension;
13
14
    public function __construct($path = false)
15
    {
16
        if ($path) {
17
            $this->setPath($path);
18
        }
19
    }
20
21
    /**
22
     * @param $target
23
     * @return $this
24
     * @throws Nip_File_Exception
25
     */
26 View Code Duplication
    public function move($target)
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...
27
    {
28
        $dir = dirname($target);
29
        if (!is_dir($target)) {
30
            mkdir($dir, 0755, true);
31
        }
32
33
        if (rename($this->getPath(), $target)) {
34
            $this->setPath($target);
35
        } else {
36
            throw new Nip_File_Exception("Cannot move $this->_path file to $target");
37
        }
38
        return $this;
39
    }
40
41
    public function getPath()
42
    {
43
        return $this->_path;
44
    }
45
46
    public function setPath($path)
47
    {
48
        $this->_name = basename($path);
49
        $this->_extension = pathinfo($path, PATHINFO_EXTENSION);
50
        $this->_path = $path;
51
        return $this;
52
    }
53
54
    /**
55
     * @param $target
56
     * @return $this
57
     * @throws Nip_File_Exception
58
     */
59 View Code Duplication
    public function copy($target)
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...
60
    {
61
        $dir = dirname($target);
62
        if (!is_dir($target)) {
63
            mkdir($dir, 0755, true);
64
        }
65
66
        if (copy($this->getPath(), $target)) {
67
            $this->setPath($target);
68
        } else {
69
            throw new Nip_File_Exception("Cannot copy $this->_path file to $target");
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $target
76
     * @return Nip_Process
77
     */
78 View Code Duplication
    public function unzip($target)
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...
79
    {
80
        if (!is_dir($target)) {
81
            mkdir($target, 0755, true);
82
        }
83
84
        $process = new Nip_Process("unzip {$this->_path}  -d $target");
85
        $process->run();
86
87
        return $process;
88
    }
89
90
    public function download($filename = false, $contentType = false)
91
    {
92
        if (!$filename) {
93
            $filename = $this->getName();
94
        }
95
        if (!$contentType) {
96
            $contentType = "application/force-download";
97
        }
98
99
        header("Pragma: public");
100
        header("Expires: 0");
101
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102
        header("Cache-Control: private", false);
103
        header("Content-Type: $contentType");
104
        header("Content-Disposition: attachment; filename=\"$filename\"");
105
        header("Content-Length: {$this->getSize()}");
106
        header("Content-Transfer-Encoding: binary");
107
108
        readfile($this->getPath());
109
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method download() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
110
    }
111
112
    public function getName()
113
    {
114
        return $this->_name;
115
    }
116
117
    public function getSize()
118
    {
119
        return filesize($this->getPath());
120
    }
121
122
    public function delete()
123
    {
124
        unlink($this->getPath());
125
    }
126
127
    public function getExtension()
128
    {
129
        return $this->_extension;
130
    }
131
132
    public function getTime()
133
    {
134
        return filemtime($this->getPath());
135
    }
136
137
    public function getMimeType()
138
    {
139
        if (function_exists('mime_content_type')) {
140
            return mime_content_type($this->getPath());
141
142
        } elseif (function_exists('finfo_open')) {
143
            $finfo = finfo_open(FILEINFO_MIME);
144
            $mimetype = finfo_file($finfo, $this->getPath());
145
            finfo_close($finfo);
146
            return $mimetype;
147
        }
148
149
        return "unknown";
150
    }
151
152
}