From a7f6d7b3d3511363f7a3c75b022551516999c237 Mon Sep 17 00:00:00 2001 From: abmmhasan Date: Sat, 26 Mar 2022 22:27:45 +0600 Subject: [PATCH] updated readability --- .github/workflows/ci.yml | 38 ++++++++++++++++++++++++++++ .github/workflows/php.yml | 36 --------------------------- .gitignore | 2 ++ README.md | 6 ++--- src/Draw/LuckyDraw.php | 52 ++++++++++++++++++++++++++++++--------- src/Draw/MegaDraw.php | 14 +++++------ 6 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/php.yml create mode 100644 .gitignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fb1f0e8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml deleted file mode 100644 index 65254b6..0000000 --- a/.github/workflows/php.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: PHP Composer - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - 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 - - # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" - # Docs: https://getcomposer.org/doc/articles/scripts.md - - # - name: Run test suite - # run: composer run-script test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45b1244 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +/vendor \ No newline at end of file diff --git a/README.md b/README.md index 3c3fd0d..6e192f5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Draw/LuckyDraw.php b/src/Draw/LuckyDraw.php index d0f14b9..96f51fa 100644 --- a/src/Draw/LuckyDraw.php +++ b/src/Draw/LuckyDraw.php @@ -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!'); @@ -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; @@ -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) { @@ -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'); @@ -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); diff --git a/src/Draw/MegaDraw.php b/src/Draw/MegaDraw.php index dfef2ad..69803f0 100644 --- a/src/Draw/MegaDraw.php +++ b/src/Draw/MegaDraw.php @@ -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!'); @@ -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(); @@ -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 = []; @@ -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) { @@ -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++) {