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'); |
|
|
|
|
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); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function ended() |
88
|
|
|
{ |
89
|
|
|
$this->openFile(); |
90
|
|
|
|
91
|
|
|
return \feof($this->fileHandle); |
|
|
|
|
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
|
|
|
} |
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 theid
property of an instance of theAccount
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.