EnvFile::isNotEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace sixlive\DotenvEditor;
4
5
use sixlive\DotenvEditor\Support\Arr;
6
use SplFileObject;
7
8
class EnvFile
9
{
10
    /**
11
     * @var \SplFileObject|null
12
     */
13
    protected $file;
14
15
    /**
16
     * @param  string  $path
17
     */
18 12
    public function __construct($path)
19
    {
20 12
        $this->file = new SplFileObject($path, 'r+');
21 12
    }
22
23
    /**
24
     * Write content to the file.
25
     *
26
     * @param  string  $content
27
     *
28
     * @return bool
29
     */
30 8
    public function write($content)
31
    {
32 8
        $this->file->rewind();
0 ignored issues
show
Bug introduced by
The method rewind() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $this->file->/** @scrutinizer ignore-call */ 
33
                     rewind();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33 8
        $this->file->ftruncate(0);
34
35 8
        return $this->file->fwrite($content);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->file->fwrite($content) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
36
    }
37
38
    /**
39
     * Check if the file is NOT empty.
40
     *
41
     * @return bool
42
     */
43 12
    public function isNotEmpty()
44
    {
45 12
        return $this->file->getSize() > 0;
46
    }
47
48
    /**
49
     * Convert the file values into an associative array.
50
     *
51
     * @return array
52
     */
53 6
    public function toArray()
54
    {
55 6
        return Arr::flatten($this->mapLineValuesAndKeys());
56
    }
57
58
    /**
59
     * Close the file buffer.
60
     *
61
     * @return void
62
     */
63 12
    public function close()
64
    {
65 12
        $this->file = null;
66 12
    }
67
68
    /**
69
     * Extract lines from the file.
70
     *
71
     * @return array
72
     */
73 6
    private function linesFromFile()
74
    {
75 6
        return explode(
76 6
            "\n",
77 6
            $this->file->fread($this->file->getSize())
78
        );
79
    }
80
81
    /**
82
     * Convert config line into key value pairs.
83
     *
84
     * @return array
85
     */
86 6
    private function convertLineValuesToArray()
87
    {
88
        return array_map(function ($line) {
89 6
            return preg_split('/=/', $line, 2);
90 6
        }, $this->linesFromFile());
91
    }
92
93
    /**
94
     * Map config line extraction into key value pairs.
95
     *
96
     * @return array
97
     */
98 6
    private function mapLineValuesAndKeys()
99
    {
100
        return array_map(function ($line) {
101 6
            if (count($line) === 2) {
102 6
                return [$line[0] => $line[1]];
103
            }
104
105 6
            return $line[0];
106 6
        }, $this->convertLineValuesToArray());
107
    }
108
}
109