|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Common; |
|
19
|
|
|
|
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
class FileIO |
|
23
|
|
|
{ |
|
24
|
|
|
public static function readFile($filePath) |
|
25
|
|
|
{ |
|
26
|
|
|
if (false === $fileData = @file_get_contents($filePath)) { |
|
27
|
|
|
throw new RuntimeException(sprintf('unable to read file "%s"', $filePath)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $filePath; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function readJsonFile($filePath) |
|
34
|
|
|
{ |
|
35
|
|
|
$fileData = self::readFile($filePath); |
|
36
|
|
|
$jsonData = json_decode($fileData, true); |
|
|
|
|
|
|
37
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
38
|
|
|
throw new RuntimeException(sprintf('unable to decode JSON from file "%s"', $filePath)); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function writeFile($filePath, $fileData) |
|
43
|
|
|
{ |
|
44
|
|
|
if (false === @file_put_contents($filePath, $fileData)) { |
|
45
|
|
|
throw new RuntimeException(sprintf('unable to write file "%s"', $filePath)); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function writeJsonFile($filePath, $fileJsonData) |
|
50
|
|
|
{ |
|
51
|
|
|
$fileData = json_encode($fileJsonData); |
|
|
|
|
|
|
52
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
53
|
|
|
throw new RuntimeException(sprintf('unable to encode JSON for file "%s"', $filePath)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
self::writeFile($filePath, json_encode($fileJsonData)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.