Completed
Push — master ( a7b092...3bee99 )
by Michaël
02:47
created

SimpleReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
        return $this->cleanLineContent($file->current());
0 ignored issues
show
Bug introduced by
It seems like $file->current() targeting SplFileObject::current() can also be of type array; however, TextFile\Reader\SimpleReader::cleanLineContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function getLineContent(\SplFileObject $file, $lineNumber)
90
    {
91 3
        if ($lineNumber > $this->walker->countLines($file) || $lineNumber < 0) {
92 2
            throw new OutOfBoundsException();
93
        }
94
95 1
        $originalLineNumber = $file->key();
96
97 1
        $this->walker->goToLine($file, $lineNumber);
98
99 1
        $content = $this->getCurrentLineContent($file);
100
101 1
        $this->walker->goToLine($file, $originalLineNumber);
102
103 1
        return $content;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 1
    public function getNextCharacterContent(\SplFileObject $file)
110
    {
111 1
        $originalPosition = $file->ftell();
112
113 1
        $character = $file->fgetc();
114
115 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
116
117 1
        return $character;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 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...
124
    {
125 1
        $originalPosition = $file->ftell();
126
127 1
        $this->walker->goBeforeCharacter($file, $originalPosition - 1);
128
129 1
        $character = $file->fgetc();
130
131 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
132
133 1
        return $character;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 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...
140
    {
141 1
        $originalPosition = $file->ftell();
142
143 1
        $this->walker->goBeforeCharacter($file, $characterNumber);
144
145 1
        $character = $this->getNextCharacterContent($file);
146
147 1
        $this->walker->goBeforeCharacter($file, $originalPosition);
148
149 1
        return $character;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 1
    public function cleanLineContent($content)
156
    {
157 1
        return trim($content);
158
    }
159
}
160