File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Rougin\Combustor\Common;
4
5
/**
6
 * File
7
 *
8
 * A simple object-oriented interface for handling files.
9
 *
10
 * @package Combustor
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
class File
14
{
15
    /**
16
     * @var resource
17
     */
18
    protected $file;
19
20
    /**
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * @param string $path
27
     * @param string $mode
28
     */
29 48
    public function __construct($path, $mode = 'wb')
30
    {
31 48
        $this->path = $path;
32 48
        $this->file = fopen($path, $mode);
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($path, $mode) can also be of type false. However, the property $file 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...
33 48
    }
34
35
    /**
36
     * Closes an open file pointer.
37
     *
38
     * @return boolean
39
     */
40 45
    public function close()
41
    {
42 45
        return fclose($this->file);
43
    }
44
45
    /**
46
     * Reads entire file into a string.
47
     *
48
     * @return string
49
     */
50 3
    public function getContents()
51
    {
52 3
        return file_get_contents($this->path);
53
    }
54
55
    /**
56
     * Writes a string to a file.
57
     *
58
     * @param  string $content
59
     * @return integer|boolean
60
     */
61 45
    public function putContents($content)
62
    {
63 45
        return file_put_contents($this->path, $content);
64
    }
65
66
    /**
67
     * Changes the file mode of the file.
68
     *
69
     * @param  integer $mode
70
     * @return boolean
71
     */
72 30
    public function chmod($mode)
73
    {
74 30
        return chmod($this->path, $mode);
75
    }
76
}
77