Skip to content

Commit

Permalink
updated readability
Browse files Browse the repository at this point in the history
  • Loading branch information
abmmhasan committed Mar 26, 2022
1 parent 6b2a73b commit a7f6d7b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 58 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "build"

on: [ "pull_request", "push" ]

jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ ubuntu-latest, windows-latest, macOS-latest ]
php-versions: [ '7.0','7.1','7.2','7.3','7.4','8.0', '8.1' ]
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Check PHP Version
run: php -v
36 changes: 0 additions & 36 deletions .github/workflows/php.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/vendor
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Game Draw

![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/abmmhasan/game-draw)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/abmmhasan/game-draw/build)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/56e7f49275dc4042b67d53b4209b193d)](https://www.codacy.com/gh/abmmhasan/Game-Draw/dashboard?utm_source=github.com&utm_medium=referral&utm_content=abmmhasan/Game-Draw&utm_campaign=Badge_Grade)
![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/abmmhasan/game-draw)
![Packagist Downloads](https://img.shields.io/packagist/dt/abmmhasan/game-draw)
![Packagist License](https://img.shields.io/packagist/l/abmmhasan/game-draw)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
![Packagist Version](https://img.shields.io/packagist/v/abmmhasan/game-draw)
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/abmmhasan/game-draw)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/abmmhasan/game-draw)
![Lines of code](https://img.shields.io/tokei/lines/github/abmmhasan/game-draw)


The Lucky Draw class takes an example array (explained below) and generates Item and Item count for winners.

The Mega Draw class takes 2 example array (explained below) and generates winners for each prize according to given amount per prize.
Expand Down
52 changes: 40 additions & 12 deletions src/Draw/LuckyDraw.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@
*/
class LuckyDraw
{
/**
* @param array $items , the list of items
* @param bool $fraction , If the chances/amounts include fraction number(true by default) or not(false)
* @param bool $check , If the items are already checked before can omit by passing false
* @return array with Item Code/Name and Item Counter
* @exception If required keys not present/values for the keys are not properly formatted
*/
private $items = [];
private $fraction;
private $check;
private $draw = [];


/**
* @param array $items the list of items
* @param bool $fraction If the chances/amounts include fraction number(true by default) or not(false)
* @param bool $check If the items are already checked before can omit by passing false
* @exception If required keys not present/values for the keys are not properly formatted
*/
public function __construct(array $items, bool $fraction = true, bool $check = true)
{
$this->init($items, $fraction, $check);
}

private function init(array $items, bool $fraction = true, bool $check = true)
{
if (count($items) < 1) {
throw new \LengthException('Invalid number of items!');
Expand All @@ -54,11 +59,17 @@ public function __construct(array $items, bool $fraction = true, bool $check = t
$this->gift($items);
}

public function draw()
/**
* @return array
*/
public function draw(): array
{
return $this->draw;
}

/**
* @return void
*/
private function multiply()
{
if (!$this->fraction) return;
Expand All @@ -70,7 +81,10 @@ private function multiply()
str_pad(1, $length, '0'))));
}

private function setFraction()
/**
* @return int
*/
private function setFraction(): int
{
$length = 0;
foreach ($this->items as $item) {
Expand All @@ -84,19 +98,30 @@ private function setFraction()
return (int)$length;
}

/**
* @return void
*/
private function getPositive()
{
$this->items = array_filter($this->items, function ($v) {
return $v > 0;
$this->items = array_filter($this->items, function ($value) {
return $value > 0;
});
}

private function numSequence($array)
/**
* @param $array
* @return bool
*/
private function numSequence($array): bool
{
if (!array_key_exists(0, $array)) return false;
return array_keys($array) === range(0, count($array) - 1);
}

/**
* @param $items
* @return void
*/
private function gift($items)
{
$this->items = array_column($items, 'chances', 'item');
Expand All @@ -113,6 +138,9 @@ private function gift($items)
$this->draw = [$item, $count];
}

/**
* @return false|int|mixed|string
*/
private function generate()
{
if (count($this->items) == 1) return current($this->items);
Expand Down
14 changes: 7 additions & 7 deletions src/Draw/MegaDraw.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MegaDraw
* @return array with Item Code/Name and Item Counter
* @exception If required keys not present/values for the keys are not properly formatted
*/
public function get(array $items, array $users, $grouped = false)
public function get(array $items, array $users, bool $grouped = false): array
{
if (count($items) < 1) {
throw new \LengthException('Invalid number of items!');
Expand All @@ -40,17 +40,17 @@ public function get(array $items, array $users, $grouped = false)
else return self::generalGift($items, $users);
}

private static function intValue(array $array)
private static function intValue(array $array): bool
{
return $array === array_filter($array, 'is_int');
}

private static function positiveValue(array $array)
private static function positiveValue(array $array): bool
{
return min($array) >= 0;
}

private static function generalGift($items, $users)
private static function generalGift($items, $users): array
{
$users = self::select($users, array_sum($items));
$select = array();
Expand All @@ -60,7 +60,7 @@ private static function generalGift($items, $users)
return $select;
}

private static function groupedGift($items, $users)
private static function groupedGift($items, $users): array
{
$users = self::groupBase($users);
$gift_array = [];
Expand All @@ -70,7 +70,7 @@ private static function groupedGift($items, $users)
return $gift_array;
}

private static function groupBase($users)
private static function groupBase($users): array
{
$group = array();
foreach ($users as $user => $gift) {
Expand All @@ -79,7 +79,7 @@ private static function groupBase($users)
return $group;
}

private static function select($users, $total)
private static function select($users, $total): array
{
$select = array();
for ($i = 0; $i < $total; $i++) {
Expand Down

0 comments on commit a7f6d7b

Please sign in to comment.