Skip to content

Commit

Permalink
Force _fromSeqenceString result array values to int (#6)
Browse files Browse the repository at this point in the history
* Force _fromSeqenceString result array to (int)

as currently, the first value will be a string. The iteration with ++
will force the second loop to convert  to int, but the first loop
is using  as a string leading to errors with strongly typed methods.

Signed-off-by: Anna Larch <[email protected]>

* Update test/Horde/Imap/Client/IdsTest.php

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Kieran <[email protected]>
  • Loading branch information
miaulalala and bytestream authored Aug 10, 2022
1 parent 9053080 commit ceafee1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Horde/Imap/Client/Ids.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function add($ids)
} elseif ($add = $this->_resolveIds($ids)) {
if (is_array($this->_ids) && !empty($this->_ids)) {
foreach ($add as $val) {
$this->_ids[] = $val;
$this->_ids[] = (int)$val;
}
} else {
$this->_ids = $add;
Expand Down Expand Up @@ -363,10 +363,10 @@ protected function _fromSequenceString($str)
$range = explode(':', $val);
if (isset($range[1])) {
for ($i = min($range), $j = max($range); $i <= $j; ++$i) {
$ids[] = $i;
$ids[] = (int)$i;
}
} else {
$ids[] = $val;
$ids[] = (int)$val;
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/Horde/Imap/Client/IdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,44 @@ public function testSerialize()
);
}

public function testForcedIntForRange()
{
$ids = new Horde_Imap_Client_Ids('1:3');
$this->assertEquals(
array(1, 2, 3),
iterator_to_array($ids)
);

foreach (iterator_to_array($ids) as $id) {
$this->assertIsInt($id);
}
}

public function testForcedIntForSequence()
{
$ids = new Horde_Imap_Client_Ids('1,5,7');
$this->assertEquals(
array(1, 5, 7),
iterator_to_array($ids)
);

foreach (iterator_to_array($ids) as $id) {
$this->assertIsInt($id);
}
}

public function testAddingWithForcedIntConversion()
{
$ids = new Horde_Imap_Client_Ids('1,5,7');
$ids->add('101:103');

$this->assertEquals(
array(1, 5, 7, 101, 102, 103),
iterator_to_array($ids)
);

foreach (iterator_to_array($ids) as $id) {
$this->assertIsInt($id);
}
}
}

0 comments on commit ceafee1

Please sign in to comment.