Passed
Push — master ( 35235e...b730fe )
by Malte
03:40
created

FileSystem::writeArrayToFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Helper;
4
5
/**
6
 * Helper for common file system functions.
7
 */
8
final class FileSystem
9
{
10
    /**
11
     * @param string $userProvidedPath
12
     * @return string|null
13
     */
14
    public static function getRealPathToReadableAndWritableFile($userProvidedPath)
15
    {
16
        $realPath = realpath(dirname($userProvidedPath)) . '/' . basename($userProvidedPath);
17
        return is_file($realPath) && is_readable($realPath) && is_writable($realPath) ? $realPath : null;
18
    }
19
20
    /**
21
     * @param string $path
22
     * @return string[]|bool
23
     */
24
    public static function readFileIntoArray($path)
25
    {
26
        return file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
27
    }
28
29
    /**
30
     * @param string[] $lines
31
     * @param string $path
32
     */
33
    public static function writeArrayToFile(array $lines, $path)
34
    {
35
        $handle = fopen($path, 'wb');
36
        fwrite($handle, implode(PHP_EOL, $lines));
37
        fclose($handle);
38
    }
39
}
40