Skip to content

Commit 400f8a0

Browse files
committed
refactor: remove useless code statement
1 parent 49c0f65 commit 400f8a0

File tree

4 files changed

+12
-74
lines changed

4 files changed

+12
-74
lines changed

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
"autoload-dev": {
2727
"psr-4": {
2828
"Xdevor\\ComposerParser\\Tests\\": "tests/"
29-
},
30-
"files": [
31-
"./src/helpers.php"
32-
]
29+
}
3330
},
3431
"scripts": {
3532
"test": "./vendor/bin/pest"

src/Concerns/ParseArray.php

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,26 @@
22

33
namespace Xdevor\ComposerParser\Concerns;
44

5-
use ArrayAccess;
6-
7-
/**
8-
* The code here is based on/reference to Illuminate\Support/Arr.
9-
*/
105
trait ParseArray
116
{
12-
/**
13-
* Get an item from an array using "dot" notation.
14-
*
15-
* @param \ArrayAccess|array $array
16-
* @param string|int|null $key
17-
* @param mixed $default
18-
* @return mixed
19-
*/
20-
private static function parseArray($array, $key, $default = null)
7+
private static function parseArray(array $array, string $key, $default = null)
218
{
22-
if (! static::isValueAccessible($array)) {
23-
return value($default);
24-
}
25-
26-
if (is_null($key)) {
27-
return $array;
28-
}
29-
30-
if (static::isKeyExists($array, $key)) {
9+
if (array_key_exists($key, $array)) {
3110
return $array[$key];
3211
}
3312

3413
if (strpos($key, '.') === false) {
35-
return $array[$key] ?? value($default);
14+
return $array[$key] ?? $default;
3615
}
3716

3817
foreach (explode('.', $key) as $segment) {
39-
if (static::isValueAccessible($array) && static::isKeyExists($array, $segment)) {
18+
if (array_key_exists($segment, $array)) {
4019
$array = $array[$segment];
4120
} else {
42-
return value($default);
21+
return $default;
4322
}
4423
}
4524

4625
return $array;
4726
}
48-
49-
/**
50-
* Determine whether the given value is array accessible.
51-
*
52-
* @param mixed $value
53-
* @return bool
54-
*/
55-
private static function isValueAccessible($value)
56-
{
57-
return is_array($value) || $value instanceof ArrayAccess;
58-
}
59-
60-
/**
61-
* Determine if the given key exists in the provided array.
62-
*
63-
* @param \ArrayAccess|array $array
64-
* @param string|int $key
65-
* @return bool
66-
*/
67-
private static function isKeyExists($array, $key)
68-
{
69-
if ($array instanceof ArrayAccess) {
70-
return $array->offsetExists($key);
71-
}
72-
73-
return array_key_exists($key, $array);
74-
}
7527
}

src/helpers.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/ParserTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
expect($actual)->toBe(null);
1515
});
1616

17-
test('It return default value if key not exist', function () {
17+
test('It return default value if key not exist', function ($key) {
1818
$packageName = 'ooo/xxx';
19-
$actual = (new Parser())->parse($packageName, 'name', 'this_is_default');
19+
$actual = (new Parser())->parse($packageName, $key, 'this_is_default');
2020
expect($actual)->toBe('this_is_default');
21-
});
21+
})->with([
22+
'name',
23+
'author.name'
24+
]);
2225

2326
test('It can return correct value if array key exist', function () {
2427
$packageName = 'doctrine/instantiator';

0 commit comments

Comments
 (0)