Skip to content

Commit 3d1b7e8

Browse files
committed
refactor
1 parent df1a0e1 commit 3d1b7e8

File tree

11 files changed

+50
-75
lines changed

11 files changed

+50
-75
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.8.0
8+
9+
### Added
10+
11+
- Add methods `toPostgresPoint`, `fromPostgresPoint` to `Arr` helper
12+
713
## v4.7.0
814

915
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^9.5",
23-
"phpstan/phpstan": "~0.12.85",
23+
"phpstan/phpstan": "^1.4",
2424
"squizlabs/php_codesniffer": "^3.6"
2525
},
2626
"autoload": {

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ parameters:
44
- src
55
checkMissingIterableValueType: false
66
checkGenericClassInNonGenericObjectType: false
7-
excludes_analyse:
7+
excludePaths:
88
- 'src/Types/Point.php'
99
- 'src/Types/GeoPoint.php'

src/Exceptions/ConfigException.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@
1111
*/
1212
class ConfigException extends Exception
1313
{
14-
/**
15-
* @var mixed
16-
*/
17-
protected $config;
18-
1914
/**
2015
* ConfigException constructor.
2116
*
22-
* @param mixed|null $config
17+
* @param ?array $config
2318
* @param string $message
2419
*/
25-
public function __construct($message = 'Config Exception', $config = null)
20+
public function __construct(string $message = 'Config Exception', protected ?array $config = null)
2621
{
2722
parent::__construct($message);
28-
29-
$this->config = $config;
3023
}
3124

3225
/**

src/Exceptions/InvalidConfigException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class InvalidConfigException extends ConfigException
1414
/**
1515
* InvalidConfigException constructor.
1616
*
17-
* @param mixed|null $config
17+
* @param ?array $config
1818
* @param string $message
1919
*/
20-
public function __construct($config = null, $message = 'Invalid Configuration')
20+
public function __construct(?array $config = null, $message = 'Invalid Configuration')
2121
{
2222
parent::__construct($message, $config);
2323
}

src/Exceptions/MissingConfigException.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,13 @@
1111
*/
1212
class MissingConfigException extends ConfigException
1313
{
14-
/** @var string|null */
15-
protected $needKey;
16-
1714
/**
18-
* MissingConfigException constructor.
19-
*
20-
* @param mixed $config
21-
* @param string $needKey
15+
* @param ?array $config
16+
* @param ?string $needKey
2217
* @param string $message
2318
*/
24-
public function __construct($config = null, $needKey = null, $message = 'Missing Config')
19+
public function __construct(?array $config = null, protected ?string $needKey = null, $message = 'Missing Config')
2520
{
26-
$this->needKey = $needKey;
27-
2821
parent::__construct($message, $config);
2922
}
3023
}

src/Exceptions/MissingPropertyException.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111
*/
1212
class MissingPropertyException extends ConfigException
1313
{
14-
/** @var string|null */
15-
protected $property;
16-
1714
/**
1815
* MissingPropertyException constructor.
1916
*
2017
* @param null|string $message
2118
* @param null|string $property
22-
* @param mixed $config
19+
* @param ?array $config
2320
*/
24-
public function __construct(?string $message = null, ?string $property = null, $config = null)
21+
public function __construct(?string $message = null, protected ?string $property = null, ?array $config = null)
2522
{
26-
$this->property = $property;
2723
parent::__construct(
2824
$message ?? ($this->getName() . ($this->property ? ': "' . $this->property . '"' : '')),
2925
$config

src/Global/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
221221
return $class::$method(...$params);
222222
}
223223

224-
\Php\Support\Exceptions\MissingMethodException::throw("$class::$method");
224+
throw new \Php\Support\Exceptions\MissingMethodException("$class::$method");
225225
}
226226
}
227227

src/Helpers/Arr.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Php\Support\Helpers;
66

77
use ArrayAccess;
8+
use ArrayObject;
89
use JsonSerializable;
910
use Php\Support\Interfaces\Arrayable;
1011
use Php\Support\Interfaces\Jsonable;
@@ -68,7 +69,7 @@ public static function toArray($items): array
6869
}
6970
}
7071

71-
return $res;
72+
return (array)$res;
7273
}
7374

7475
/**
@@ -209,14 +210,14 @@ public static function toIndexedArray(array $array): array
209210
*
210211
* @param string|null $s
211212
* @param int $start
212-
* @param null $end
213+
* @param ?int $end
213214
*
214215
* @return array
215216
*/
216217
public static function fromPostgresArrayWithBraces(
217218
?string $s,
218219
int $start = 0,
219-
&$end = null,
220+
?int &$end = null,
220221
array $braces = [
221222
'{',
222223
'}',
@@ -284,11 +285,23 @@ public static function fromPostgresArrayWithBraces(
284285
return $return;
285286
}
286287

287-
public static function fromPostgresArray(?string $s, int $start = 0, &$end = null): array
288+
/**
289+
* @param string|null $s
290+
* @param int $start
291+
* @param ?int $end
292+
*
293+
* @return array
294+
*/
295+
public static function fromPostgresArray(?string $s, int $start = 0, ?int &$end = null): array
288296
{
289297
return static::fromPostgresArrayWithBraces($s, $start, $end, ['{', '}']);
290298
}
291299

300+
/**
301+
* @param string|null $value
302+
*
303+
* @return array|null
304+
*/
292305
public static function fromPostgresPoint(?string $value): ?array
293306
{
294307
if (empty($value)) {
@@ -312,7 +325,7 @@ public static function fromPostgresPoint(?string $value): ?array
312325
*
313326
* @return mixed
314327
*/
315-
public static function get($array, ?string $key, $default = null)
328+
public static function get(mixed $array, ?string $key, mixed $default = null): mixed
316329
{
317330
if (!static::accessible($array)) {
318331
return value($default);
@@ -322,11 +335,12 @@ public static function get($array, ?string $key, $default = null)
322335
return $array;
323336
}
324337

338+
/** @var array|ArrayAccess $array */
325339
if (static::exists($array, $key)) {
326340
return $array[$key];
327341
}
328342

329-
if (strpos($key, '.') === false) {
343+
if (!str_contains($key, '.')) {
330344
return $array[$key] ?? value($default);
331345
}
332346

@@ -348,7 +362,7 @@ public static function get($array, ?string $key, $default = null)
348362
*
349363
* @return bool
350364
*/
351-
public static function accessible($value): bool
365+
public static function accessible(mixed $value): bool
352366
{
353367
return is_array($value) || $value instanceof ArrayAccess;
354368
}
@@ -361,7 +375,7 @@ public static function accessible($value): bool
361375
*
362376
* @return bool
363377
*/
364-
public static function exists($array, $key): bool
378+
public static function exists(ArrayAccess|array $array, string|int $key): bool
365379
{
366380
if ($array instanceof ArrayAccess) {
367381
return $array->offsetExists($key);
@@ -378,7 +392,7 @@ public static function exists($array, $key): bool
378392
*
379393
* @return bool
380394
*/
381-
public static function has($array, $keys): bool
395+
public static function has(ArrayAccess|array $array, string|array $keys): bool
382396
{
383397
$keys = (array)$keys;
384398

@@ -410,13 +424,13 @@ public static function has($array, $keys): bool
410424
*
411425
* If no key is given to the method, the entire array will be replaced.
412426
*
413-
* @param ?array $array
427+
* @param array|ArrayObject|null $array
414428
* @param string $key
415429
* @param mixed $value
416430
*
417-
* @return array|null
431+
* @return array|ArrayObject|null
418432
*/
419-
public static function set(&$array, string $key, $value): ?array
433+
public static function set(array|ArrayObject|null &$array, string $key, mixed $value): array|ArrayObject|null
420434
{
421435
if ($array === null) {
422436
return $array;
@@ -442,12 +456,12 @@ public static function set(&$array, string $key, $value): ?array
442456
/**
443457
* Remove one or many array items from a given array using "dot" notation.
444458
*
445-
* @param array $array
459+
* @param array|ArrayObject $array
446460
* @param array|string $keys
447461
*
448462
* @return void
449463
*/
450-
public static function remove(&$array, $keys): void
464+
public static function remove(array|ArrayObject &$array, array|string $keys): void
451465
{
452466
$original = &$array;
453467
$keys = (array)$keys;
@@ -497,7 +511,7 @@ public static function replaceByTemplate(array $array, array $replace): array
497511
{
498512
$res = [];
499513
foreach ($array as $key => $item) {
500-
$res[$key] = static::itemReplaceByTemplate($item, $replace);
514+
$res[$key] = self::itemReplaceByTemplate($item, $replace);
501515
}
502516
return $res;
503517
}

src/Helpers/Json.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function htmlEncode($value): ?string
4141
* @param int $options the encoding options. For more details please refer to
4242
* <http://www.php.net/manual/en/function.json-encode.php>. Default is
4343
* `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
44-
* @param int $depth
44+
* @param int<1, max> $depth
4545
*
4646
* @return string|null
4747
*/
@@ -59,7 +59,7 @@ public static function encode($value, $options = 320, int $depth = 512): ?string
5959
* @param null|string $json the JSON string to be decoded
6060
* @param bool $asArray whether to return objects in terms of associative arrays.
6161
* @param int $options
62-
* @param int $depth
62+
* @param int<1, max> $depth
6363
*
6464
* @return mixed|null
6565
*/

0 commit comments

Comments
 (0)