Bill settlement for multiple users











up vote
3
down vote

favorite
1












I want to improve the logic for bill settlement and calculation of amount due. I managed to write code with T.D.D., but I think those loops could be improved somehow. Do you have any ideas on that front?



<?php

namespace TestsUnit;

use PHPUnitFrameworkTestCase;

class BillTest extends TestCase
{
protected $dataSet = [
[
'day' => 1,
'amount' => 50,
'paid_by' => 'tanu',
'friends' => ['kasun', 'tanu']
],
[
'day' => 2,
'amount' => 100,
'paid_by' => 'kasun',
'friends' => ['kasun', 'tanu', 'liam']
],
[
'day' => 3,
'amount' => 100,
'paid_by' => 'liam',
'friends' => ['liam', 'tanu', 'liam']
]
];

/**
* @test
*/
public function it_throws_exception_if_data_is_not_a_multidimensional_array()
{
$this->expectException(InvalidArgumentException::class);

new Bill();
}

/**
* @test
*/
public function it_throws_exception_if_data_is_invalid()
{
$this->expectException(InvalidArgumentException::class);

$invalidData = [
[
'baz' => 1,
'foo' => 50,
'bar' => 'foo',
'zoo' => ['bar', 'foo']
]
];

new Bill($invalidData);
}

/**
* @test
*/
public function it_should_have_a_valid_data_set()
{
$expense = new Bill($this->dataSet);

$this->assertTrue($expense->hasValidDataSet());
}

/**
* @test
*/
public function it_calculates_total_number_of_days()
{
$this->assertEquals(3, (new Bill($this->dataSet))->days());
}

/**
* @test
*/
public function it_calculates_total_bill_amount()
{
$this->assertEquals(250, (new Bill($this->dataSet))->total());
}

/**
* @test
*/
public function it_calculates_each_users_total_expense()
{
$expected = [
'tanu' => 50,
'kasun' => 100,
'liam' => 100
];

$this->assertEquals($expected, (new Bill($this->dataSet))->expenseByUsers());
}

/**
* @test
*/
public function it_calculates_due_amount_of_each_users()
{
$expected = [
'tanu' => 66.66,
'kasun' => 25,
'liam' => 33.33
];

$this->assertEquals($expected, (new Bill($this->dataSet))->dueByUsers());
}

/**
* @test
*/
public function it_calsulates_settlement_of_each_friends()
{
$expected = [
'tanu' => ,
'kasun' => [
[
'from' => 'tanu',
'amount' => 41.66
],
[
'from' => 'liam',
'amount' => 8.33
]
],
'liam' => [
[
'from' => 'tanu',
'amount' => 33.33,
]
]
];

$this->assertEquals($expected, (new Bill($this->dataSet))->settlement());
}
}

class Bill
{
/**
* Keys of the data item
*
* @var array
*/
protected $keys = ['day', 'amount', 'paid_by', 'friends'];

/**
* Settlement data set
*
* @var array
*/
protected $data;

/**
* Bill constructor.
*
* @param array $data
*/
public function __construct($data)
{
if (! $this->isMultidimensionalArray($data) || ! $this->hasValidDataSet($data)) {
$keys = implode(', ', $this->keys);

throw new InvalidArgumentException(
"Data should contains only {$keys} keys."
);
}

$this->data = $data;
}

/**
* Check data set is valid
*
* @param $data
* @return bool
*/
public function hasValidDataSet($data = null)
{
$data = is_null($data) ? $this->data : $data;

foreach ($data as $item) {
if (count(array_diff_key(array_flip($this->keys), $item))) {
return false;
}
}

return true;
}

/**
* Number of days of the bill
*
* @return int
*/
public function days()
{
return count($this->fetchValuesByKey('day'));
}

/**
* Total bill amount
*
* @return int
*/
public function total()
{
return array_sum($this->fetchValuesByKey('amount'));
}

/**
* All bill paid users
*
* @return array
*/
public function users()
{
return array_unique($this->fetchValuesByKey('paid_by'));
}

/**
* Total expense amount by each user
*
* @return array
*/
public function expenseByUsers()
{
$expense = ;

foreach ($this->data as $item) {
$user = $item['paid_by'];

$expense[$user] = isset($expense[$user])
? $expense[$user] + $item['amount']
: $item['amount'];
}

return $expense;
}

/**
* Due amount of the each user
*
* @return array
*/
public function dueByUsers()
{
$due = ;

foreach ($this->data as $item) {
$share = round($item['amount'] / count($item['friends']), 2);

$users = array_filter($item['friends'], function ($friend) use ($item) {
return $friend != $item['paid_by'];
});

foreach ($users as $user) {
$due[$user] = isset($due[$user])
? $due[$user] + $share
: $share;
}
}

return $due;
}

/**
* Settlement of each user
*
* @return array
*/
public function settlement()
{
$settlements = ;

$users = $this->users();
$dueUsers = $this->dueByUsers();

foreach ($users as $creditor) {
$settlements[$creditor] = ;

foreach ($dueUsers as $debtor => $due) {
if ($creditor == $debtor) {
continue;
}

if ($dueUsers[$creditor] > $due) {
continue;
}

$owe = [
'from' => $debtor,
'amount' => $due - $dueUsers[$creditor]
];

array_push($settlements[$creditor], $owe);
}
}

return $settlements;
}

/**
* Fetch data values by key
*
* @param $key
* @return array
*/
protected function fetchValuesByKey($key)
{
$values = ;

foreach ($this->data as $item) {
$values = $item[$key];
}

return $values;
}

/**
* Check multidimensional array
*
* @param $data
* @return bool
*/
protected function isMultidimensionalArray($data)
{
return count($data) !== count($data, COUNT_RECURSIVE);
}
}









share|improve this question
















bumped to the homepage by Community 9 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    3
    down vote

    favorite
    1












    I want to improve the logic for bill settlement and calculation of amount due. I managed to write code with T.D.D., but I think those loops could be improved somehow. Do you have any ideas on that front?



    <?php

    namespace TestsUnit;

    use PHPUnitFrameworkTestCase;

    class BillTest extends TestCase
    {
    protected $dataSet = [
    [
    'day' => 1,
    'amount' => 50,
    'paid_by' => 'tanu',
    'friends' => ['kasun', 'tanu']
    ],
    [
    'day' => 2,
    'amount' => 100,
    'paid_by' => 'kasun',
    'friends' => ['kasun', 'tanu', 'liam']
    ],
    [
    'day' => 3,
    'amount' => 100,
    'paid_by' => 'liam',
    'friends' => ['liam', 'tanu', 'liam']
    ]
    ];

    /**
    * @test
    */
    public function it_throws_exception_if_data_is_not_a_multidimensional_array()
    {
    $this->expectException(InvalidArgumentException::class);

    new Bill();
    }

    /**
    * @test
    */
    public function it_throws_exception_if_data_is_invalid()
    {
    $this->expectException(InvalidArgumentException::class);

    $invalidData = [
    [
    'baz' => 1,
    'foo' => 50,
    'bar' => 'foo',
    'zoo' => ['bar', 'foo']
    ]
    ];

    new Bill($invalidData);
    }

    /**
    * @test
    */
    public function it_should_have_a_valid_data_set()
    {
    $expense = new Bill($this->dataSet);

    $this->assertTrue($expense->hasValidDataSet());
    }

    /**
    * @test
    */
    public function it_calculates_total_number_of_days()
    {
    $this->assertEquals(3, (new Bill($this->dataSet))->days());
    }

    /**
    * @test
    */
    public function it_calculates_total_bill_amount()
    {
    $this->assertEquals(250, (new Bill($this->dataSet))->total());
    }

    /**
    * @test
    */
    public function it_calculates_each_users_total_expense()
    {
    $expected = [
    'tanu' => 50,
    'kasun' => 100,
    'liam' => 100
    ];

    $this->assertEquals($expected, (new Bill($this->dataSet))->expenseByUsers());
    }

    /**
    * @test
    */
    public function it_calculates_due_amount_of_each_users()
    {
    $expected = [
    'tanu' => 66.66,
    'kasun' => 25,
    'liam' => 33.33
    ];

    $this->assertEquals($expected, (new Bill($this->dataSet))->dueByUsers());
    }

    /**
    * @test
    */
    public function it_calsulates_settlement_of_each_friends()
    {
    $expected = [
    'tanu' => ,
    'kasun' => [
    [
    'from' => 'tanu',
    'amount' => 41.66
    ],
    [
    'from' => 'liam',
    'amount' => 8.33
    ]
    ],
    'liam' => [
    [
    'from' => 'tanu',
    'amount' => 33.33,
    ]
    ]
    ];

    $this->assertEquals($expected, (new Bill($this->dataSet))->settlement());
    }
    }

    class Bill
    {
    /**
    * Keys of the data item
    *
    * @var array
    */
    protected $keys = ['day', 'amount', 'paid_by', 'friends'];

    /**
    * Settlement data set
    *
    * @var array
    */
    protected $data;

    /**
    * Bill constructor.
    *
    * @param array $data
    */
    public function __construct($data)
    {
    if (! $this->isMultidimensionalArray($data) || ! $this->hasValidDataSet($data)) {
    $keys = implode(', ', $this->keys);

    throw new InvalidArgumentException(
    "Data should contains only {$keys} keys."
    );
    }

    $this->data = $data;
    }

    /**
    * Check data set is valid
    *
    * @param $data
    * @return bool
    */
    public function hasValidDataSet($data = null)
    {
    $data = is_null($data) ? $this->data : $data;

    foreach ($data as $item) {
    if (count(array_diff_key(array_flip($this->keys), $item))) {
    return false;
    }
    }

    return true;
    }

    /**
    * Number of days of the bill
    *
    * @return int
    */
    public function days()
    {
    return count($this->fetchValuesByKey('day'));
    }

    /**
    * Total bill amount
    *
    * @return int
    */
    public function total()
    {
    return array_sum($this->fetchValuesByKey('amount'));
    }

    /**
    * All bill paid users
    *
    * @return array
    */
    public function users()
    {
    return array_unique($this->fetchValuesByKey('paid_by'));
    }

    /**
    * Total expense amount by each user
    *
    * @return array
    */
    public function expenseByUsers()
    {
    $expense = ;

    foreach ($this->data as $item) {
    $user = $item['paid_by'];

    $expense[$user] = isset($expense[$user])
    ? $expense[$user] + $item['amount']
    : $item['amount'];
    }

    return $expense;
    }

    /**
    * Due amount of the each user
    *
    * @return array
    */
    public function dueByUsers()
    {
    $due = ;

    foreach ($this->data as $item) {
    $share = round($item['amount'] / count($item['friends']), 2);

    $users = array_filter($item['friends'], function ($friend) use ($item) {
    return $friend != $item['paid_by'];
    });

    foreach ($users as $user) {
    $due[$user] = isset($due[$user])
    ? $due[$user] + $share
    : $share;
    }
    }

    return $due;
    }

    /**
    * Settlement of each user
    *
    * @return array
    */
    public function settlement()
    {
    $settlements = ;

    $users = $this->users();
    $dueUsers = $this->dueByUsers();

    foreach ($users as $creditor) {
    $settlements[$creditor] = ;

    foreach ($dueUsers as $debtor => $due) {
    if ($creditor == $debtor) {
    continue;
    }

    if ($dueUsers[$creditor] > $due) {
    continue;
    }

    $owe = [
    'from' => $debtor,
    'amount' => $due - $dueUsers[$creditor]
    ];

    array_push($settlements[$creditor], $owe);
    }
    }

    return $settlements;
    }

    /**
    * Fetch data values by key
    *
    * @param $key
    * @return array
    */
    protected function fetchValuesByKey($key)
    {
    $values = ;

    foreach ($this->data as $item) {
    $values = $item[$key];
    }

    return $values;
    }

    /**
    * Check multidimensional array
    *
    * @param $data
    * @return bool
    */
    protected function isMultidimensionalArray($data)
    {
    return count($data) !== count($data, COUNT_RECURSIVE);
    }
    }









    share|improve this question
















    bumped to the homepage by Community 9 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I want to improve the logic for bill settlement and calculation of amount due. I managed to write code with T.D.D., but I think those loops could be improved somehow. Do you have any ideas on that front?



      <?php

      namespace TestsUnit;

      use PHPUnitFrameworkTestCase;

      class BillTest extends TestCase
      {
      protected $dataSet = [
      [
      'day' => 1,
      'amount' => 50,
      'paid_by' => 'tanu',
      'friends' => ['kasun', 'tanu']
      ],
      [
      'day' => 2,
      'amount' => 100,
      'paid_by' => 'kasun',
      'friends' => ['kasun', 'tanu', 'liam']
      ],
      [
      'day' => 3,
      'amount' => 100,
      'paid_by' => 'liam',
      'friends' => ['liam', 'tanu', 'liam']
      ]
      ];

      /**
      * @test
      */
      public function it_throws_exception_if_data_is_not_a_multidimensional_array()
      {
      $this->expectException(InvalidArgumentException::class);

      new Bill();
      }

      /**
      * @test
      */
      public function it_throws_exception_if_data_is_invalid()
      {
      $this->expectException(InvalidArgumentException::class);

      $invalidData = [
      [
      'baz' => 1,
      'foo' => 50,
      'bar' => 'foo',
      'zoo' => ['bar', 'foo']
      ]
      ];

      new Bill($invalidData);
      }

      /**
      * @test
      */
      public function it_should_have_a_valid_data_set()
      {
      $expense = new Bill($this->dataSet);

      $this->assertTrue($expense->hasValidDataSet());
      }

      /**
      * @test
      */
      public function it_calculates_total_number_of_days()
      {
      $this->assertEquals(3, (new Bill($this->dataSet))->days());
      }

      /**
      * @test
      */
      public function it_calculates_total_bill_amount()
      {
      $this->assertEquals(250, (new Bill($this->dataSet))->total());
      }

      /**
      * @test
      */
      public function it_calculates_each_users_total_expense()
      {
      $expected = [
      'tanu' => 50,
      'kasun' => 100,
      'liam' => 100
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->expenseByUsers());
      }

      /**
      * @test
      */
      public function it_calculates_due_amount_of_each_users()
      {
      $expected = [
      'tanu' => 66.66,
      'kasun' => 25,
      'liam' => 33.33
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->dueByUsers());
      }

      /**
      * @test
      */
      public function it_calsulates_settlement_of_each_friends()
      {
      $expected = [
      'tanu' => ,
      'kasun' => [
      [
      'from' => 'tanu',
      'amount' => 41.66
      ],
      [
      'from' => 'liam',
      'amount' => 8.33
      ]
      ],
      'liam' => [
      [
      'from' => 'tanu',
      'amount' => 33.33,
      ]
      ]
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->settlement());
      }
      }

      class Bill
      {
      /**
      * Keys of the data item
      *
      * @var array
      */
      protected $keys = ['day', 'amount', 'paid_by', 'friends'];

      /**
      * Settlement data set
      *
      * @var array
      */
      protected $data;

      /**
      * Bill constructor.
      *
      * @param array $data
      */
      public function __construct($data)
      {
      if (! $this->isMultidimensionalArray($data) || ! $this->hasValidDataSet($data)) {
      $keys = implode(', ', $this->keys);

      throw new InvalidArgumentException(
      "Data should contains only {$keys} keys."
      );
      }

      $this->data = $data;
      }

      /**
      * Check data set is valid
      *
      * @param $data
      * @return bool
      */
      public function hasValidDataSet($data = null)
      {
      $data = is_null($data) ? $this->data : $data;

      foreach ($data as $item) {
      if (count(array_diff_key(array_flip($this->keys), $item))) {
      return false;
      }
      }

      return true;
      }

      /**
      * Number of days of the bill
      *
      * @return int
      */
      public function days()
      {
      return count($this->fetchValuesByKey('day'));
      }

      /**
      * Total bill amount
      *
      * @return int
      */
      public function total()
      {
      return array_sum($this->fetchValuesByKey('amount'));
      }

      /**
      * All bill paid users
      *
      * @return array
      */
      public function users()
      {
      return array_unique($this->fetchValuesByKey('paid_by'));
      }

      /**
      * Total expense amount by each user
      *
      * @return array
      */
      public function expenseByUsers()
      {
      $expense = ;

      foreach ($this->data as $item) {
      $user = $item['paid_by'];

      $expense[$user] = isset($expense[$user])
      ? $expense[$user] + $item['amount']
      : $item['amount'];
      }

      return $expense;
      }

      /**
      * Due amount of the each user
      *
      * @return array
      */
      public function dueByUsers()
      {
      $due = ;

      foreach ($this->data as $item) {
      $share = round($item['amount'] / count($item['friends']), 2);

      $users = array_filter($item['friends'], function ($friend) use ($item) {
      return $friend != $item['paid_by'];
      });

      foreach ($users as $user) {
      $due[$user] = isset($due[$user])
      ? $due[$user] + $share
      : $share;
      }
      }

      return $due;
      }

      /**
      * Settlement of each user
      *
      * @return array
      */
      public function settlement()
      {
      $settlements = ;

      $users = $this->users();
      $dueUsers = $this->dueByUsers();

      foreach ($users as $creditor) {
      $settlements[$creditor] = ;

      foreach ($dueUsers as $debtor => $due) {
      if ($creditor == $debtor) {
      continue;
      }

      if ($dueUsers[$creditor] > $due) {
      continue;
      }

      $owe = [
      'from' => $debtor,
      'amount' => $due - $dueUsers[$creditor]
      ];

      array_push($settlements[$creditor], $owe);
      }
      }

      return $settlements;
      }

      /**
      * Fetch data values by key
      *
      * @param $key
      * @return array
      */
      protected function fetchValuesByKey($key)
      {
      $values = ;

      foreach ($this->data as $item) {
      $values = $item[$key];
      }

      return $values;
      }

      /**
      * Check multidimensional array
      *
      * @param $data
      * @return bool
      */
      protected function isMultidimensionalArray($data)
      {
      return count($data) !== count($data, COUNT_RECURSIVE);
      }
      }









      share|improve this question















      I want to improve the logic for bill settlement and calculation of amount due. I managed to write code with T.D.D., but I think those loops could be improved somehow. Do you have any ideas on that front?



      <?php

      namespace TestsUnit;

      use PHPUnitFrameworkTestCase;

      class BillTest extends TestCase
      {
      protected $dataSet = [
      [
      'day' => 1,
      'amount' => 50,
      'paid_by' => 'tanu',
      'friends' => ['kasun', 'tanu']
      ],
      [
      'day' => 2,
      'amount' => 100,
      'paid_by' => 'kasun',
      'friends' => ['kasun', 'tanu', 'liam']
      ],
      [
      'day' => 3,
      'amount' => 100,
      'paid_by' => 'liam',
      'friends' => ['liam', 'tanu', 'liam']
      ]
      ];

      /**
      * @test
      */
      public function it_throws_exception_if_data_is_not_a_multidimensional_array()
      {
      $this->expectException(InvalidArgumentException::class);

      new Bill();
      }

      /**
      * @test
      */
      public function it_throws_exception_if_data_is_invalid()
      {
      $this->expectException(InvalidArgumentException::class);

      $invalidData = [
      [
      'baz' => 1,
      'foo' => 50,
      'bar' => 'foo',
      'zoo' => ['bar', 'foo']
      ]
      ];

      new Bill($invalidData);
      }

      /**
      * @test
      */
      public function it_should_have_a_valid_data_set()
      {
      $expense = new Bill($this->dataSet);

      $this->assertTrue($expense->hasValidDataSet());
      }

      /**
      * @test
      */
      public function it_calculates_total_number_of_days()
      {
      $this->assertEquals(3, (new Bill($this->dataSet))->days());
      }

      /**
      * @test
      */
      public function it_calculates_total_bill_amount()
      {
      $this->assertEquals(250, (new Bill($this->dataSet))->total());
      }

      /**
      * @test
      */
      public function it_calculates_each_users_total_expense()
      {
      $expected = [
      'tanu' => 50,
      'kasun' => 100,
      'liam' => 100
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->expenseByUsers());
      }

      /**
      * @test
      */
      public function it_calculates_due_amount_of_each_users()
      {
      $expected = [
      'tanu' => 66.66,
      'kasun' => 25,
      'liam' => 33.33
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->dueByUsers());
      }

      /**
      * @test
      */
      public function it_calsulates_settlement_of_each_friends()
      {
      $expected = [
      'tanu' => ,
      'kasun' => [
      [
      'from' => 'tanu',
      'amount' => 41.66
      ],
      [
      'from' => 'liam',
      'amount' => 8.33
      ]
      ],
      'liam' => [
      [
      'from' => 'tanu',
      'amount' => 33.33,
      ]
      ]
      ];

      $this->assertEquals($expected, (new Bill($this->dataSet))->settlement());
      }
      }

      class Bill
      {
      /**
      * Keys of the data item
      *
      * @var array
      */
      protected $keys = ['day', 'amount', 'paid_by', 'friends'];

      /**
      * Settlement data set
      *
      * @var array
      */
      protected $data;

      /**
      * Bill constructor.
      *
      * @param array $data
      */
      public function __construct($data)
      {
      if (! $this->isMultidimensionalArray($data) || ! $this->hasValidDataSet($data)) {
      $keys = implode(', ', $this->keys);

      throw new InvalidArgumentException(
      "Data should contains only {$keys} keys."
      );
      }

      $this->data = $data;
      }

      /**
      * Check data set is valid
      *
      * @param $data
      * @return bool
      */
      public function hasValidDataSet($data = null)
      {
      $data = is_null($data) ? $this->data : $data;

      foreach ($data as $item) {
      if (count(array_diff_key(array_flip($this->keys), $item))) {
      return false;
      }
      }

      return true;
      }

      /**
      * Number of days of the bill
      *
      * @return int
      */
      public function days()
      {
      return count($this->fetchValuesByKey('day'));
      }

      /**
      * Total bill amount
      *
      * @return int
      */
      public function total()
      {
      return array_sum($this->fetchValuesByKey('amount'));
      }

      /**
      * All bill paid users
      *
      * @return array
      */
      public function users()
      {
      return array_unique($this->fetchValuesByKey('paid_by'));
      }

      /**
      * Total expense amount by each user
      *
      * @return array
      */
      public function expenseByUsers()
      {
      $expense = ;

      foreach ($this->data as $item) {
      $user = $item['paid_by'];

      $expense[$user] = isset($expense[$user])
      ? $expense[$user] + $item['amount']
      : $item['amount'];
      }

      return $expense;
      }

      /**
      * Due amount of the each user
      *
      * @return array
      */
      public function dueByUsers()
      {
      $due = ;

      foreach ($this->data as $item) {
      $share = round($item['amount'] / count($item['friends']), 2);

      $users = array_filter($item['friends'], function ($friend) use ($item) {
      return $friend != $item['paid_by'];
      });

      foreach ($users as $user) {
      $due[$user] = isset($due[$user])
      ? $due[$user] + $share
      : $share;
      }
      }

      return $due;
      }

      /**
      * Settlement of each user
      *
      * @return array
      */
      public function settlement()
      {
      $settlements = ;

      $users = $this->users();
      $dueUsers = $this->dueByUsers();

      foreach ($users as $creditor) {
      $settlements[$creditor] = ;

      foreach ($dueUsers as $debtor => $due) {
      if ($creditor == $debtor) {
      continue;
      }

      if ($dueUsers[$creditor] > $due) {
      continue;
      }

      $owe = [
      'from' => $debtor,
      'amount' => $due - $dueUsers[$creditor]
      ];

      array_push($settlements[$creditor], $owe);
      }
      }

      return $settlements;
      }

      /**
      * Fetch data values by key
      *
      * @param $key
      * @return array
      */
      protected function fetchValuesByKey($key)
      {
      $values = ;

      foreach ($this->data as $item) {
      $values = $item[$key];
      }

      return $values;
      }

      /**
      * Check multidimensional array
      *
      * @param $data
      * @return bool
      */
      protected function isMultidimensionalArray($data)
      {
      return count($data) !== count($data, COUNT_RECURSIVE);
      }
      }






      php object-oriented unit-testing iteration phpunit






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 10 at 21:40









      Sᴀᴍ Onᴇᴌᴀ

      8,07461751




      8,07461751










      asked Oct 1 '17 at 16:41









      Kalpa Perera

      542




      542





      bumped to the homepage by Community 9 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 9 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I don't see very many improvements I would make, though I do see a couple places where native PHP functions could be used instead of manually iterating over arrays.



          These changes (in the first couple blocks) shaved a few milliseconds off the total time - e.g. from ~194ms to 169ms.



          filtering friends in dueByUsers()



          The filtering of array elements at key friends in dueByUsers() could be simplified using array_diff().



          I.E. this block:




          $users = array_filter($item['friends'], function ($friend) use ($item) {
          return $friend != $item['paid_by'];
          });



          Could be simplified like the following:



          $users = array_diff($item['friends'], [$item['paid_by']]);


          fetchValuesByKey()



          The method fetchValuesByKey() can be simplified to basically a call to array_column():



          protected function fetchValuesByKey($key)
          {
          return array_column($this->data, $key);
          }


          Or perhaps it would be simpler to eliminate that method and replace its usage with calls to array_column().



          Array_push()



          On the PHP documentation for array_push(), there is a paragraph in the description:




          Note: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function.




          So you might want to consider doing that in the settlement() method.



          Functional approaches



          Functional approaches, like using array_map(), array_reduce(), etc. could be employed, although in some places it would require extra work (e.g. employing the use keyword to reference variables outside closures)) and may be slower because a function is being called.



          For example, method settlement() could be re-written as:



          public function settlement()
          {

          $users = $this->users();
          $dueUsers = $this->dueByUsers();
          return array_reduce($users, function($settlements, $creditor) use($dueUsers) {
          $settlements[$creditor] = array_reduce(array_keys($dueUsers), function($creditorSettlements, $debtor) use ($creditor, $dueUsers) {
          $due = $dueUsers[$debtor];
          if ($creditor !== $debtor && $dueUsers[$creditor] <= $due) {
          $creditorSettlements = [
          'from' => $debtor,
          'amount' => $due - $dueUsers[$creditor]
          ];
          }
          return $creditorSettlements;
          }, );
          return $settlements;
          }, );





          share|improve this answer























            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f176931%2fbill-settlement-for-multiple-users%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I don't see very many improvements I would make, though I do see a couple places where native PHP functions could be used instead of manually iterating over arrays.



            These changes (in the first couple blocks) shaved a few milliseconds off the total time - e.g. from ~194ms to 169ms.



            filtering friends in dueByUsers()



            The filtering of array elements at key friends in dueByUsers() could be simplified using array_diff().



            I.E. this block:




            $users = array_filter($item['friends'], function ($friend) use ($item) {
            return $friend != $item['paid_by'];
            });



            Could be simplified like the following:



            $users = array_diff($item['friends'], [$item['paid_by']]);


            fetchValuesByKey()



            The method fetchValuesByKey() can be simplified to basically a call to array_column():



            protected function fetchValuesByKey($key)
            {
            return array_column($this->data, $key);
            }


            Or perhaps it would be simpler to eliminate that method and replace its usage with calls to array_column().



            Array_push()



            On the PHP documentation for array_push(), there is a paragraph in the description:




            Note: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function.




            So you might want to consider doing that in the settlement() method.



            Functional approaches



            Functional approaches, like using array_map(), array_reduce(), etc. could be employed, although in some places it would require extra work (e.g. employing the use keyword to reference variables outside closures)) and may be slower because a function is being called.



            For example, method settlement() could be re-written as:



            public function settlement()
            {

            $users = $this->users();
            $dueUsers = $this->dueByUsers();
            return array_reduce($users, function($settlements, $creditor) use($dueUsers) {
            $settlements[$creditor] = array_reduce(array_keys($dueUsers), function($creditorSettlements, $debtor) use ($creditor, $dueUsers) {
            $due = $dueUsers[$debtor];
            if ($creditor !== $debtor && $dueUsers[$creditor] <= $due) {
            $creditorSettlements = [
            'from' => $debtor,
            'amount' => $due - $dueUsers[$creditor]
            ];
            }
            return $creditorSettlements;
            }, );
            return $settlements;
            }, );





            share|improve this answer



























              up vote
              0
              down vote













              I don't see very many improvements I would make, though I do see a couple places where native PHP functions could be used instead of manually iterating over arrays.



              These changes (in the first couple blocks) shaved a few milliseconds off the total time - e.g. from ~194ms to 169ms.



              filtering friends in dueByUsers()



              The filtering of array elements at key friends in dueByUsers() could be simplified using array_diff().



              I.E. this block:




              $users = array_filter($item['friends'], function ($friend) use ($item) {
              return $friend != $item['paid_by'];
              });



              Could be simplified like the following:



              $users = array_diff($item['friends'], [$item['paid_by']]);


              fetchValuesByKey()



              The method fetchValuesByKey() can be simplified to basically a call to array_column():



              protected function fetchValuesByKey($key)
              {
              return array_column($this->data, $key);
              }


              Or perhaps it would be simpler to eliminate that method and replace its usage with calls to array_column().



              Array_push()



              On the PHP documentation for array_push(), there is a paragraph in the description:




              Note: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function.




              So you might want to consider doing that in the settlement() method.



              Functional approaches



              Functional approaches, like using array_map(), array_reduce(), etc. could be employed, although in some places it would require extra work (e.g. employing the use keyword to reference variables outside closures)) and may be slower because a function is being called.



              For example, method settlement() could be re-written as:



              public function settlement()
              {

              $users = $this->users();
              $dueUsers = $this->dueByUsers();
              return array_reduce($users, function($settlements, $creditor) use($dueUsers) {
              $settlements[$creditor] = array_reduce(array_keys($dueUsers), function($creditorSettlements, $debtor) use ($creditor, $dueUsers) {
              $due = $dueUsers[$debtor];
              if ($creditor !== $debtor && $dueUsers[$creditor] <= $due) {
              $creditorSettlements = [
              'from' => $debtor,
              'amount' => $due - $dueUsers[$creditor]
              ];
              }
              return $creditorSettlements;
              }, );
              return $settlements;
              }, );





              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                I don't see very many improvements I would make, though I do see a couple places where native PHP functions could be used instead of manually iterating over arrays.



                These changes (in the first couple blocks) shaved a few milliseconds off the total time - e.g. from ~194ms to 169ms.



                filtering friends in dueByUsers()



                The filtering of array elements at key friends in dueByUsers() could be simplified using array_diff().



                I.E. this block:




                $users = array_filter($item['friends'], function ($friend) use ($item) {
                return $friend != $item['paid_by'];
                });



                Could be simplified like the following:



                $users = array_diff($item['friends'], [$item['paid_by']]);


                fetchValuesByKey()



                The method fetchValuesByKey() can be simplified to basically a call to array_column():



                protected function fetchValuesByKey($key)
                {
                return array_column($this->data, $key);
                }


                Or perhaps it would be simpler to eliminate that method and replace its usage with calls to array_column().



                Array_push()



                On the PHP documentation for array_push(), there is a paragraph in the description:




                Note: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function.




                So you might want to consider doing that in the settlement() method.



                Functional approaches



                Functional approaches, like using array_map(), array_reduce(), etc. could be employed, although in some places it would require extra work (e.g. employing the use keyword to reference variables outside closures)) and may be slower because a function is being called.



                For example, method settlement() could be re-written as:



                public function settlement()
                {

                $users = $this->users();
                $dueUsers = $this->dueByUsers();
                return array_reduce($users, function($settlements, $creditor) use($dueUsers) {
                $settlements[$creditor] = array_reduce(array_keys($dueUsers), function($creditorSettlements, $debtor) use ($creditor, $dueUsers) {
                $due = $dueUsers[$debtor];
                if ($creditor !== $debtor && $dueUsers[$creditor] <= $due) {
                $creditorSettlements = [
                'from' => $debtor,
                'amount' => $due - $dueUsers[$creditor]
                ];
                }
                return $creditorSettlements;
                }, );
                return $settlements;
                }, );





                share|improve this answer














                I don't see very many improvements I would make, though I do see a couple places where native PHP functions could be used instead of manually iterating over arrays.



                These changes (in the first couple blocks) shaved a few milliseconds off the total time - e.g. from ~194ms to 169ms.



                filtering friends in dueByUsers()



                The filtering of array elements at key friends in dueByUsers() could be simplified using array_diff().



                I.E. this block:




                $users = array_filter($item['friends'], function ($friend) use ($item) {
                return $friend != $item['paid_by'];
                });



                Could be simplified like the following:



                $users = array_diff($item['friends'], [$item['paid_by']]);


                fetchValuesByKey()



                The method fetchValuesByKey() can be simplified to basically a call to array_column():



                protected function fetchValuesByKey($key)
                {
                return array_column($this->data, $key);
                }


                Or perhaps it would be simpler to eliminate that method and replace its usage with calls to array_column().



                Array_push()



                On the PHP documentation for array_push(), there is a paragraph in the description:




                Note: If you use array_push() to add one element to the array it's better to use $array = because in that way there is no overhead of calling a function.




                So you might want to consider doing that in the settlement() method.



                Functional approaches



                Functional approaches, like using array_map(), array_reduce(), etc. could be employed, although in some places it would require extra work (e.g. employing the use keyword to reference variables outside closures)) and may be slower because a function is being called.



                For example, method settlement() could be re-written as:



                public function settlement()
                {

                $users = $this->users();
                $dueUsers = $this->dueByUsers();
                return array_reduce($users, function($settlements, $creditor) use($dueUsers) {
                $settlements[$creditor] = array_reduce(array_keys($dueUsers), function($creditorSettlements, $debtor) use ($creditor, $dueUsers) {
                $due = $dueUsers[$debtor];
                if ($creditor !== $debtor && $dueUsers[$creditor] <= $due) {
                $creditorSettlements = [
                'from' => $debtor,
                'amount' => $due - $dueUsers[$creditor]
                ];
                }
                return $creditorSettlements;
                }, );
                return $settlements;
                }, );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 29 at 20:01

























                answered Oct 1 '17 at 22:56









                Sᴀᴍ Onᴇᴌᴀ

                8,07461751




                8,07461751






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f176931%2fbill-settlement-for-multiple-users%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno