SimpleReader   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 34.9 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.23%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 2
dl 52
loc 149
ccs 49
cts 52
cp 0.9423
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNextLineContent() 14 14 2
A getPreviousLineContent() 14 14 2
A getLinesRange() 0 8 3
A getCurrentLineContent() 0 8 2
A getLineContent() 0 16 3
A getNextCharacterContent() 0 10 1
A getPreviousCharacterContent() 12 12 1
A getCharacterContent() 12 12 1
A cleanLineContent() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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