Completed
Push — develop ( d383bc...d9bd45 )
by Adrien
23:59
created

ColumnCellIterator::seek()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 1
dl 10
loc 10
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7
8
class ColumnCellIterator extends CellIterator
9
{
10
    /**
11
     * Current iterator position.
12
     *
13
     * @var int
14
     */
15
    private $currentRow;
16
17
    /**
18
     * Column index.
19
     *
20
     * @var string
21
     */
22
    private $columnIndex;
23
24
    /**
25
     * Start position.
26
     *
27
     * @var int
28
     */
29
    private $startRow = 1;
30
31
    /**
32
     * End position.
33
     *
34
     * @var int
35
     */
36
    private $endRow = 1;
37
38
    /**
39
     * Create a new row iterator.
40
     *
41
     * @param Worksheet $subject The worksheet to iterate over
42
     * @param string $columnIndex The column that we want to iterate
43
     * @param int $startRow The row number at which to start iterating
44
     * @param int $endRow Optionally, the row number at which to stop iterating
45
     */
46 6 View Code Duplication
    public function __construct(Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null)
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...
47
    {
48
        // Set subject
49 6
        $this->worksheet = $subject;
50 6
        $this->columnIndex = Coordinate::columnIndexFromString($columnIndex);
51 6
        $this->resetEnd($endRow);
52 6
        $this->resetStart($startRow);
53 6
    }
54
55
    /**
56
     * (Re)Set the start row and the current row pointer.
57
     *
58
     * @param int $startRow The row number at which to start iterating
59
     *
60
     * @throws PhpSpreadsheetException
61
     *
62
     * @return ColumnCellIterator
63
     */
64 6
    public function resetStart($startRow = 1)
65
    {
66 6
        $this->startRow = $startRow;
67 6
        $this->adjustForExistingOnlyRange();
68 6
        $this->seek($startRow);
69
70 6
        return $this;
71
    }
72
73
    /**
74
     * (Re)Set the end row.
75
     *
76
     * @param int $endRow The row number at which to stop iterating
77
     *
78
     * @throws PhpSpreadsheetException
79
     *
80
     * @return ColumnCellIterator
81
     */
82 6
    public function resetEnd($endRow = null)
83
    {
84 6
        $this->endRow = ($endRow) ? $endRow : $this->worksheet->getHighestRow();
85 6
        $this->adjustForExistingOnlyRange();
86
87 6
        return $this;
88
    }
89
90
    /**
91
     * Set the row pointer to the selected row.
92
     *
93
     * @param int $row The row number to set the current pointer at
94
     *
95
     * @throws PhpSpreadsheetException
96
     *
97
     * @return ColumnCellIterator
98
     */
99 6 View Code Duplication
    public function seek($row = 1)
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...
100
    {
101 6
        if (($row < $this->startRow) || ($row > $this->endRow)) {
102 1
            throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
103 6
        } elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $row))) {
0 ignored issues
show
Bug introduced by
$this->columnIndex of type string is incompatible with the type integer expected by parameter $columnIndex of PhpOffice\PhpSpreadsheet...lExistsByColumnAndRow(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        } elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow(/** @scrutinizer ignore-type */ $this->columnIndex, $row))) {
Loading history...
104
            throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
105
        }
106 6
        $this->currentRow = $row;
107
108 6
        return $this;
109
    }
110
111
    /**
112
     * Rewind the iterator to the starting row.
113
     */
114 2
    public function rewind()
115
    {
116 2
        $this->currentRow = $this->startRow;
117 2
    }
118
119
    /**
120
     * Return the current cell in this worksheet column.
121
     *
122
     * @return null|\PhpOffice\PhpSpreadsheet\Cell\Cell
123
     */
124 2
    public function current()
125
    {
126 2
        return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow);
0 ignored issues
show
Bug introduced by
$this->columnIndex of type string is incompatible with the type integer expected by parameter $columnIndex of PhpOffice\PhpSpreadsheet...getCellByColumnAndRow(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
        return $this->worksheet->getCellByColumnAndRow(/** @scrutinizer ignore-type */ $this->columnIndex, $this->currentRow);
Loading history...
127
    }
128
129
    /**
130
     * Return the current iterator key.
131
     *
132
     * @return int
133
     */
134 3
    public function key()
135
    {
136 3
        return $this->currentRow;
137
    }
138
139
    /**
140
     * Set the iterator to its next value.
141
     */
142 2
    public function next()
143
    {
144 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
145 2
            ++$this->currentRow;
146 2
        } while (($this->onlyExistingCells) &&
147 2
            (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
148 2
            ($this->currentRow <= $this->endRow));
149 2
    }
150
151
    /**
152
     * Set the iterator to its previous value.
153
     */
154 2
    public function prev()
155
    {
156 2
        if ($this->currentRow <= $this->startRow) {
157 1
            throw new PhpSpreadsheetException("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})");
158
        }
159
160 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
161 1
            --$this->currentRow;
162 1
        } while (($this->onlyExistingCells) &&
163 1
            (!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) &&
164 1
            ($this->currentRow >= $this->startRow));
165 1
    }
166
167
    /**
168
     * Indicate if more rows exist in the worksheet range of rows that we're iterating.
169
     *
170
     * @return bool
171
     */
172 2
    public function valid()
173
    {
174 2
        return $this->currentRow <= $this->endRow;
175
    }
176
177
    /**
178
     * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
179
     *
180
     * @throws PhpSpreadsheetException
181
     */
182 6 View Code Duplication
    protected function adjustForExistingOnlyRange()
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...
183
    {
184 6
        if ($this->onlyExistingCells) {
185
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) &&
0 ignored issues
show
Bug introduced by
$this->columnIndex of type string is incompatible with the type integer expected by parameter $columnIndex of PhpOffice\PhpSpreadsheet...lExistsByColumnAndRow(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
            while ((!$this->worksheet->cellExistsByColumnAndRow(/** @scrutinizer ignore-type */ $this->columnIndex, $this->startRow)) &&
Loading history...
186
                ($this->startRow <= $this->endRow)) {
187
                ++$this->startRow;
188
            }
189
            if ($this->startRow > $this->endRow) {
190
                throw new PhpSpreadsheetException('No cells exist within the specified range');
191
            }
192
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) &&
193
                ($this->endRow >= $this->startRow)) {
194
                --$this->endRow;
195
            }
196
            if ($this->endRow < $this->startRow) {
197
                throw new PhpSpreadsheetException('No cells exist within the specified range');
198
            }
199
        }
200 6
    }
201
}
202