Completed
Pull Request — master (#439)
by Michael
14:10 queued 07:19
created

FileHandler::eof()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Service;
21
22
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
23
24
/**
25
 * Class FileHandler
26
 */
27
class FileHandler
28
{
29
    /**
30
     * @var resource
31
     */
32
    private $resource;
33
34
    /**
35
     * FileHandler destructor.
36
     */
37 9
    public function __destruct()
38
    {
39 9
        $this->close();
40 9
    }
41
42
    /**
43
     * @param string $filename
44
     * @param string $mode
45
     *
46
     * @return $this
47
     */
48 8
    public function open(string $filename, string $mode): self
49
    {
50 8
        $fileHandler = new self();
51 8
        $fileHandler->initialize($filename, $mode);
52
53 7
        return $fileHandler;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59 9
    public function close(): bool
60
    {
61 9
        if (\is_resource($this->resource)) {
62 7
            return fclose($this->resource);
63
        }
64
65 9
        return false;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 2
    public function eof(): bool
72
    {
73 2
        return feof($this->resource);
74
    }
75
76
    /**
77
     * @param int $length
78
     *
79
     * @return string
80
     */
81 5
    public function read(int $length): string
82
    {
83 5
        return fread($this->resource, $length);
84
    }
85
86
    /**
87
     * @param int $offset
88
     * @param int $whence
89
     *
90
     * @return int
91
     */
92 1
    public function seek(int $offset, int $whence = SEEK_SET): int
93
    {
94 1
        return fseek($this->resource, $offset, $whence);
95
    }
96
97
    /**
98
     * @param string $string
99
     * @param int    $length
100
     *
101
     * @return int
102
     */
103 1
    public function write(string $string, int $length = null): int
104
    {
105 1
        if (null === $length) {
106 1
            $length = mb_strlen($string);
107
        }
108
109 1
        return fwrite($this->resource, $string, $length);
110
    }
111
112
    /**
113
     * @param string $filename
114
     * @param string $mode
115
     *
116
     * @return void
117
     *
118
     * @throws FileNotFoundException
119
     */
120 8
    private function initialize(string $filename, string $mode): void
121
    {
122 8
        if (!file_exists($filename)) {
123 1
            throw new FileNotFoundException(sprintf('file %s does not exist', $filename));
124
        }
125 7
        $this->resource = fopen($filename, $mode);
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($filename, $mode) can also be of type false. However, the property $resource 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...
126 7
    }
127
}
128