Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

allow randomElements to accept a Traversable object #1389

Merged
merged 8 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,19 @@ public static function randomAscii()
*
* @return array New array with $count elements from $array
*/
public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
public static function randomElements($array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
Copy link
Owner

@fzaninotto fzaninotto Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you re-add the type hint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't know what you mean by this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just revert your changes to this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole point of this PR is to accept things other than arrays, so if I add the type hint back, that negates all of the new code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, you're right, my bad

{
if ($array instanceof \Traversable) {

$return = array();

foreach ($array as $element) {
$return[] = $element;
}

$array = $return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad practice to update a parameter. Prefer using a new variable with a ternary expression

}

$allKeys = array_keys($array);
$numKeys = count($allKeys);

Expand Down Expand Up @@ -206,7 +217,7 @@ public static function randomElements(array $array = array('a', 'b', 'c'), $coun
*/
public static function randomElement($array = array('a', 'b', 'c'))
{
if (!$array) {
if (!$array || ($array instanceof \Traversable && !count($array))) {
return null;
}
$elements = static::randomElements($array, 1);
Expand Down
16 changes: 16 additions & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Faker\Provider\Base as BaseProvider;
use PHPUnit\Framework\TestCase;
use Traversable;

class BaseTest extends TestCase
{
Expand Down Expand Up @@ -134,6 +135,11 @@ public function testRandomElementReturnsNullWhenArrayEmpty()
$this->assertNull(BaseProvider::randomElement(array()));
}

public function testRandomElementReturnsNullWhenCollectionEmpty()
{
$this->assertNull(BaseProvider::randomElement(new Collection(array())));
}

public function testRandomElementReturnsElementFromArray()
{
$elements = array('23', 'e', 32, '#');
Expand All @@ -146,6 +152,12 @@ public function testRandomElementReturnsElementFromAssociativeArray()
$this->assertContains(BaseProvider::randomElement($elements), $elements);
}

public function testRandomElementReturnsElementFromCollection()
{
$collection = new Collection(array('one', 'two', 'three'));
$this->assertContains(BaseProvider::randomElement($collection), $collection);
}

public function testShuffleReturnsStringWhenPassedAStringArgument()
{
$this->assertInternalType('string', BaseProvider::shuffle('foo'));
Expand Down Expand Up @@ -554,3 +566,7 @@ public function testRandomElements()
$this->assertContainsOnly('string', $allowDuplicates);
}
}

class Collection extends \ArrayObject
{
}