1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of YaEtl. |
||
5 | * (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
||
6 | * This source file is licensed under the MIT license which you will |
||
7 | * find in the LICENSE file or at https://opensource.org/licenses/MIT |
||
8 | */ |
||
9 | |||
10 | namespace fab2s\YaEtl\Traits; |
||
11 | |||
12 | use fab2s\NodalFlow\YaEtlException; |
||
13 | |||
14 | /** |
||
15 | * Trait FileHandlerTrait |
||
16 | */ |
||
17 | trait FileHandlerTrait |
||
18 | { |
||
19 | /** |
||
20 | * @var string UTF8 | UTF16_BE | UTF16_LE | UTF32_BE | UTF32_LE |
||
21 | */ |
||
22 | protected $bomRegEx = '\xEF\xBB\xBF|\xFE\xFF|\xFF\xFE|\x00\x00\xFE\xFF|\xFF\xFE\x00\x00'; |
||
23 | |||
24 | /** |
||
25 | * @var resource |
||
26 | */ |
||
27 | protected $handle; |
||
28 | |||
29 | /** |
||
30 | * make sure we do not hold un-necessary handles |
||
31 | */ |
||
32 | public function __destruct() |
||
33 | { |
||
34 | $this->releaseHandle(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param $string |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public function trimBom($string) |
||
43 | { |
||
44 | return preg_replace('`^' . $this->bomRegEx . '`', '', $string); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * release handle |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function releaseHandle() |
||
53 | { |
||
54 | if (is_resource($this->handle)) { |
||
55 | fclose($this->handle); |
||
56 | } |
||
57 | |||
58 | $this->handle = null; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param resource|string $input |
||
65 | * @param string $mode |
||
66 | * |
||
67 | * @throws YaEtlException |
||
68 | * |
||
69 | * @return $this |
||
70 | */ |
||
71 | protected function initHandle($input, $mode) |
||
72 | { |
||
73 | if (is_resource($input)) { |
||
74 | $this->handle = $input; |
||
75 | } elseif (is_file($input)) { |
||
76 | $this->handle = fopen($input, $mode); |
||
0 ignored issues
–
show
|
|||
77 | if (!$this->handle) { |
||
78 | throw new YaEtlException('Handle could not be opened in mode:' . $mode); |
||
79 | } |
||
80 | } else { |
||
81 | throw new YaEtlException('$input is either not a resource or not a file'); |
||
82 | } |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | } |
||
87 |
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.