Completed
Push — develop ( f99eb8...c5339b )
by Adrien
31:45
created

ColumnCellIterator::adjustForExistingOnlyRange()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 37.1271

Importance

Changes 0
Metric Value
cc 8
eloc 12
nc 11
nop 0
dl 19
loc 19
rs 7.7777
c 0
b 0
f 0
ccs 3
cts 13
cp 0.2308
crap 37.1271
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class ColumnCellIterator extends CellIterator implements \Iterator
28
{
29
    /**
30
     * Column index.
31
     *
32
     * @var string
33
     */
34
    protected $columnIndex;
35
36
    /**
37
     * Start position.
38
     *
39
     * @var int
40
     */
41
    protected $startRow = 1;
42
43
    /**
44
     * End position.
45
     *
46
     * @var int
47
     */
48
    protected $endRow = 1;
49
50
    /**
51
     * Create a new row iterator.
52
     *
53
     * @param \PhpOffice\PhpSpreadsheet\Worksheet $subject The worksheet to iterate over
54
     * @param string $columnIndex The column that we want to iterate
55
     * @param int $startRow The row number at which to start iterating
56
     * @param int $endRow Optionally, the row number at which to stop iterating
57
     */
58 6 View Code Duplication
    public function __construct(\PhpOffice\PhpSpreadsheet\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...
59
    {
60
        // Set subject
61 6
        $this->subject = $subject;
62 6
        $this->columnIndex = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($columnIndex) - 1;
0 ignored issues
show
Documentation Bug introduced by
The property $columnIndex was declared of type string, but \PhpOffice\PhpSpreadshee...tring($columnIndex) - 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
63 6
        $this->resetEnd($endRow);
64 6
        $this->resetStart($startRow);
65 6
    }
66
67
    /**
68
     * Destructor.
69
     */
70 6
    public function __destruct()
71
    {
72 6
        unset($this->subject);
73 6
    }
74
75
    /**
76
     * (Re)Set the start row and the current row pointer.
77
     *
78
     * @param int $startRow The row number at which to start iterating
79
     *
80
     * @throws \PhpOffice\PhpSpreadsheet\Exception
81
     *
82
     * @return ColumnCellIterator
83
     */
84 6
    public function resetStart($startRow = 1)
85
    {
86 6
        $this->startRow = $startRow;
87 6
        $this->adjustForExistingOnlyRange();
88 6
        $this->seek($startRow);
89
90 6
        return $this;
91
    }
92
93
    /**
94
     * (Re)Set the end row.
95
     *
96
     * @param int $endRow The row number at which to stop iterating
97
     *
98
     * @throws \PhpOffice\PhpSpreadsheet\Exception
99
     *
100
     * @return ColumnCellIterator
101
     */
102 6
    public function resetEnd($endRow = null)
103
    {
104 6
        $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow();
0 ignored issues
show
Documentation Bug introduced by
It seems like $endRow ? $endRow : $thi...ubject->getHighestRow() can also be of type string. However, the property $endRow is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
105 6
        $this->adjustForExistingOnlyRange();
106
107 6
        return $this;
108
    }
109
110
    /**
111
     * Set the row pointer to the selected row.
112
     *
113
     * @param int $row The row number to set the current pointer at
114
     *
115
     * @throws \PhpOffice\PhpSpreadsheet\Exception
116
     *
117
     * @return ColumnCellIterator
118
     */
119 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...
120
    {
121 6
        if (($row < $this->startRow) || ($row > $this->endRow)) {
122 1
            throw new \PhpOffice\PhpSpreadsheet\Exception("Row $row is out of range ({$this->startRow} - {$this->endRow})");
123 6
        } elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($this->columnIndex, $row))) {
124
            throw new \PhpOffice\PhpSpreadsheet\Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
125
        }
126 6
        $this->position = $row;
127
128 6
        return $this;
129
    }
130
131
    /**
132
     * Rewind the iterator to the starting row.
133
     */
134 2
    public function rewind()
135
    {
136 2
        $this->position = $this->startRow;
137 2
    }
138
139
    /**
140
     * Return the current cell in this worksheet column.
141
     *
142
     * @return null|\PhpOffice\PhpSpreadsheet\Cell
143
     */
144 2
    public function current()
145
    {
146 2
        return $this->subject->getCellByColumnAndRow($this->columnIndex, $this->position);
147
    }
148
149
    /**
150
     * Return the current iterator key.
151
     *
152
     * @return int
153
     */
154 3
    public function key()
155
    {
156 3
        return $this->position;
157
    }
158
159
    /**
160
     * Set the iterator to its next value.
161
     */
162 2 View Code Duplication
    public function next()
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...
163
    {
164
        do {
165 2
            ++$this->position;
166 2
        } while (($this->onlyExistingCells) &&
167
            (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) &&
168
            ($this->position <= $this->endRow));
169 2
    }
170
171
    /**
172
     * Set the iterator to its previous value.
173
     */
174 2
    public function prev()
175
    {
176 2
        if ($this->position <= $this->startRow) {
177 1
            throw new \PhpOffice\PhpSpreadsheet\Exception("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})");
178
        }
179
180
        do {
181 1
            --$this->position;
182 1
        } while (($this->onlyExistingCells) &&
183
            (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) &&
184
            ($this->position >= $this->startRow));
185 1
    }
186
187
    /**
188
     * Indicate if more rows exist in the worksheet range of rows that we're iterating.
189
     *
190
     * @return bool
191
     */
192 2
    public function valid()
193
    {
194 2
        return $this->position <= $this->endRow;
195
    }
196
197
    /**
198
     * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
199
     *
200
     * @throws \PhpOffice\PhpSpreadsheet\Exception
201
     */
202 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...
203
    {
204 6
        if ($this->onlyExistingCells) {
205
            while ((!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) &&
206
                ($this->startRow <= $this->endRow)) {
207
                ++$this->startRow;
208
            }
209
            if ($this->startRow > $this->endRow) {
210
                throw new \PhpOffice\PhpSpreadsheet\Exception('No cells exist within the specified range');
211
            }
212
            while ((!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) &&
213
                ($this->endRow >= $this->startRow)) {
214
                --$this->endRow;
215
            }
216
            if ($this->endRow < $this->startRow) {
217
                throw new \PhpOffice\PhpSpreadsheet\Exception('No cells exist within the specified range');
218
            }
219
        }
220 6
    }
221
}
222