From b985ea09fa63e3cd1580b03bb78d46911dd80d81 Mon Sep 17 00:00:00 2001 From: Prasit Gebsaap Date: Sat, 28 Sep 2024 22:14:55 +0700 Subject: [PATCH] Fix PHPStan errors --- .github/workflows/run-tests.yml | 2 +- composer.json | 8 +- .../migrations/create_addresses_table.php | 7 +- .../create_thai_districts_table.php | 6 +- .../create_thai_geographies_table.php | 8 +- .../create_thai_provinces_table.php | 6 +- .../create_thai_subdistricts_table.php | 8 +- phpstan.neon.dist | 3 +- src/Facades/ThaiAddresses.php | 8 +- src/Models/Address.php | 11 +-- src/Models/District.php | 12 +-- src/Models/Geography.php | 9 +- src/Models/Province.php | 12 +-- src/Models/Subdistrict.php | 12 +-- src/ThaiAddresses.php | 85 +++++-------------- src/ThaiAddressesServiceProvider.php | 11 --- src/Traits/HasAddress.php | 7 +- .../migrations/create_users_table.php | 3 +- tests/src/Feature/UserTest.php | 2 +- 19 files changed, 81 insertions(+), 139 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3388797..c48feb0 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -19,7 +19,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.2] + php: [8.2, 8.3] laravel: [10.*, 11.*] stability: [prefer-stable] include: diff --git a/composer.json b/composer.json index 4260fed..cb0334e 100644 --- a/composer.json +++ b/composer.json @@ -20,23 +20,23 @@ } ], "require": { - "php": "^8.1|^8.2", + "php": "^8.2|^8.3", "illuminate/contracts": "^10.0|^11.0", "illuminate/support": "^10.0|^11.0", "laravel/pint": "^1.17", "spatie/laravel-package-tools": "^1.9.2" }, "require-dev": { - "nunomaduro/collision": "^6.0|^7.0", "larastan/larastan": "^2.0.1", + "nunomaduro/collision": "^6.0|^7.0", "orangehill/iseed": "^3.0", - "orchestra/testbench": "^8.0|^9.0", + "orchestra/testbench": "*", "pestphp/pest": "^1.21", "pestphp/pest-plugin-laravel": "^1.1", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.5|^10", "spatie/laravel-ray": "^1.26" }, "autoload": { diff --git a/database/migrations/create_addresses_table.php b/database/migrations/create_addresses_table.php index fe3d336..018223c 100644 --- a/database/migrations/create_addresses_table.php +++ b/database/migrations/create_addresses_table.php @@ -9,8 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getAddressTableName(), function (Blueprint $table) { - // Columns + $table = ThaiAddresses::getAddressTableName(); + Schema::create($table, function (Blueprint $table) { $table->id('id'); $table->morphs('addressable'); @@ -36,6 +36,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getAddressTableName()); + $table = ThaiAddresses::getAddressTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_districts_table.php b/database/migrations/create_thai_districts_table.php index 629a84c..ca415d3 100644 --- a/database/migrations/create_thai_districts_table.php +++ b/database/migrations/create_thai_districts_table.php @@ -9,7 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getDistrictTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getDistrictTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); // add fields @@ -24,6 +25,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getDistrictTableName()); + $table = ThaiAddresses::getDistrictTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_geographies_table.php b/database/migrations/create_thai_geographies_table.php index c8c9d8b..34e88db 100644 --- a/database/migrations/create_thai_geographies_table.php +++ b/database/migrations/create_thai_geographies_table.php @@ -9,11 +9,10 @@ { public function up() { - Schema::create(ThaiAddresses::getGeographyTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getGeographyTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); - // add fields - $table->string('name_en'); $table->string('name_th'); $table->timestamps(); @@ -22,6 +21,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getGeographyTableName()); + $table = ThaiAddresses::getGeographyTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_provinces_table.php b/database/migrations/create_thai_provinces_table.php index 97fc49e..f95f3a2 100644 --- a/database/migrations/create_thai_provinces_table.php +++ b/database/migrations/create_thai_provinces_table.php @@ -9,7 +9,8 @@ { public function up() { - Schema::create(ThaiAddresses::getProvinceTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getProvinceTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); // add fields @@ -23,6 +24,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getProvinceTableName()); + $table = ThaiAddresses::getProvinceTableName(); + Schema::dropIfExists($table); } }; diff --git a/database/migrations/create_thai_subdistricts_table.php b/database/migrations/create_thai_subdistricts_table.php index 59cd91f..4f96339 100644 --- a/database/migrations/create_thai_subdistricts_table.php +++ b/database/migrations/create_thai_subdistricts_table.php @@ -9,10 +9,9 @@ { public function up() { - Schema::create(ThaiAddresses::getSubdistrictTableName(), function (Blueprint $table) { + $table = ThaiAddresses::getSubdistrictTableName(); + Schema::create($table, function (Blueprint $table) { $table->id(); - - // add fields $table->string('zip_code'); $table->string('name_th'); $table->string('name_en'); @@ -24,6 +23,7 @@ public function up() public function down() { - Schema::dropIfExists(ThaiAddresses::getSubdistrictTableName()); + $table = ThaiAddresses::getSubdistrictTableName(); + Schema::dropIfExists($table); } }; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a91953b..de458d6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 5 paths: - src - config @@ -10,5 +10,4 @@ parameters: tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true - checkMissingIterableValueType: false diff --git a/src/Facades/ThaiAddresses.php b/src/Facades/ThaiAddresses.php index 44cfaf5..7992e55 100644 --- a/src/Facades/ThaiAddresses.php +++ b/src/Facades/ThaiAddresses.php @@ -5,17 +5,15 @@ use Illuminate\Support\Facades\Facade; /** - * @see \Soap\ThaiProvinces\ThaiProvinces + * @see \Soap\ThaiProvinces\ThaiAddesses */ class ThaiAddresses extends Facade { /** * Alias of dynamic class, need to be registered in service provider - * - * @return string */ - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { - return 'thai-addresses'; + return \Soap\ThaiAddresses\ThaiAddresses::class; } } diff --git a/src/Models/Address.php b/src/Models/Address.php index 670d6a5..5b4e1d8 100644 --- a/src/Models/Address.php +++ b/src/Models/Address.php @@ -48,11 +48,9 @@ class Address extends Model 'deleted_at' => 'datetime', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getAddressTableName(); + return ThaiAddresses::getAddressTableName(); } /** @@ -63,10 +61,7 @@ public function addressable(): MorphTo return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id'); } - /** - * @return BelongsTo - */ - public function subdistrict() + public function subdistrict(): BelongsTo { return $this->belongsTo(Subdistrict::class); } diff --git a/src/Models/District.php b/src/Models/District.php index 0215e54..396899c 100644 --- a/src/Models/District.php +++ b/src/Models/District.php @@ -3,6 +3,8 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class District extends Model @@ -13,19 +15,17 @@ class District extends Model 'code', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getDistrictTableName(); + return ThaiAddresses::getDistrictTableName(); } - public function province() + public function province(): BelongsTo { return $this->belongsTo(Province::class); } - public function subdistricts() + public function subdistricts(): HasMany { return $this->hasMany(Subdistrict::class); } diff --git a/src/Models/Geography.php b/src/Models/Geography.php index d8cfbe2..bfb1620 100644 --- a/src/Models/Geography.php +++ b/src/Models/Geography.php @@ -3,6 +3,7 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; /** @@ -16,14 +17,12 @@ class Geography extends Model 'name_en', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getGeographyTableName(); + return ThaiAddresses::getGeographyTableName(); } - public function provinces() + public function provinces(): HasMany { return $this->hasMany(Province::class); } diff --git a/src/Models/Province.php b/src/Models/Province.php index bcf4247..4774161 100644 --- a/src/Models/Province.php +++ b/src/Models/Province.php @@ -3,6 +3,8 @@ namespace Soap\ThaiAddresses\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class Province extends Model @@ -13,19 +15,17 @@ class Province extends Model 'code', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getProvinceTableName(); + return ThaiAddresses::getProvinceTableName(); } - public function geography() + public function geography(): BelongsTo { return $this->belongsTo(Geography::class); } - public function districts() + public function districts(): HasMany { return $this->hasMany(District::class); } diff --git a/src/Models/Subdistrict.php b/src/Models/Subdistrict.php index 1bdfb3e..cebaf90 100644 --- a/src/Models/Subdistrict.php +++ b/src/Models/Subdistrict.php @@ -4,6 +4,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Soap\ThaiAddresses\Facades\ThaiAddresses; class Subdistrict extends Model @@ -16,19 +18,17 @@ class Subdistrict extends Model 'name_en', ]; - public function __construct(array $attributes = []) + public function getTable() { - parent::__construct($attributes); - - $this->table = ThaiAddresses::getSubdistrictTableName(); + return ThaiAddresses::getSubdistrictTableName(); } - public function district() + public function district(): BelongsTo { return $this->belongsTo(District::class); } - public function addresses() + public function addresses(): HasMany { return $this->hasMany(Address::class); } diff --git a/src/ThaiAddresses.php b/src/ThaiAddresses.php index 9ab3342..5967c83 100644 --- a/src/ThaiAddresses.php +++ b/src/ThaiAddresses.php @@ -2,112 +2,73 @@ namespace Soap\ThaiAddresses; -use Illuminate\Contracts\Container\BindingResolutionException; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; - class ThaiAddresses { + protected $config; + + public function __construct() + { + $this->config = config('thai-addresses'); + } + /** * Get geograpy table name (ตารางสำหรับข้อมูลภาค) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getGeographyTableName() + public function getGeographyTableName(): string { - return config('thai-addresses.geography.table_name'); + return $this->config['geography']['table_name']; } /** * Get province table name (ตารางสำหรับข้อมูลจังหวัด) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getProvinceTableName() + public function getProvinceTableName(): string { - return config('thai-addresses.province.table_name'); + return $this->config['province']['table_name']; } /** * Get district table name (ตารางสำหรับข้อมูลอำเภอ) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getDistrictTableName() + public function getDistrictTableName(): string { - return config('thai-addresses.district.table_name'); + return $this->config['district']['table_name']; } /** * Get subdistrict table name (ตารางสำหรับข้อมูลตำบล/เทศบาล) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getSubdistrictTableName() + public function getSubdistrictTableName(): string { - return config('thai-addresses.subdistrict.table_name'); + return $this->config['subdistrict']['table_name']; } - public function getAddressTableName() + public function getAddressTableName(): string { - return config('thai-addresses.address.table_name'); + return $this->config['address']['table_name']; } /** * Get geograpy foreign key name (อ้างอิงจากตารางจังหวัด) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getGeographyForeignKeyName() + public function getGeographyForeignKeyName(): string { - return config('thai-addresses.geography.foreign_key'); + return $this->config['geography']['foreign_key']; } /** * Get province foreign key name (อ้างอิงโดยตารางอำเภอ) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getProvinceForeignKeyName() + public function getProvinceForeignKeyName(): string { - return config('thai-addresses.province.foreign_key'); + return $this->config['province']['foreign_key']; } /** * Get district foreign key name (อ้างอิงโดยตารางภาค) - * - * @return mixed - * - * @throws BindingResolutionException - * @throws NotFoundExceptionInterface - * @throws ContainerExceptionInterface */ - public function getDistrictForeignKeyName() + public function getDistrictForeignKeyName(): string { - return config('thai-addresses.district.foreign_key'); + return $this->config['district']['foreign_key']; } } diff --git a/src/ThaiAddressesServiceProvider.php b/src/ThaiAddressesServiceProvider.php index b498598..a0c374d 100644 --- a/src/ThaiAddressesServiceProvider.php +++ b/src/ThaiAddressesServiceProvider.php @@ -40,15 +40,4 @@ public function configurePackage(Package $package): void ->askToStarRepoOnGitHub('soap/thai-addresses'); }); } - - public function registeringPackage() - { - $this->app->bind('thai-addresses', function ($app) { - return new ThaiAddresses(); - }); - } - - public function bootingPackage() - { - } } diff --git a/src/Traits/HasAddress.php b/src/Traits/HasAddress.php index b9d7503..d4b6bfb 100644 --- a/src/Traits/HasAddress.php +++ b/src/Traits/HasAddress.php @@ -51,13 +51,8 @@ public function addresses(): MorphMany /** * Find addressables by distance. - * - * @param string $distance - * @param string $unit - * @param string $latitude - * @param string $longitude */ - public function findByDistance($distance, $unit, $latitude, $longitude): Collection + public function findByDistance(string $distance, string $unit, string $latitude, string $longitude): Collection { // @TODO: this method needs to be refactored! return $this->addresses()->within($distance, $unit, $latitude, $longitude)->get(); diff --git a/tests/database/migrations/create_users_table.php b/tests/database/migrations/create_users_table.php index 2c9a716..8efafc6 100644 --- a/tests/database/migrations/create_users_table.php +++ b/tests/database/migrations/create_users_table.php @@ -4,7 +4,8 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class () extends Migration { +return new class extends Migration +{ public function up(): void { Schema::create('users', function (Blueprint $table): void { diff --git a/tests/src/Feature/UserTest.php b/tests/src/Feature/UserTest.php index f0a0125..1f72794 100644 --- a/tests/src/Feature/UserTest.php +++ b/tests/src/Feature/UserTest.php @@ -9,7 +9,7 @@ test('user can have address', function () { $user = User::factory()->create(); $subdistrict = Subdistrict::where('name_th', '=', 'กระบี่ใหญ่')->first(); - $address = new Address(); + $address = new Address; $address->fill([ 'label' => 'Default Address', 'given_name' => 'Prasit',