Skip to content

Commit 8fc13e5

Browse files
authored
Merge pull request #31 from n4ss1m/enhancements_dev
Set PHPStan to Maximum Level
2 parents 2d05022 + 516795a commit 8fc13e5

File tree

12 files changed

+99
-69
lines changed

12 files changed

+99
-69
lines changed

baseline-8.1.neon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Method Kossa\\\\AlgerianCities\\\\Commune\\:\\:wilaya\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsTo\\<Kossa\\\\AlgerianCities\\\\Wilaya, \\$this\\(Kossa\\\\AlgerianCities\\\\Commune\\)\\> but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsTo\\<Kossa\\\\AlgerianCities\\\\Wilaya, Kossa\\\\AlgerianCities\\\\Commune\\>\\.$#"
5+
count: 1
6+
path: src/Commune.php
7+
8+
-
9+
message: "#^Generic type Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<Kossa\\\\AlgerianCities\\\\Commune, \\$this\\(Kossa\\\\AlgerianCities\\\\Wilaya\\)\\> in PHPDoc tag @return specifies 2 template types, but class Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany supports only 1\\: TRelatedModel$#"
10+
count: 1
11+
path: src/Wilaya.php
12+
13+
-
14+
message: "#^Method Kossa\\\\AlgerianCities\\\\Wilaya\\:\\:communes\\(\\) should return Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<Kossa\\\\AlgerianCities\\\\Commune, \\$this\\(Kossa\\\\AlgerianCities\\\\Wilaya\\)\\> but returns Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<Kossa\\\\AlgerianCities\\\\Commune\\>\\.$#"
15+
count: 1
16+
path: src/Wilaya.php

database/seeders/WilayaCommuneSeeder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class WilayaCommuneSeeder extends Seeder
1212
{
1313
/**
1414
* Run the database seeds.
15-
*
16-
* @return void
1715
*/
18-
public function run()
16+
public function run(): void
1917
{
2018
// Check if table exist
2119
if (! Schema::hasTable('wilayas') || ! Schema::hasTable('communes')) {
@@ -35,13 +33,13 @@ public function run()
3533
$this->command->comment('Wilayas/Communes already loaded');
3634
}
3735

38-
protected function loadData()
36+
protected function loadData(): void
3937
{
4038
$this->insertWilayas();
4139
$this->insertCommunes();
4240
}
4341

44-
protected function insertWilayas()
42+
protected function insertWilayas(): void
4543
{
4644
// Load wilayas from json
4745
try {
@@ -63,7 +61,7 @@ protected function insertWilayas()
6361
DB::table('wilayas')->insert($data);
6462
}
6563

66-
protected function insertCommunes()
64+
protected function insertCommunes(): void
6765
{
6866
// Load wilayas from json
6967
try {

ignore-by-php-version.neon.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$includes = [];
6+
7+
if (PHP_VERSION_ID < 80200) {
8+
$includes[] = __DIR__.'/baseline-8.1.neon';
9+
}
10+
11+
$config = [];
12+
$config['includes'] = $includes;
13+
$config['parameters']['phpVersion'] = PHP_VERSION_ID;
14+
15+
return $config;

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
includes:
22
- vendor/larastan/larastan/extension.neon
3+
- ignore-by-php-version.neon.php
34
parameters:
4-
level: 5
5+
level: max
56
paths:
67
- src/
78
- tests/

src/Commune.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Kossa\AlgerianCities;
66

7+
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
810
use Illuminate\Support\Facades\DB;
911

1012
/**
@@ -14,31 +16,32 @@ class Commune extends Model
1416
{
1517
protected $fillable = ['name', 'arabic_name', 'post_code', 'wilaya_id', 'longitude', 'latitude'];
1618

17-
/*
18-
|------------------------------------------------------------------------------------
19-
| Validations
20-
|------------------------------------------------------------------------------------
21-
*/
22-
public static function rules($update = false, $id = null): array
19+
/**
20+
* Validation rules
21+
*
22+
* @return array<string, string>
23+
*/
24+
public static function rules(): array
2325
{
2426
return [
2527
'name' => 'required',
2628
'wilaya_id' => 'required|numeric',
2729
];
2830
}
2931

30-
/*
31-
|------------------------------------------------------------------------------------
32-
| Relations
33-
|------------------------------------------------------------------------------------
34-
*/
35-
public function scopeWithWilaya($q, $name = 'name'): void
32+
/**
33+
* @param Builder<Wilaya> $query
34+
*/
35+
public function scopeWithWilaya(Builder $query, string $name = 'name'): void
3636
{
37-
$q->leftJoin('wilayas', 'wilayas.id', 'communes.wilaya_id')
37+
$query->leftJoin('wilayas', 'wilayas.id', 'communes.wilaya_id')
3838
->select('communes.id', DB::raw(sprintf("concat(communes.%s, ', ', wilayas.%s) as name", $name, $name)));
3939
}
4040

41-
public function wilaya()
41+
/**
42+
* @return BelongsTo<Wilaya, $this>
43+
*/
44+
public function wilaya(): BelongsTo
4245
{
4346
return $this->belongsTo(Wilaya::class)->withDefault();
4447
}
@@ -48,7 +51,7 @@ public function wilaya()
4851
| Attribute
4952
|------------------------------------------------------------------------------------
5053
*/
51-
public function getWilayaNameAttribute()
54+
public function getWilayaNameAttribute(): string
5255
{
5356
return $this->wilaya->name;
5457
}

src/Controllers/Api/CommuneController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44

55
namespace Kossa\AlgerianCities\Controllers\Api;
66

7+
use Illuminate\Database\Eloquent\Collection;
78
use Kossa\AlgerianCities\Commune;
89

910
class CommuneController
1011
{
1112
/**
1213
* Get all Communes.
14+
*
15+
* @return Collection<int, Commune>
1316
*/
14-
public function index()
17+
public function index(): Collection
1518
{
1619
return Commune::all();
1720
}
1821

1922
/**
2023
* Get a specified Commune.
21-
*
22-
* @param int $id
2324
*/
24-
public function show($id)
25+
public function show(int $id): Commune
2526
{
2627
return Commune::findOrFail($id);
2728
}
2829

2930
/**
3031
* Search wilaya by name or arabic_name
3132
*
32-
* @param string $q
33+
* @return Collection<int, Commune>
3334
*/
34-
public function search($q)
35+
public function search(string $keyword): Collection
3536
{
36-
return Commune::where('name', 'like', sprintf('%%%s%%', $q))
37-
->orWhere('arabic_name', 'like', sprintf('%%%s%%', $q))
37+
return Commune::where('name', 'like', sprintf('%%%s%%', $keyword))
38+
->orWhere('arabic_name', 'like', sprintf('%%%s%%', $keyword))
3839
->get();
3940
}
4041
}

src/Controllers/Api/WilayaController.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,49 @@
44

55
namespace Kossa\AlgerianCities\Controllers\Api;
66

7+
use Illuminate\Database\Eloquent\Collection;
78
use Kossa\AlgerianCities\Commune;
89
use Kossa\AlgerianCities\Wilaya;
910

1011
class WilayaController
1112
{
1213
/**
1314
* Get all wilayas.
15+
*
16+
* @return Collection<int, Wilaya>
1417
*/
15-
public function index()
18+
public function index(): Collection
1619
{
1720
return Wilaya::all();
1821
}
1922

2023
/**
2124
* Get a specified Wilaya.
22-
*
23-
* @param int $id
2425
*/
25-
public function show($id)
26+
public function show(int $id): Wilaya
2627
{
2728
return Wilaya::findOrFail($id);
2829
}
2930

3031
/**
3132
* Get communes of wilayas_id.
3233
*
33-
* @param int $id
34+
* @return Collection<int, Commune>
3435
*/
35-
public function communes($id)
36+
public function communes(int $id): Collection
3637
{
3738
return Commune::where('wilaya_id', $id)->get();
3839
}
3940

4041
/**
4142
* Search wilaya by name or arabic_name
4243
*
43-
* @param string $q
44+
* @return Collection<int, Wilaya>
4445
*/
45-
public function search($q)
46+
public function search(string $keyword): Collection
4647
{
47-
return Wilaya::where('name', 'like', sprintf('%%%s%%', $q))
48-
->orWhere('arabic_name', 'like', sprintf('%%%s%%', $q))
48+
return Wilaya::where('name', 'like', sprintf('%%%s%%', $keyword))
49+
->orWhere('arabic_name', 'like', sprintf('%%%s%%', $keyword))
4950
->get();
5051
}
5152
}

src/Wilaya.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kossa\AlgerianCities;
66

77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
89

910
/**
1011
* @property string $name
@@ -13,24 +14,22 @@ class Wilaya extends Model
1314
{
1415
protected $fillable = ['name', 'arabic_name', 'longitude', 'latitude'];
1516

16-
/*
17-
|------------------------------------------------------------------------------------
18-
| Validations
19-
|------------------------------------------------------------------------------------
20-
*/
21-
public static function rules($update = false, $id = null): array
17+
/**
18+
* Validation rules
19+
*
20+
* @return array<string, string>
21+
*/
22+
public static function rules(): array
2223
{
2324
return [
2425
'name' => 'required',
2526
];
2627
}
2728

28-
/*
29-
|------------------------------------------------------------------------------------
30-
| Relations
31-
|------------------------------------------------------------------------------------
32-
*/
33-
public function communes()
29+
/**
30+
* @return HasMany<Commune, $this>
31+
*/
32+
public function communes(): HasMany
3433
{
3534
return $this->hasMany(Commune::class);
3635
}

src/helpers.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
/**
1111
* Get list of communes
1212
*
13-
* @param int|null $wilaya_id wilaya_id the id of wilaya
14-
* @param bool $withWilaya withWilaya if you need to include the wilaya
15-
* @param string $name name the default name user arabic_name to get arabic name
13+
* @return array<mixed>
1614
*/
17-
function communes(?int $wilaya_id = null, bool $withWilaya = false, $name = 'name'): array
15+
function communes(?int $wilaya_id = null, bool $withWilaya = false, string $name = 'name'): array
1816
{
1917
$communes = Commune::query();
2018

@@ -35,7 +33,7 @@ function communes(?int $wilaya_id = null, bool $withWilaya = false, $name = 'nam
3533
/**
3634
* Get list of wilayas
3735
*
38-
* @param string $name default name use arabic_name to get wilayas in arabic
36+
* @return array<mixed>
3937
*/
4038
function wilayas(string $name = 'name'): array
4139
{
@@ -47,11 +45,8 @@ function wilayas(string $name = 'name'): array
4745

4846
/**
4947
* Get single commune
50-
*
51-
* @param int $id id The ID of commune
52-
* @param bool $withWilaya withWilaya True if you need to include wilaya
5348
*/
54-
function commune(int $id, bool $withWilaya = false)
49+
function commune(int $id, bool $withWilaya = false): Commune
5550
{
5651
$commune = Commune::findOrFail($id);
5752

@@ -67,10 +62,8 @@ function commune(int $id, bool $withWilaya = false)
6762

6863
/**
6964
* Get single wilaya
70-
*
71-
* @param int $id id The ID of wilaya
72-
*/
73-
function wilaya($id)
65+
**/
66+
function wilaya(int $id): Wilaya
7467
{
7568
return wilaya::findOrFail($id);
7669
}

tests/CommuneTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_if_commune_count_is_correct(): void
1616
public function test_if_commune_details_are_correct(): void
1717
{
1818
$sampleCommune = Commune::where('name', 'Alger Centre')
19-
->first(['id', 'name', 'arabic_name', 'post_code', 'longitude', 'latitude']);
19+
->firstOrFail(['id', 'name', 'arabic_name', 'post_code', 'longitude', 'latitude']);
2020

2121
$this->assertJsonStringEqualsJsonString(
2222
$sampleCommune->toJson(),

0 commit comments

Comments
 (0)