SimpleWalker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 0
cbo 1
dl 0
loc 58
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A goToLine() 0 15 4
A countLines() 0 15 1
A goBeforeCharacter() 0 14 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