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

FileSystem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeArrayToFile() 0 5 1
A readFileIntoArray() 0 3 1
A getRealPathToReadableAndWritableFile() 0 4 4
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