From e779ec02b49c9105eea7b0a25997ca5ab35ebb83 Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 13:58:22 +0545 Subject: [PATCH 01/11] add phpstan and update code for level 2 --- .github/workflows/ci.yml | 4 ++- composer.json | 3 +- composer.lock | 64 ++++++++++++++++++++++++++++++++++- src/Entities/BaseEntity.php | 4 +-- src/Entities/Category.php | 2 +- src/Entities/District.php | 2 +- src/Entities/Municipality.php | 2 +- 7 files changed, 73 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 57adc79..899ad13 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,4 +20,6 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run PHPUnit tests - run: vendor/bin/phpunit \ No newline at end of file + run: vendor/bin/phpunit + - name: Run PhpStan Analyse Code + run: vendor/bin/phpstan analyse --level=2 src tests \ No newline at end of file diff --git a/composer.json b/composer.json index 7088e3a..8a06b74 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": ">=8.0" }, "require-dev": { - "phpunit/phpunit": "9.6.*" + "phpunit/phpunit": "9.6.*", + "phpstan/phpstan": "^1.10" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 22f9c15..07bdf6d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a555d306f430af55c8dec5c2a59d0e87", + "content-hash": "65bafa18c224be4f0c81b54dce8f6379", "packages": [], "packages-dev": [ { @@ -312,6 +312,68 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.10.66", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "94779c987e4ebd620025d9e5fdd23323903950bd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/94779c987e4ebd620025d9e5fdd23323903950bd", + "reference": "94779c987e4ebd620025d9e5fdd23323903950bd", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2024-03-28T16:17:31+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.31", diff --git a/src/Entities/BaseEntity.php b/src/Entities/BaseEntity.php index 5cb04ac..c12fa04 100644 --- a/src/Entities/BaseEntity.php +++ b/src/Entities/BaseEntity.php @@ -7,12 +7,12 @@ abstract class BaseEntity { /** - * @var + * @var array */ protected $items; /** - * @var + * @var array */ protected $keys; diff --git a/src/Entities/Category.php b/src/Entities/Category.php index c6863fa..e0bdf73 100644 --- a/src/Entities/Category.php +++ b/src/Entities/Category.php @@ -58,7 +58,7 @@ public function find($id) * Find category by short code * * @param $short_code - * @return |null + * @return string|null */ public function findByShortCode($short_code) { diff --git a/src/Entities/District.php b/src/Entities/District.php index cb4185b..591af3f 100644 --- a/src/Entities/District.php +++ b/src/Entities/District.php @@ -83,7 +83,7 @@ public function getDistrictsByProvince($provinceId) * Find district by id * * @param $id - * @return false|int|string + * @return object|null */ public function find($id) { diff --git a/src/Entities/Municipality.php b/src/Entities/Municipality.php index b8d2124..bc5839b 100644 --- a/src/Entities/Municipality.php +++ b/src/Entities/Municipality.php @@ -91,7 +91,7 @@ public function getMunicipalityByProvince($provinceId) * Find municipality by id * * @param $id - * @return false|int|string + * @return object|null */ public function find($id) { From d4f56d4da10a4ed23a1daa6841b685daee2464c4 Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 14:03:12 +0545 Subject: [PATCH 02/11] set phpstan level to 3 and update code --- .github/workflows/ci.yml | 2 +- src/Entities/Category.php | 4 ++-- src/Entities/Province.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 899ad13..a486e28 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,4 +22,4 @@ jobs: - name: Run PHPUnit tests run: vendor/bin/phpunit - name: Run PhpStan Analyse Code - run: vendor/bin/phpstan analyse --level=2 src tests \ No newline at end of file + run: vendor/bin/phpstan analyse --level=3 src tests \ No newline at end of file diff --git a/src/Entities/Category.php b/src/Entities/Category.php index e0bdf73..38791ab 100644 --- a/src/Entities/Category.php +++ b/src/Entities/Category.php @@ -45,7 +45,7 @@ public function allCategories() * Find category by id * * @param $id - * @return false|int|string + * @return object|null */ public function find($id) { @@ -58,7 +58,7 @@ public function find($id) * Find category by short code * * @param $short_code - * @return string|null + * @return object|null */ public function findByShortCode($short_code) { diff --git a/src/Entities/Province.php b/src/Entities/Province.php index 112769d..2fea09c 100644 --- a/src/Entities/Province.php +++ b/src/Entities/Province.php @@ -45,7 +45,7 @@ public function allProvinces() * Find province by id * * @param $id - * @return false|int|string + * @return object|null */ public function find($id) { From d5b5a9e6966e957712863873b3f802f2b86a2a16 Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 14:16:21 +0545 Subject: [PATCH 03/11] update code and set phpstan level to 4 --- .github/workflows/ci.yml | 2 +- tests/Unit/CategoryTest.php | 7 ++----- tests/Unit/DistrictTest.php | 7 ++----- tests/Unit/MunicipalityTest.php | 9 ++------- tests/Unit/ProvinceTest.php | 6 ++---- 5 files changed, 9 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a486e28..8ff0455 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,4 +22,4 @@ jobs: - name: Run PHPUnit tests run: vendor/bin/phpunit - name: Run PhpStan Analyse Code - run: vendor/bin/phpstan analyse --level=3 src tests \ No newline at end of file + run: vendor/bin/phpstan analyse --level=4 src tests \ No newline at end of file diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php index 28f7932..e44ddca 100644 --- a/tests/Unit/CategoryTest.php +++ b/tests/Unit/CategoryTest.php @@ -86,15 +86,12 @@ public function test_findBy_for_each_short_code() public function test_allCategories_for_null_values() { - $hasNull = false; foreach ($this->category->allCategories() as $set) { - if ($hasNull = in_array(null, (array) $set, true)) { + if (in_array(null, (array) $set, true)) { $this->fail('Category dataset can\'t have null values'); - break; } } - - $this->assertTrue(!$hasNull); + $this->assertTrue(true); } public function test_allCategories_for_count() diff --git a/tests/Unit/DistrictTest.php b/tests/Unit/DistrictTest.php index ce0ed79..00e4c03 100644 --- a/tests/Unit/DistrictTest.php +++ b/tests/Unit/DistrictTest.php @@ -81,15 +81,13 @@ public function test_allDistricts_for_count() public function test_allDistricts_for_null_values() { - $hasNull = false; foreach ($this->district->allDistricts() as $set) { - if ($hasNull = in_array(null, (array) $set, true)) { + if (in_array(null, (array) $set, true)) { $this->fail('District dataset can\'t have null values'); - break; } } - $this->assertTrue(!$hasNull); + $this->assertTrue(true); } public function test_district_for_correct_province_id() @@ -103,7 +101,6 @@ public function test_district_for_correct_province_id() if($district && !in_array($district->province_id, range(1,7))){ $correct = false; $this->fail('Invalid Province for District'); - break; } } diff --git a/tests/Unit/MunicipalityTest.php b/tests/Unit/MunicipalityTest.php index 3f44d85..ae68895 100644 --- a/tests/Unit/MunicipalityTest.php +++ b/tests/Unit/MunicipalityTest.php @@ -83,15 +83,13 @@ public function test_allMunicipalities_count() public function test_allMunicipalities_for_null_values() { - $hasNull = false; foreach ($this->municipality->allMunicipalities() as $set) { - if ($hasNull = in_array(null, (array) $set, true)) { + if (in_array(null, (array) $set, true)) { $this->fail('Municipality dataset can\'t have null values'); - break; } } - $this->assertTrue(!$hasNull); + $this->assertTrue(true); } public function test_if_municipality_has_wards_in_range_of_5_to_33() @@ -114,7 +112,6 @@ public function test_if_municipality_has_wards_in_range_of_5_to_33() { $correct = false; $this->fail('Invalid Wards for Municipality'); - break; } } @@ -132,7 +129,6 @@ public function test_if_municipality_has_correct_category_id() if($municipality && !in_array($municipality->category_id, range(1,4))){ $correct = false; $this->fail('Invalid Category for Municipality'); - break; } } @@ -150,7 +146,6 @@ public function test_if_municipality_has_correct_district_id() if($municipality && !in_array($municipality->district_id, range(1,77))){ $correct = false; $this->fail('Invalid District for Municipality'); - break; } } diff --git a/tests/Unit/ProvinceTest.php b/tests/Unit/ProvinceTest.php index f679132..4fa3c79 100644 --- a/tests/Unit/ProvinceTest.php +++ b/tests/Unit/ProvinceTest.php @@ -84,15 +84,13 @@ public function test_allProvinces_count() public function test_allProvinces_for_null_values() { - $hasNull = false; foreach ($this->province->allProvinces() as $set) { - if ($hasNull = in_array(null, (array) $set, true)) { + if (in_array(null, (array) $set, true)) { $this->fail('Province dataset can\'t have null values'); - break; } } - $this->assertTrue(!$hasNull); + $this->assertTrue(true); } public function test_search() From c443f954febbf7bfef389c097c31bc1c846b74dc Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 21:11:05 +0545 Subject: [PATCH 04/11] lock code quality at level 5 for tests and set 6 for src --- .github/workflows/ci.yml | 6 +++++- src/Entities/BaseEntity.php | 30 +++++++++++++--------------- src/Entities/Category.php | 10 +++++----- src/Entities/District.php | 18 ++++++++--------- src/Entities/Municipality.php | 30 +++++++++++++++++----------- src/Entities/Province.php | 21 +++++++++---------- src/Helpers/Helper.php | 16 +++++++-------- src/Loaders/CategoriesLoader.php | 2 +- src/Loaders/DistrictsLoader.php | 2 +- src/Loaders/MunicipalitiesLoader.php | 2 +- src/Loaders/ProvinceLoader.php | 2 +- tests/Unit/MunicipalityTest.php | 2 +- 12 files changed, 75 insertions(+), 66 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8ff0455..a40def2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,5 +21,9 @@ jobs: - name: Run PHPUnit tests run: vendor/bin/phpunit + + - name: Run PhpStan Analyse Code + run: vendor/bin/phpstan analyse --level=6 src + - name: Run PhpStan Analyse Code - run: vendor/bin/phpstan analyse --level=4 src tests \ No newline at end of file + run: vendor/bin/phpstan analyse --level=5 tests \ No newline at end of file diff --git a/src/Entities/BaseEntity.php b/src/Entities/BaseEntity.php index c12fa04..1346dc0 100644 --- a/src/Entities/BaseEntity.php +++ b/src/Entities/BaseEntity.php @@ -1,9 +1,7 @@ $data * @param bool $exact - * @return array + * @return array */ - protected function filter($key, $value, $data, $exact = false) + protected function filter($key, $value, $data, $exact = false): array { return array_values(array_filter($data, function ($item) use ($key, $value, $exact) { return ($exact ? ($item->$key == $value ? true:false) :(is_int(strpos($item->$key, $value)) ? true:false)); @@ -42,15 +40,15 @@ protected function filter($key, $value, $data, $exact = false) * * @return string */ - public function getLanguage() + public function getLanguage(): string { return $this->lang; } /** - * @return mixed + * @return array */ - public function getKeys() + public function getKeys(): array { return $this->keys; } @@ -58,11 +56,11 @@ public function getKeys() /** * Recursive Search Data * - * @param $params - * @param array $items - * @return array|mixed + * @param array $params + * @param array $items + * @return array */ - public function recursiveSearch($params, $items = []) + public function recursiveSearch($params, $items = []): array { $items = $items ? $items : $this->items; @@ -78,9 +76,9 @@ public function recursiveSearch($params, $items = []) * * @param string $key * @param int $order - * @return mixed + * @return array */ - public function sortBy($key = 'name', $order = SORT_ASC) + public function sortBy($key = 'name', $order = SORT_ASC): array { $keys = array_column($this->items, $key); diff --git a/src/Entities/Category.php b/src/Entities/Category.php index 38791ab..14add63 100644 --- a/src/Entities/Category.php +++ b/src/Entities/Category.php @@ -44,7 +44,7 @@ public function allCategories() /** * Find category by id * - * @param $id + * @param int $id * @return object|null */ public function find($id) @@ -57,7 +57,7 @@ public function find($id) /** * Find category by short code * - * @param $short_code + * @param string $short_code * @return object|null */ public function findByShortCode($short_code) @@ -70,10 +70,10 @@ public function findByShortCode($short_code) /** * Search Categories * - * @param $key - * @param $value + * @param string $key + * @param string $value * @param bool $exact - * @return array + * @return array */ public function search($key, $value, $exact = false) { diff --git a/src/Entities/District.php b/src/Entities/District.php index 591af3f..4b34d15 100644 --- a/src/Entities/District.php +++ b/src/Entities/District.php @@ -45,7 +45,7 @@ public function allDistricts() /** * Get districts with municipalities * - * @return array + * @return array * @throws LoadingException */ public function getDistrictsWithMunicipalities() @@ -69,8 +69,8 @@ public function getDistrictsWithMunicipalities() /** * Get districts by province id * - * @param $provinceId - * @return array|mixed|null + * @param int $provinceId + * @return array|null */ public function getDistrictsByProvince($provinceId) { @@ -82,7 +82,7 @@ public function getDistrictsByProvince($provinceId) /** * Find district by id * - * @param $id + * @param int $id * @return object|null */ public function find($id) @@ -95,7 +95,7 @@ public function find($id) /** * Get district with largest area * - * @return mixed + * @return object */ public function largest() { @@ -113,7 +113,7 @@ public function largest() /** * Get district with smallest area * - * @return mixed + * @return object */ public function smallest() { @@ -131,10 +131,10 @@ public function smallest() /** * Search Districts * - * @param $key - * @param $value + * @param string $key + * @param string $value * @param bool $exact - * @return array + * @return array */ public function search($key, $value, $exact = false) { diff --git a/src/Entities/Municipality.php b/src/Entities/Municipality.php index bc5839b..cff2102 100644 --- a/src/Entities/Municipality.php +++ b/src/Entities/Municipality.php @@ -53,8 +53,8 @@ public function allMunicipalities() /** * Get municipalities by district id * - * @param $districtId - * @return array|mixed|null + * @param int $districtId + * @return array|null */ public function getMunicipalitiesByDistrict($districtId) { @@ -66,8 +66,8 @@ public function getMunicipalitiesByDistrict($districtId) /** * Get municipalities by category id * - * @param $categoryId - * @return array + * @param int $categoryId + * @return array */ public function getMunicipalityByCategory($categoryId) { @@ -76,6 +76,12 @@ public function getMunicipalityByCategory($categoryId) })); } + /** + * Get municipalities by province + * + * @param int $provinceId + * @return array + */ public function getMunicipalityByProvince($provinceId) { $district = new District(); @@ -90,7 +96,7 @@ public function getMunicipalityByProvince($provinceId) /** * Find municipality by id * - * @param $id + * @param int $id * @return object|null */ public function find($id) @@ -103,7 +109,7 @@ public function find($id) /** * Get municipality with largest area * - * @return mixed + * @return object */ public function largest() { @@ -121,7 +127,7 @@ public function largest() /** * Get municipality with smallest area * - * @return mixed + * @return object */ public function smallest() { @@ -139,8 +145,8 @@ public function smallest() /** * Get wards of municipality * - * @param $id - * @return array + * @param int $id + * @return array */ public function wards($id) { @@ -163,10 +169,10 @@ public function wards($id) /** * Search Municipalities * - * @param $key - * @param $value + * @param string $key + * @param string $value * @param bool $exact - * @return array + * @return array */ public function search($key, $value, $exact = false) { diff --git a/src/Entities/Province.php b/src/Entities/Province.php index 2fea09c..5179676 100644 --- a/src/Entities/Province.php +++ b/src/Entities/Province.php @@ -14,7 +14,7 @@ class Province extends BaseEntity { /** * Province constructor. - * @param $lang + * @param string $lang * @throws LoadingException */ public function __construct($lang = 'en') @@ -34,7 +34,7 @@ public function __construct($lang = 'en') /** * Get list of all provinces - * @return mixed|null + * @return array */ public function allProvinces() { @@ -44,7 +44,7 @@ public function allProvinces() /** * Find province by id * - * @param $id + * @param int $id * @return object|null */ public function find($id) @@ -56,7 +56,8 @@ public function find($id) /** * Get province with largest area - * @return mixed + * + * @return object */ public function largest() { @@ -74,7 +75,7 @@ public function largest() /** * Get province with smallest area * - * @return mixed + * @return object */ public function smallest() { @@ -92,7 +93,7 @@ public function smallest() /** * Get provinces with districts * - * @return array + * @return array * @throws LoadingException */ public function getProvincesWithDistricts() @@ -111,7 +112,7 @@ public function getProvincesWithDistricts() /** * Get provinces with districts with municipalities * - * @return array + * @return array * @throws LoadingException */ public function getProvincesWithDistrictsWithMunicipalities() @@ -142,10 +143,10 @@ public function getProvincesWithDistrictsWithMunicipalities() /** * Search Provinces * - * @param $key - * @param $value + * @param string $key + * @param string $value * @param bool $exact - * @return array + * @return array */ public function search($key, $value, $exact = false) { diff --git a/src/Helpers/Helper.php b/src/Helpers/Helper.php index 0d15c0f..3a595f3 100644 --- a/src/Helpers/Helper.php +++ b/src/Helpers/Helper.php @@ -10,22 +10,22 @@ class Helper { /** - * @var array + * @var array */ private static $nepali = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']; /** - * @var array + * @var array */ private static $english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; /** * Convert english numeric value to nepali numeric value * - * @param $value - * @return mixed + * @param string $value + * @return string */ - public static function numericNepali($value) + public static function numericNepali($value): string { for ($i = 0; $i < 10; $i++) { @@ -38,10 +38,10 @@ public static function numericNepali($value) /** * Convert nepali numeric value to english numeric value * - * @param $value - * @return mixed + * @param string $value + * @return string */ - public static function numericEnglish($value) + public static function numericEnglish($value): string { for ($i = 0; $i < 10; $i++) { diff --git a/src/Loaders/CategoriesLoader.php b/src/Loaders/CategoriesLoader.php index a98c99e..73fa41b 100644 --- a/src/Loaders/CategoriesLoader.php +++ b/src/Loaders/CategoriesLoader.php @@ -19,7 +19,7 @@ class CategoriesLoader /** * CategoriesLoader constructor. - * @param $lang + * @param string $lang * @throws LoadingException */ public function __construct($lang = 'en') diff --git a/src/Loaders/DistrictsLoader.php b/src/Loaders/DistrictsLoader.php index c871ca7..28b246f 100644 --- a/src/Loaders/DistrictsLoader.php +++ b/src/Loaders/DistrictsLoader.php @@ -19,7 +19,7 @@ class DistrictsLoader /** * DistrictsLoader constructor. - * @param $lang + * @param string $lang * @throws LoadingException */ public function __construct($lang = 'en') diff --git a/src/Loaders/MunicipalitiesLoader.php b/src/Loaders/MunicipalitiesLoader.php index 51008c8..c09303e 100644 --- a/src/Loaders/MunicipalitiesLoader.php +++ b/src/Loaders/MunicipalitiesLoader.php @@ -18,7 +18,7 @@ class MunicipalitiesLoader /** * MunicipalitiesLoader constructor. - * @param $lang + * @param string $lang * @throws LoadingException */ public function __construct($lang = 'en') diff --git a/src/Loaders/ProvinceLoader.php b/src/Loaders/ProvinceLoader.php index 5950065..badb1f4 100644 --- a/src/Loaders/ProvinceLoader.php +++ b/src/Loaders/ProvinceLoader.php @@ -17,7 +17,7 @@ class ProvinceLoader /** * ProvinceLoader constructor. - * @param $lang + * @param string $lang * @throws LoadingException */ public function __construct($lang = 'en') diff --git a/tests/Unit/MunicipalityTest.php b/tests/Unit/MunicipalityTest.php index ae68895..d3c3656 100644 --- a/tests/Unit/MunicipalityTest.php +++ b/tests/Unit/MunicipalityTest.php @@ -98,7 +98,7 @@ public function test_if_municipality_has_wards_in_range_of_5_to_33() if($lang == 'np'){ $wards = range(1,33); $wards = array_map(function ($item){ - return \Sagautam5\LocalStateNepal\Helpers\Helper::numericNepali($item); + return \Sagautam5\LocalStateNepal\Helpers\Helper::numericNepali((string) $item); }, $wards); }else{ $wards = range(1,33); From c8172cc8d9fc03f053910325e729ca3e1fcc846e Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 21:36:18 +0545 Subject: [PATCH 05/11] set phpstan static check to 7 and refactor --- src/Entities/District.php | 2 +- src/Entities/Municipality.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Entities/District.php b/src/Entities/District.php index 4b34d15..ddcc1f8 100644 --- a/src/Entities/District.php +++ b/src/Entities/District.php @@ -75,7 +75,7 @@ public function getDistrictsWithMunicipalities() public function getDistrictsByProvince($provinceId) { return array_values(array_filter($this->items, function ($item) use ($provinceId) { - return ($item->province_id == $provinceId); + return property_exists($item, 'province_id') && ($item->province_id == $provinceId); })); } diff --git a/src/Entities/Municipality.php b/src/Entities/Municipality.php index cff2102..c00b8ad 100644 --- a/src/Entities/Municipality.php +++ b/src/Entities/Municipality.php @@ -59,7 +59,7 @@ public function allMunicipalities() public function getMunicipalitiesByDistrict($districtId) { return array_values(array_filter($this->items, function ($item) use ($districtId) { - return ($item->district_id == $districtId); + return property_exists($item, 'district_id') && ($item->district_id == $districtId); })); } @@ -72,7 +72,7 @@ public function getMunicipalitiesByDistrict($districtId) public function getMunicipalityByCategory($categoryId) { return array_values(array_filter($this->items, function ($item) use ($categoryId) { - return ($item->category_id == $categoryId); + return property_exists($item, 'category_id') && ($item->category_id == $categoryId); })); } @@ -87,7 +87,7 @@ public function getMunicipalityByProvince($provinceId) $district = new District(); $districts = $district->getDistrictsByProvince($provinceId); $municipalities = array_map(function ($item) { - return $this->getMunicipalitiesByDistrict($item->id); + return $this->getMunicipalitiesByDistrict($item->id ?? 0); }, $districts); return array_merge(...$municipalities); @@ -154,14 +154,14 @@ public function wards($id) if($this->lang == 'np'){ - $totalWards = Helper::numericEnglish($municipality->wards); + $totalWards = Helper::numericEnglish($municipality->wards ?? 1); $wards = range(1, $totalWards); $wards = array_map(function ($item){ return Helper::numericNepali($item); }, $wards); }else{ - $wards = range(1, $municipality->wards); + $wards = range(1, $municipality->wards ?? 1); } return $wards; } From 9d7fdb546a72524db2a5996cdbab55566e21fcdf Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 21:57:46 +0545 Subject: [PATCH 06/11] set static type check to 8 and refactor --- .github/workflows/ci.yml | 2 +- src/Entities/District.php | 2 +- src/Entities/Municipality.php | 8 ++++++-- src/Entities/Province.php | 18 +++++++++--------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a40def2..ffd862a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: run: vendor/bin/phpunit - name: Run PhpStan Analyse Code - run: vendor/bin/phpstan analyse --level=6 src + run: vendor/bin/phpstan analyse --level=7 src - name: Run PhpStan Analyse Code run: vendor/bin/phpstan analyse --level=5 tests \ No newline at end of file diff --git a/src/Entities/District.php b/src/Entities/District.php index ddcc1f8..ba072d2 100644 --- a/src/Entities/District.php +++ b/src/Entities/District.php @@ -61,7 +61,7 @@ public function getDistrictsWithMunicipalities() $municipalityItem = (array) $municipalityItem; $municipalityItem['wards'] = $municipality->wards($municipalityItem['id']); return (object) $municipalityItem; - }, $municipalities); + }, (array) $municipalities); return (object) $districtItem; },$provinces); } diff --git a/src/Entities/Municipality.php b/src/Entities/Municipality.php index c00b8ad..2151300 100644 --- a/src/Entities/Municipality.php +++ b/src/Entities/Municipality.php @@ -88,8 +88,12 @@ public function getMunicipalityByProvince($provinceId) $districts = $district->getDistrictsByProvince($provinceId); $municipalities = array_map(function ($item) { return $this->getMunicipalitiesByDistrict($item->id ?? 0); - }, $districts); - + }, (array) $districts); + + $municipalities = array_filter($municipalities, function ($value) { + return $value !== null; + }); + return array_merge(...$municipalities); } diff --git a/src/Entities/Province.php b/src/Entities/Province.php index 5179676..3988db6 100644 --- a/src/Entities/Province.php +++ b/src/Entities/Province.php @@ -123,21 +123,21 @@ public function getProvincesWithDistrictsWithMunicipalities() $provinces = $this->allProvinces(); return array_map(function ($provinceItem) use ($district, $municipality) { - $provinceItem = (array)$provinceItem; + $provinceItem = (array) $provinceItem; $provinceDistricts = $district->getDistrictsByProvince($provinceItem['id']); $provinceItem['districts'] = array_map(function ($districtItem) use ($municipality) { - $districtItem = (array)$districtItem; + $districtItem = (array) $districtItem; $municipalities = $municipality->getMunicipalitiesByDistrict($districtItem['id']); $districtItem['municipalities'] = array_map(function ($municipalityItem) use ($municipality) { - $municipalityItem = (array)$municipalityItem; + $municipalityItem = (array) $municipalityItem; $municipalityItem['wards'] = $municipality->wards($municipalityItem['id']); - return (object)$municipalityItem; - }, $municipalities); + return (object) $municipalityItem; + }, (array) $municipalities); - return (object)$districtItem; - }, $provinceDistricts); - return (object)$provinceItem; - }, $provinces); + return (object) $districtItem; + }, (array) $provinceDistricts); + return (object) $provinceItem; + }, (array) $provinces); } /** From 7bdce827c57e1cdf4ed9618c2d66331c3e98974c Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 7 Apr 2024 21:58:18 +0545 Subject: [PATCH 07/11] update pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ffd862a..bcdcb73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: run: vendor/bin/phpunit - name: Run PhpStan Analyse Code - run: vendor/bin/phpstan analyse --level=7 src + run: vendor/bin/phpstan analyse --level=8 src - name: Run PhpStan Analyse Code run: vendor/bin/phpstan analyse --level=5 tests \ No newline at end of file From b418a6b6df67ae0b55de9f07e9ee8fcf09a57ee0 Mon Sep 17 00:00:00 2001 From: AsmitNepali Date: Sun, 28 Apr 2024 10:59:51 +0545 Subject: [PATCH 08/11] ADD: Add local setup with docker docs. --- docs/docker/setup.md | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 docs/docker/setup.md diff --git a/docs/docker/setup.md b/docs/docker/setup.md new file mode 100644 index 0000000..6ed5d9e --- /dev/null +++ b/docs/docker/setup.md @@ -0,0 +1,51 @@ +## Local setup with docker + +In order to make contribution and bug reporting easier, we have recently dockerized this package so that contributors can setup, run tests and view demo locally in just few steps. +You can clone this repository in your computer. Assuming you have docker desktop configured in your computer, you just have to run following command. + + +If docker desktop is not installed in your computer, You can setup docker desktop from official docs [Docker Desktop](https://docs.docker.com/desktop/) + + + docker compose up -d + + +On successful execution of above command, you will have a docker container `localstates` and `nginx` configuration too. You can access demo by visiting following url + +[localstates.test](http://localstates.test) + +With following command, we can enter to our container. + + docker compose exec localstates bash + +In order to verify contribution align with php unit tests and php stan standards, you have to execute following commands + +vendor/bin/phpunit + +vendor/bin/phpstan analyse --level=9 + +## Docker Images + +We have uploaded docker images for various version of the php. If you find bug related to specific version of the php, then you can report the bug or contribute on bug +fixing by using respective docker image in your setup. + +In the docker file, you can update following line with specific docker image. + + FROM sagautam5/localstates_8.3:v1.0 + +As you can see, Above image is for php 8.3. We have the following docker images for respective php versions. + +- [sagautam5/localstates_7.1:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.1) +- [sagautam5/localstates_7.2:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.2) +- [sagautam5/localstates_7.3:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.3) +- [sagautam5/localstates_7.4:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.4) +- [sagautam5/localstates_8.0:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.0) +- [sagautam5/localstates_8.1:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.1) +- [sagautam5/localstates_8.2:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.2) +- [sagautam5/localstates_8.3:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.3) + +After updating docker file, you have recreate the container with following command. + + docker compose up -d --force-recreate + +Now, it's ready with specific version of php \ No newline at end of file From 9ce23f9b0808749f4da2dfda210d3edde7e2b961 Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 28 Apr 2024 12:07:15 +0545 Subject: [PATCH 09/11] update docs and add contribution guideline --- CONTRIBUTING.md | 19 +++++++++++++++---- docs/docker/setup.md | 20 ++++++++++---------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f8e0173..1300aac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,20 +34,31 @@ Before filing an issue: Before submitting a pull request: - Fetch the phpunit testing dependency by executing following command on root of the project. ```sh -composer update -```- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. + composer install +``` +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. - Create and test the feature/issue and make sure it doesn't affect codebase by running full tests by executing following console command for two different environment variables. Set APP_LANG to np and en respectively and run following command for each value of language. ```sh -vendor/bin/phpunit + vendor/bin/phpunit +``` +- Make sure contribution align with phpstan standards. You can run following command to ensure this. +```sh + vendor/bin/phpunit analyse --level=9 ``` - Check the codebase to ensure that your feature doesn't already exist. - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. +## Docker Option + +We have recently added workflow to setup this package with Docker Desktop. You can find guidelines in the following document. + +- [Local setup with Docker](https://github.com/sagautam5/local-states-nepal/tree/master/docs/docker/setup.md) + ## Requirements If the project maintainer has any additional requirements, you will find them listed here. @@ -61,4 +72,4 @@ If the project maintainer has any additional requirements, you will find them li - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. -**Happy coding**! +**Happy coding** ! diff --git a/docs/docker/setup.md b/docs/docker/setup.md index 6ed5d9e..e2168a0 100644 --- a/docs/docker/setup.md +++ b/docs/docker/setup.md @@ -1,20 +1,20 @@ -## Local setup with docker +## Local setup with Docker -In order to make contribution and bug reporting easier, we have recently dockerized this package so that contributors can setup, run tests and view demo locally in just few steps. -You can clone this repository in your computer. Assuming you have docker desktop configured in your computer, you just have to run following command. +In order to make contribution and bug reporting easier, we have recently Dockerized this package so that contributors can setup, run tests and view demo locally in just a few steps. +You can clone this repository on your computer. Assuming you have docker desktop configured in your computer, you just have to run the following command: -If docker desktop is not installed in your computer, You can setup docker desktop from official docs [Docker Desktop](https://docs.docker.com/desktop/) +If Docker Desktop is not installed on your computer, you can setup Docker Desktop from th official docs [Docker Desktop](https://docs.docker.com/desktop/) docker compose up -d -On successful execution of above command, you will have a docker container `localstates` and `nginx` configuration too. You can access demo by visiting following url +On successful execution of the above command, you will have a Docker container `localstates` and `nginx` configuration too. You can access demo by visiting the following url [localstates.test](http://localstates.test) -With following command, we can enter to our container. +With the following command, we can enter our container. docker compose exec localstates bash @@ -29,11 +29,11 @@ vendor/bin/phpstan analyse --level=9 We have uploaded docker images for various version of the php. If you find bug related to specific version of the php, then you can report the bug or contribute on bug fixing by using respective docker image in your setup. -In the docker file, you can update following line with specific docker image. +In the `Dockerfile`, you can update the following line with an specific Docker image. FROM sagautam5/localstates_8.3:v1.0 -As you can see, Above image is for php 8.3. We have the following docker images for respective php versions. +As you can see,the above image is for PHP 8.3. We have the following Docker images for the respective PHP versions. - [sagautam5/localstates_7.1:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.1) - [sagautam5/localstates_7.2:v1.0](https://hub.docker.com/r/sagautam5/localstates_7.2) @@ -44,8 +44,8 @@ As you can see, Above image is for php 8.3. We have the following docker images - [sagautam5/localstates_8.2:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.2) - [sagautam5/localstates_8.3:v1.0](https://hub.docker.com/r/sagautam5/localstates_8.3) -After updating docker file, you have recreate the container with following command. +After updating `Dockerfile`, you have to recreate the container with the following command: docker compose up -d --force-recreate -Now, it's ready with specific version of php \ No newline at end of file +Now, it's ready with an specific version of PHP \ No newline at end of file From fb2ee1fbdc24a0f2d6f0ab5a76917ff6103288ca Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 28 Apr 2024 12:19:09 +0545 Subject: [PATCH 10/11] update docs --- CONTRIBUTING.md | 4 ++-- Dockerfile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1300aac..5157a2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,7 +46,7 @@ Before submitting a pull request: ``` - Make sure contribution align with phpstan standards. You can run following command to ensure this. ```sh - vendor/bin/phpunit analyse --level=9 + vendor/bin/phpunit analyse --level=8 src/ ``` - Check the codebase to ensure that your feature doesn't already exist. @@ -57,7 +57,7 @@ Before submitting a pull request: We have recently added workflow to setup this package with Docker Desktop. You can find guidelines in the following document. -- [Local setup with Docker](https://github.com/sagautam5/local-states-nepal/tree/master/docs/docker/setup.md) +- [Local setup with Docker](./docs/docker/setup.md) ## Requirements diff --git a/Dockerfile b/Dockerfile index 23e6a38..5181c22 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1 +1 @@ -FROM sagautam5/localstates_7.4:v1.0 \ No newline at end of file +FROM sagautam5/localstates_8.3:v1.0 \ No newline at end of file From 49896506433bfe3a3be2f9ba35a70fda9d26fa50 Mon Sep 17 00:00:00 2001 From: sagautam5 Date: Sun, 28 Apr 2024 12:54:57 +0545 Subject: [PATCH 11/11] update docs --- CONTRIBUTING.md | 2 ++ README.md | 6 +++++- docs/docker/setup.md | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5157a2e..094e1e4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -47,6 +47,8 @@ Before submitting a pull request: - Make sure contribution align with phpstan standards. You can run following command to ensure this. ```sh vendor/bin/phpunit analyse --level=8 src/ + vendor/bin/phpunit analyse --level=5 tests/ + ``` - Check the codebase to ensure that your feature doesn't already exist. diff --git a/README.md b/README.md index bf70f07..c4dbce7 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Image Source: [Wikipedia(Administrative divisions of Nepal)](https://simple.wiki - 7.3 - 7.4 - Current **dev-master** has been tested on following php versions. + Current **v2.0.0** has been tested on following php versions. - 8.0 - 8.1 @@ -60,7 +60,11 @@ Example: http://127.0.0.1/local-states-nepal/demo Demo Video: https://www.loom.com/share/a8c3d825a9ee41a9817b24645453e979 + +Additionally, you can setup with Docker and visit [localstates.test](http://localstates.test) to view the demo. Here is guideline for setting up with Docker. + [Local setup with Docker](./docs/docker/setup.md) + ## Support Currently, This library supports following two languages. diff --git a/docs/docker/setup.md b/docs/docker/setup.md index e2168a0..9690f46 100644 --- a/docs/docker/setup.md +++ b/docs/docker/setup.md @@ -22,7 +22,9 @@ In order to verify contribution align with php unit tests and php stan standards vendor/bin/phpunit -vendor/bin/phpstan analyse --level=9 +vendor/bin/phpstan analyse --level=8 src/ + +vendor/bin/phpstan analyse --level=5 tests/ ## Docker Images