Completed
Push — master ( 28b764...a7b092 )
by Michaël
02:42
created

SimpleWalker::goBeforeCharacter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 2
crap 4
1
<?php
2
3
namespace TextFile\Walker;
4
5
use TextFile\Exception\OutOfBoundsException;
6
7
/**
8
 * Class SimpleWalker
9
 *
10
 * @package TextFile\Walker
11
 */
12
class SimpleWalker implements WalkerInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 3
    public function goToLine(\SplFileObject $file, $lineNumber)
18
    {
19 3
        if ($lineNumber < 0 || $lineNumber > $this->countLines($file)) {
20 2
            throw new OutOfBoundsException();
21
        }
22
23 1
        $file->rewind();
24
25 1
        for ($i = 0; $i < $lineNumber; $i++) {
26 1
            $file->next();
27 1
            $file->current();
28 1
        }
29
30 1
        return $file;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 3
    public function goBeforeCharacter(\SplFileObject $file, $characterNumber)
37
    {
38 3
        $file->rewind();
39
40 3
        if ($characterNumber < 0 || $characterNumber > $file->getSize()) {
41 2
            throw new OutOfBoundsException();
42
        }
43
44 1
        for ($i = 0; $i <= $characterNumber - 1; $i++) {
45 1
            $file->fgetc();
46 1
        }
47
48 1
        return $file;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function countLines(\SplFileObject $file)
55
    {
56
        // Refresh file size
57 1
        clearstatcache($file->getFilename());
58
59 1
        $previous = $file->key();
60
61 1
        $file->seek($file->getSize());
62
63 1
        $count = $file->key();
64
65 1
        $file->seek($previous);
66
67 1
        return $count;
68
    }
69
}
70