Commit 59862270 authored by Korko's avatar Korko Committed by StyleCI Bot
Browse files

Apply fixes from StyleCI

parent 94b4c544
......@@ -49,8 +49,8 @@ class RandomFormRequest extends Request
'exclusions.'.$key => 'sometimes|array',
];
$exclusions = $this->request->get('exclusions') ?: array();
$exclusions = isset($exclusions[$key]) ? $exclusions[$key] : array();
$exclusions = $this->request->get('exclusions') ?: [];
$exclusions = isset($exclusions[$key]) ? $exclusions[$key] : [];
foreach ($exclusions as $key2 => $name2) {
$rules += [
'exclusions.'.$key.'.'.$key2 => 'integer|fieldinkeys:name,'.$key,
......
......@@ -3,7 +3,6 @@
namespace Korko\SecretSanta\Libs;
use Closure;
use Exception;
use Traversable;
class Combinator
......@@ -12,8 +11,8 @@ class Combinator
{
$combinations = [];
foreach(self::getGenerator($elements) as $combination) {
if(isset($validator) && !self::isCombinationValid($combination, $validator)) {
foreach (self::getGenerator($elements) as $combination) {
if (isset($validator) && !self::isCombinationValid($combination, $validator)) {
continue;
}
......@@ -25,24 +24,24 @@ class Combinator
public static function getGenerator(array $elements) : Traversable
{
if($elements === array()) {
if ($elements === []) {
yield;
}
$rndElements = $elements;
shuffle($rndElements);
foreach(self::getGenerator_internal($rndElements) as $combination) {
foreach (self::getGenerator_internal($rndElements) as $combination) {
yield array_combine($elements, $combination);
}
}
private static function getGenerator_internal(array $elements, $combination = array()) : Traversable
private static function getGenerator_internal(array $elements, $combination = []) : Traversable
{
if($elements === array()) {
if ($elements === []) {
yield $combination;
} else {
foreach($elements as $idx => $element) {
foreach ($elements as $idx => $element) {
// Take the first element
// Add it to the combination
......@@ -60,11 +59,12 @@ class Combinator
private static function isCombinationValid(array $combination, Closure $validator) : bool
{
foreach($combination as $elementA => $elementB) {
if(!$validator($elementA, $elementB)) {
foreach ($combination as $elementA => $elementB) {
if (!$validator($elementA, $elementB)) {
return false;
}
}
return true;
}
}
......@@ -8,20 +8,21 @@ class Randomizer
{
public static function randomize(array $participants) : array
{
$combinations = Combinator::all(array_keys($participants), function($santa, $target) use($participants) {
$combinations = Combinator::all(array_keys($participants), function ($santa, $target) use ($participants) {
return $santa !== $target && !in_array($target, $participants[$santa]['exclusions']);
});
if($combinations === array()) {
if ($combinations === []) {
throw new Exception('Cannot resolve '.json_encode($participants));
}
$rnd = array_rand($combinations);
$combination = $combinations[$rnd];
foreach($combination as $idx => $idx2) {
foreach ($combination as $idx => $idx2) {
$combination[$idx] = $participants[$idx2]['name'];
}
return $combination;
}
}
......@@ -6,7 +6,7 @@ class CombinatorTest extends TestCase
{
public function assertCombination($valid, $test)
{
$test = array_map(function($el) {
$test = array_map(function ($el) {
return implode('', $el);
}, $test);
$this->assertEquals(sort($valid), sort($test));
......@@ -14,38 +14,45 @@ class CombinatorTest extends TestCase
public function testNoFilter()
{
$this->assertCombination(array(
'AB', 'BA'
), Combinator::all(['A', 'B']));
$this->assertCombination([
'AB', 'BA',
], Combinator::all(['A', 'B']));
}
public function testNoExclusion()
{
$this->assertCombination(array(
'BA'
), Combinator::all(['A', 'B'], function($a, $b) { return $a !== $b; }));
$this->assertCombination([
'BA',
], Combinator::all(['A', 'B'], function ($a, $b) {
return $a !== $b;
}));
$this->assertCombination(array(
'BCA', 'CAB'
), Combinator::all(['A', 'B', 'C'], function($a, $b) { return $a !== $b; }));
$this->assertCombination([
'BCA', 'CAB',
], Combinator::all(['A', 'B', 'C'], function ($a, $b) {
return $a !== $b;
}));
$this->assertCombination(array(
$this->assertCombination([
'BCDA', 'BDAC', 'BADC',
'CDAB', 'CADB', 'CDBA',
'DABC', 'DCBA', 'DCAB'
), Combinator::all(['A', 'B', 'C', 'D'], function($a, $b) { return $a !== $b; }));
'DABC', 'DCBA', 'DCAB',
], Combinator::all(['A', 'B', 'C', 'D'], function ($a, $b) {
return $a !== $b;
}));
}
public function testSimpleExclusion()
{
$exclusions = array('A' => ['C']);
$exclusions = ['A' => ['C']];
$this->assertCombination(array(
'BCA'
), Combinator::all(['A', 'B', 'C'], function($elementA, $elementB) use($exclusions) {
$this->assertCombination([
'BCA',
], Combinator::all(['A', 'B', 'C'], function ($elementA, $elementB) use ($exclusions) {
return $elementA !== $elementB && (!isset($exclusions[$elementA]) || !in_array($elementB, $exclusions[$elementA]));
}));
}
/*
public function testComplexExclusion()
{
......@@ -59,16 +66,16 @@ class CombinatorTest extends TestCase
*/
public function testInvalidExclusion()
{
$this->assertEquals([], Combinator::all(['A', 'B', 'C'], function($elementA, $elementB) {
$this->assertEquals([], Combinator::all(['A', 'B', 'C'], function ($elementA, $elementB) {
return false;
}));
}
public function testImpossibleSolution()
{
$exclusions = array('A' => ['B', 'C']);
$exclusions = ['A' => ['B', 'C']];
$this->assertEquals([], Combinator::all(['A', 'B', 'C'], function($elementA, $elementB) use($exclusions) {
$this->assertEquals([], Combinator::all(['A', 'B', 'C'], function ($elementA, $elementB) use ($exclusions) {
return $elementA !== $elementB && (!isset($exclusions[$elementA]) || !in_array($elementB, $exclusions[$elementA]));
}));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment