File   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
dl 0
loc 135
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 10 4
A getFilePath() 0 3 1
A isRemoteFile() 0 3 1
A getMimetype() 0 3 2
A getContents() 0 3 1
A getSize() 0 3 1
A __construct() 0 6 1
A close() 0 4 2
A getFileName() 0 3 1
A __destruct() 0 3 1
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\FileUpload;
24
25
use Facebook\Exception\SDKException;
26
27
/**
28
 * @package Facebook
29
 */
30
class File
31
{
32
    /**
33
     * @var string the path to the file on the system
34
     */
35
    protected $path;
36
37
    /**
38
     * @var int The maximum bytes to read. Defaults to -1 (read all the remaining buffer).
39
     */
40
    private $maxLength;
41
42
    /**
43
     * @var int Seek to the specified offset before reading. If this number is negative, no seeking will occur and reading will start from the current position.
44
     */
45
    private $offset;
46
47
    /**
48
     * @var resource the stream pointing to the file
49
     */
50
    protected $stream;
51
52
    /**
53
     * Creates a new File entity.
54
     *
55
     * @param string $filePath
56
     * @param int    $maxLength
57
     * @param int    $offset
58
     *
59
     * @throws SDKException
60
     */
61 15
    public function __construct($filePath, $maxLength = -1, $offset = -1)
62
    {
63 15
        $this->path = $filePath;
64 15
        $this->maxLength = $maxLength;
65 15
        $this->offset = $offset;
66 15
        $this->open();
67 14
    }
68
69
    /**
70
     * Closes the stream when destructed.
71
     */
72 11
    public function __destruct()
73
    {
74 11
        $this->close();
75 11
    }
76
77
    /**
78
     * Opens a stream for the file.
79
     *
80
     * @throws SDKException
81
     */
82 15
    public function open()
83
    {
84 15
        if (!$this->isRemoteFile($this->path) && !is_readable($this->path)) {
85 1
            throw new SDKException('Failed to create File entity. Unable to read resource: ' . $this->path . '.');
86
        }
87
88 14
        $this->stream = fopen($this->path, 'r');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($this->path, 'r') can also be of type false. However, the property $stream is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
89
90 14
        if (!$this->stream) {
91
            throw new SDKException('Failed to create File entity. Unable to open resource: ' . $this->path . '.');
92
        }
93 14
    }
94
95
    /**
96
     * Stops the file stream.
97
     */
98 11
    public function close()
99
    {
100 11
        if (is_resource($this->stream)) {
101 11
            fclose($this->stream);
102
        }
103 11
    }
104
105
    /**
106
     * Return the contents of the file.
107
     *
108
     * @return string
109
     */
110 9
    public function getContents()
111
    {
112 9
        return stream_get_contents($this->stream, $this->maxLength, $this->offset);
113
    }
114
115
    /**
116
     * Return the name of the file.
117
     *
118
     * @return string
119
     */
120 7
    public function getFileName()
121
    {
122 7
        return basename($this->path);
123
    }
124
125
    /**
126
     * Return the path of the file.
127
     *
128
     * @return string
129
     */
130 4
    public function getFilePath()
131
    {
132 4
        return $this->path;
133
    }
134
135
    /**
136
     * Return the size of the file.
137
     *
138
     * @return int
139
     */
140 4
    public function getSize()
141
    {
142 4
        return filesize($this->path);
143
    }
144
145
    /**
146
     * Return the mimetype of the file.
147
     *
148
     * @return string
149
     */
150 7
    public function getMimetype()
151
    {
152 7
        return Mimetypes::getInstance()->fromFilename($this->path) ?: 'text/plain';
153
    }
154
155
    /**
156
     * Returns true if the path to the file is remote.
157
     *
158
     * @param string $pathToFile
159
     *
160
     * @return bool
161
     */
162 15
    protected function isRemoteFile($pathToFile)
163
    {
164 15
        return preg_match('/^(https?|ftp):\/\/.*/', $pathToFile) === 1;
165
    }
166
}
167