Passed
Push — master ( 1b90b9...7956e0 )
by Gabriel
03:50 queued 11s
created

Nip_File_Handler::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Nip Framework.
5
 *
6
 * @category   Nip
7
 *
8
 * @copyright  2009 Nip Framework
9
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
10
 *
11
 * @version    SVN: $Id: Handler.php 70 2009-04-29 11:47:12Z victor.stanciu $
12
 */
13
class Nip_File_Handler
14
{
15
    const MODE_READ = 'r';
16
    const MODE_WRITE = 'w';
17
    const MODE_APPEND = 'a';
18
19
    /**
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * @var null|string
26
     */
27
    public $path = null;
28
29
    /**
30
     * @var null|string
31
     */
32
    public $url = null;
33
34
    public $data;
35
    public $extension;
36
    public $permissions = 0777;
37
    protected $handle = null;
38
39
    /**
40
     * Nip_File_Handler constructor.
41
     *
42
     * @param bool $data
43
     */
44
    public function __construct($data = false)
45
    {
46
        if ($data) {
47
            foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type true is not traversable.
Loading history...
48
                $this->{$key} = $value;
49
            }
50
        }
51
    }
52
53
    /**
54
     * @param $name
55
     * @param $arguments
56
     *
57
     * @return $this
58
     */
59
    public function __call($name, $arguments)
60
    {
61
        if (substr($name, 0, 3) == 'set') {
62
            $name = str_replace('set', '', $name);
63
            $name[0] = strtolower($name[0]);
64
65
            $this->$name = $arguments[0];
66
67
            return $this;
68
        } else {
69
            trigger_error("Method [$name] not defined", E_USER_ERROR);
70
        }
71
    }
72
73
    /**
74
     * @param $upload
75
     */
76
    public function upload($upload)
77
    {
78
        move_uploaded_file($upload['tmp_name'], $this->path);
79
    }
80
81
    /**
82
     * @return $this
83
     */
84
    public function delete()
85
    {
86
        return Nip_File_System::instance()->deleteFile($this->path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Nip_File_System::...deleteFile($this->path) returns the type Nip_File_System which is incompatible with the documented return type Nip_File_Handler.
Loading history...
87
    }
88
89
    /**
90
     * @return $this|false
91
     */
92
    public function gzip()
93
    {
94
        if (function_exists('gzencode')) {
95
            $this->data = gzencode($this->data, 9, FORCE_GZIP);
96
        } else {
97
            return false;
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param bool   $data
105
     * @param string $mode
106
     */
107
    public function write($data = false, $mode = self::MODE_APPEND)
108
    {
109
        if ($data) {
110
            $this->setData($data);
0 ignored issues
show
Bug introduced by
The method setData() does not exist on Nip_File_Handler. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            $this->/** @scrutinizer ignore-call */ 
111
                   setData($data);
Loading history...
111
        }
112
113
        $this->open($mode);
114
        fwrite($this->handle, $this->data);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        fwrite(/** @scrutinizer ignore-type */ $this->handle, $this->data);
Loading history...
115
        $this->close($mode);
0 ignored issues
show
Unused Code introduced by
The call to Nip_File_Handler::close() has too many arguments starting with $mode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $this->/** @scrutinizer ignore-call */ 
116
               close($mode);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
116
117
        chmod($this->path, $this->permissions);
118
    }
119
120
    /**
121
     * @param bool $data
122
     */
123
    public function rewrite($data = false)
124
    {
125
        return $this->write($data, self::MODE_WRITE);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->write($data, self::MODE_WRITE) targeting Nip_File_Handler::write() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
126
    }
127
128
    /**
129
     * @param string $mode
130
     *
131
     * @return $this
132
     */
133
    public function open($mode = self::MODE_READ)
134
    {
135
        $this->handle = fopen($this->path, $mode);
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return $this
142
     */
143
    public function close()
144
    {
145
        if ($this->handle) {
146
            fclose($this->handle);
147
            $this->handle = null;
148
        } else {
149
            trigger_error('Attempting to close an unopened file', E_USER_WARNING);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return null|string
157
     */
158
    public function getUrl()
159
    {
160
        if ($this->url === null) {
161
            $this->initUrl();
162
        }
163
164
        return $this->url;
165
    }
166
167
    public function initUrl()
168
    {
169
        $this->url = '';
170
    }
171
172
    /**
173
     * @return null|string
174
     */
175
    public function getPath()
176
    {
177
        if ($this->path === null) {
178
            $this->initPath();
179
        }
180
181
        return $this->path;
182
    }
183
184
    public function initPath()
185
    {
186
        $this->path = '';
187
    }
188
}
189