File   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
eloc 79
c 1
b 0
f 0
dl 0
loc 184
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A removeLine() 0 18 3
A __construct() 0 4 1
A replaceLine() 0 21 5
A overwriteFileContent() 0 12 4
A removeLines() 0 18 3
A issues() 0 3 1
A insertStringToFile() 0 18 3
A readLine() 0 5 1
A updateIssueCollection() 0 3 1
A removeNeedlessEmptyLines() 0 29 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer;
5
6
use Pluswerk\TypoScriptAutoFixer\Exception\FileNotWritableException;
7
use Pluswerk\TypoScriptAutoFixer\Exception\WriteFileFailedException;
8
use Pluswerk\TypoScriptAutoFixer\Issue\IssueCollection;
9
10
class File extends \SplFileInfo
11
{
12
    /**
13
     * @var IssueCollection
14
     */
15
    private $issues;
16
17
    public function __construct($file_name)
18
    {
19
        parent::__construct($file_name);
20
        $this->issues = new IssueCollection();
21
    }
22
23
    /**
24
     * @param int $line
25
     *
26
     * @return string
27
     */
28
    public function readLine(int $line): string
29
    {
30
        $fileObject = $this->openFile('r');
31
        $fileObject->seek($line - 1);
32
        return $fileObject->current();
33
    }
34
35
    public function removeLine(int $line): void
36
    {
37
        $fileObject = $this->openFile('r');
38
        $content = '';
39
        $fileObject->rewind();
40
41
        while (!$fileObject->eof()) {
42
            if ($fileObject->key() === ($line - 1)) {
43
                // do not assign! It is the content of the line to remove.
44
                $fileObject->current();
45
            } else {
46
                $content .= $fileObject->current();
47
            }
48
49
            $fileObject->next();
50
        }
51
52
        $this->overwriteFileContent($content);
53
    }
54
55
    /**
56
     * @param int[] $lines
57
     */
58
    public function removeLines(array $lines): void
59
    {
60
        $fileObject = $this->openFile('r');
61
        $content = '';
62
        $fileObject->rewind();
63
64
        while (!$fileObject->eof()) {
65
            if (in_array($fileObject->key() + 1, $lines, true)) {
66
                // do not assign! It is the content of the line to remove.
67
                $fileObject->current();
68
            } else {
69
                $content .= $fileObject->current();
70
            }
71
72
            $fileObject->next();
73
        }
74
75
        $this->overwriteFileContent($content);
76
    }
77
78
    /**
79
     * @param string $lineValue
80
     * @param int    $line
81
     *
82
     * @todo: Try to make this method a little bit nicer ;-).
83
     */
84
    public function replaceLine(string $lineValue, int $line): void
85
    {
86
        $fileObject = $this->openFile('r');
87
        $content = '';
88
        $fileObject->rewind();
89
90
        while (!$fileObject->eof()) {
91
            if ($fileObject->key() === ($line - 1)) {
92
                $content .= $lineValue;
93
                $currentLine = $fileObject->current();
94
                if (strpos($lineValue, PHP_EOL) === false && strpos($currentLine, PHP_EOL) !== false) {
95
                    $content .= PHP_EOL;
96
                }
97
            } else {
98
                $content .= $fileObject->current();
99
            }
100
101
            $fileObject->next();
102
        }
103
104
        $this->overwriteFileContent($content);
105
    }
106
107
    /**
108
     * @param int    $line
109
     * @param string $string
110
     */
111
    public function insertStringToFile(int $line, string $string): void
112
    {
113
        $fileObject = $this->openFile('r');
114
        $content = '';
115
        $fileObject->rewind();
116
117
        while (!$fileObject->eof()) {
118
            if ($fileObject->key() === ($line - 1)) {
119
                $content .= $string;
120
                $content .= $fileObject->current();
121
            } else {
122
                $content .= $fileObject->current();
123
            }
124
125
            $fileObject->next();
126
        }
127
128
        $this->overwriteFileContent($content);
129
    }
130
131
    public function removeNeedlessEmptyLines(): void
132
    {
133
        $fileObject = $this->openFile('r');
134
        $content = '';
135
        $fileObject->rewind();
136
137
        $lastLineWasEmpty = false;
138
139
        $firstLine = $fileObject->current();
140
        if ($firstLine === PHP_EOL || $firstLine === '') {
141
            $fileObject->next();
142
        }
143
144
        while (!$fileObject->eof()) {
145
            $current = $fileObject->current();
146
            if (trim($current) === '') {
147
                $lastLineWasEmpty = true;
148
                $fileObject->next();
149
                continue;
150
            }
151
            if ($lastLineWasEmpty) {
152
                $lastLineWasEmpty = false;
153
                $content .= PHP_EOL;
154
            }
155
            $content .= $current;
156
            $fileObject->next();
157
        }
158
159
        $this->overwriteFileContent($content);
160
    }
161
162
    /**
163
     * @param IssueCollection $issueCollection
164
     */
165
    public function updateIssueCollection(IssueCollection $issueCollection): void
166
    {
167
        $this->issues = $issueCollection;
168
    }
169
170
    /**
171
     * @param $content
172
     */
173
    private function overwriteFileContent($content): void
174
    {
175
        try {
176
            $writeFileObject = $this->openFile('w');
177
        } catch (\RuntimeException $e) {
178
            throw new FileNotWritableException($e->getMessage());
179
        }
180
181
        $writeResult = $writeFileObject->fwrite($content);
182
183
        if ($writeResult === false || $writeResult === null) {
184
            throw new WriteFileFailedException('write file ' . $this->getPathname() . ' failed!');
185
        }
186
    }
187
188
    /**
189
     * @return IssueCollection
190
     */
191
    public function issues(): IssueCollection
192
    {
193
        return $this->issues;
194
    }
195
}
196