FileSystem::getNextLine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sepia\PoParser\SourceHandler;
4
5
/**
6
 *    Copyright (c) 2012 Raúl Ferràs [email protected]
7
 *    All rights reserved.
8
 *
9
 *    Redistribution and use in source and binary forms, with or without
10
 *    modification, are permitted provided that the following conditions
11
 *    are met:
12
 *    1. Redistributions of source code must retain the above copyright
13
 *       notice, this list of conditions and the following disclaimer.
14
 *    2. Redistributions in binary form must reproduce the above copyright
15
 *       notice, this list of conditions and the following disclaimer in the
16
 *       documentation and/or other materials provided with the distribution.
17
 *    3. Neither the name of copyright holders nor the names of its
18
 *       contributors may be used to endorse or promote products derived
19
 *       from this software without specific prior written permission.
20
 *
21
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 *    ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
 *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 *    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
25
 *    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
 *    POSSIBILITY OF SUCH DAMAGE.
32
 *
33
 * https://github.com/raulferras/PHP-po-parser
34
 */
35
class FileSystem implements SourceHandler
36
{
37
    /** @var resource */
38
    protected $fileHandle;
39
40
    /** @var string */
41
    protected $filePath;
42
43
    public function __construct($filePath)
44
    {
45
        $this->filePath = $filePath;
46
        $this->fileHandle = null;
47
    }
48
49
    /**
50
     * @throws \Exception
51
     */
52
    protected function openFile()
53
    {
54
        if ($this->fileHandle !== null) {
55
            return;
56
        }
57
58
        if (\file_exists($this->filePath) === false) {
59
            throw new \Exception('Parser: Input File does not exists: "' . \htmlspecialchars($this->filePath) . '"');
60
        }
61
62
        if (\is_readable($this->filePath) === false) {
63
            throw new \Exception('Parser: File is not readable: "' . \htmlspecialchars($this->filePath) . '"');
64
        }
65
66
        $this->fileHandle = @\fopen($this->filePath, 'rb');
0 ignored issues
show
Documentation Bug introduced by
It seems like @fopen($this->filePath, 'rb') can also be of type false. However, the property $fileHandle 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...
67
        if ($this->fileHandle===false) {
68
            throw new \Exception('Parser: Could not open file: "' . \htmlspecialchars($this->filePath) . '"');
69
        }
70
    }
71
72
    /**
73
     * @return bool|string
74
     * @throws \Exception
75
     */
76
    public function getNextLine()
77
    {
78
        $this->openFile();
79
80
        return \fgets($this->fileHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->fileHandle can also be of type false; however, parameter $handle of fgets() 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

80
        return \fgets(/** @scrutinizer ignore-type */ $this->fileHandle);
Loading history...
81
    }
82
83
    /**
84
     * @return bool
85
     * @throws \Exception
86
     */
87
    public function ended()
88
    {
89
        $this->openFile();
90
91
        return \feof($this->fileHandle);
0 ignored issues
show
Bug introduced by
It seems like $this->fileHandle can also be of type false; however, parameter $handle of feof() 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

91
        return \feof(/** @scrutinizer ignore-type */ $this->fileHandle);
Loading history...
92
    }
93
94
    public function close()
95
    {
96
        if ($this->fileHandle === null) {
97
            return true;
98
        }
99
100
        return @\fclose($this->fileHandle);
101
    }
102
103
    /**
104
     * @param $output
105
     * @param $filePath
106
     *
107
     * @return bool
108
     * @throws \Exception
109
     */
110
    public function save($output)
111
    {
112
        $result = \file_put_contents($this->filePath, $output);
113
        if ($result === false) {
114
            throw new \Exception('Could not write into file '.\htmlspecialchars($this->filePath));
115
        }
116
117
        return true;
118
    }
119
}