SimpleReader::getLineContent()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 3
1
<?php
2
3
namespace TextFile\Reader;
4
5
use TextFile\Exception\OutOfBoundsException;
6
use TextFile\Walker\WalkerInterface;
7
8
/**
9
 * Class SimpleReader
10
 *
11
 * @package TextFile\Reader
12
 */
13
class SimpleReader implements ReaderInterface
14
{
15
    /**
16
     * @var WalkerInterface
17
     */
18
    protected $walker;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function __construct(WalkerInterface $walker)
24
    {
25
        $this->walker = $walker;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 3
    public function getLinesRange(\SplFileObject $file, $from, $to)
32
    {
33 3
        if ($from < 0 || $to > $this->walker->countLines($file)) {
34 2
            throw new OutOfBoundsException();
35
        }
36
37 1
        return new \LimitIterator($file, $from, $to);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3 View Code Duplication
    public function getNextLineContent(\SplFileObject $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 3
        if ($file->key() + 1 > $this->walker->countLines($file)) {
46 1
            throw new OutOfBoundsException();
47
        }
48
49 2
        $file->next();
50
51 2
        $content = $this->getCurrentLineContent($file);
52
53 2
        $this->walker->goToLine($file, $file->key() - 1);
54
55 2
        return $content;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2 View Code Duplication
    public function getPreviousLineContent(\SplFileObject $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 2
        if ($file->key() - 1 < 0) {
64 1
            throw new OutOfBoundsException();
65
        }
66
67 1
        $file->seek($file->key() - 1);
68
69 1
        $content = $this->getCurrentLineContent($file);
70
71 1
        $this->walker->goToLine($file, $file->key() + 1);
72
73 1
        return $content;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function getCurrentLineContent(\SplFileObject $file)
80
    {
81 1
        $file->seek($file->key());
82
83 1
        $content = $file->current();
84
85 1
        return $this->cleanLineContent(is_string($content) ? $content : '');
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 3
    public function getLineContent(\SplFileObject $file, $lineNumber)
92
    {
93 3
        if ($lineNumber > $this->walker->countLines($file) || $lineNumber < 0) {
94 2
            throw new OutOfBoundsException();
95
        }
96
97 1
        $originalLineNumber = $file->key();
98
99 1
        $this->walker->goToLine($file, $lineNumber);
100
101 1
        $content = $this->getCurrentLineContent($file);
102
103 1
        $this->walker->goToLine($file, $originalLineNumber);
104
105 1
        return $content;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function getNextCharacterContent(\SplFileObject $file)
112
    {
113 1
        $originalPosition = $file->ftell();
114
115 1
        $character = $file->fgetc();
116
117 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
118
119 1
        return $character;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1 View Code Duplication
    public function getPreviousCharacterContent(\SplFileObject $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127 1
        $originalPosition = $file->ftell();
128
129 1
        $this->walker->goBeforeCharacter($file, $originalPosition - 1);
130
131 1
        $character = $file->fgetc();
132
133 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
134
135 1
        return $character;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 1 View Code Duplication
    public function getCharacterContent(\SplFileObject $file, $characterNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143 1
        $originalPosition = $file->ftell();
144
145 1
        $this->walker->goBeforeCharacter($file, $characterNumber);
146
147 1
        $character = $this->getNextCharacterContent($file);
148
149 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
150
151 1
        return $character;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 1
    public function cleanLineContent($content)
158
    {
159 1
        return trim($content);
160
    }
161
}
162