Skip to content

Commit 1e5a3a8

Browse files
committed
Merge branch 'refactor'
2 parents a43e843 + 400f8a0 commit 1e5a3a8

File tree

6 files changed

+110
-82
lines changed

6 files changed

+110
-82
lines changed

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
"autoload-dev": {
2828
"psr-4": {
2929
"Xdevor\\ComposerParser\\Tests\\": "tests/"
30-
},
31-
"files": [
32-
"./src/helpers.php"
33-
]
30+
}
3431
},
3532
"scripts": {
3633
"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-
public static function get($array, $key, $default = null)
7+
private static function parseArray(array $array, string $key, $default = null)
218
{
22-
if (! static::accessible($array)) {
23-
return value($default);
24-
}
25-
26-
if (is_null($key)) {
27-
return $array;
28-
}
29-
30-
if (static::exists($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::accessible($array) && static::exists($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-
public static function accessible($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-
public static function exists($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/Concerns/ParseInstalledPackages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function setInstalledJsonPath(?string $installedJsonPath)
1818

1919
protected function parsePackageComposer(string $packageName, string $key, $default = null)
2020
{
21-
return self::get($this->packageComposer($packageName), $key, $default);
21+
return self::parseArray($this->packageComposer($packageName), $key, $default);
2222
}
2323

2424
protected function packageComposer(string $templatePackageName): array

src/helpers.php

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

tests/ParserTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33
use Xdevor\ComposerParser\Parser;
44

55
test('It can return correct value if key exist', function () {
6-
$package = 'doctrine/instantiator';
7-
$illuminateSupportName = (new Parser())->parse($package, 'name');
8-
expect($illuminateSupportName)->toBe($package);
6+
$packageName = 'doctrine/instantiator';
7+
$actual = (new Parser())->parse($packageName, 'name');
8+
expect($actual)->toBe($packageName);
99
});
1010

11-
test('It return default if key not exist', function () {
12-
$package = 'ooo/xxx';
13-
$illuminateSupportName = (new Parser())->parse($package, 'name', null);
14-
expect($illuminateSupportName)->toBe(null);
11+
test('It return null if key not exist', function () {
12+
$packageName = 'ooo/xxx';
13+
$actual = (new Parser())->parse($packageName, 'name');
14+
expect($actual)->toBe(null);
1515
});
1616

17+
test('It return default value if key not exist', function ($key) {
18+
$packageName = 'ooo/xxx';
19+
$actual = (new Parser())->parse($packageName, $key, 'this_is_default');
20+
expect($actual)->toBe('this_is_default');
21+
})->with([
22+
'name',
23+
'author.name'
24+
]);
25+
1726
test('It can return correct value if array key exist', function () {
18-
$package = 'doctrine/instantiator';
19-
$illuminateSupportName = (new Parser())->parse($package, 'license.0');
20-
expect($illuminateSupportName)->toBe('MIT');
27+
$packageName = 'doctrine/instantiator';
28+
$actual = (new Parser())->parse($packageName, 'license.0');
29+
expect($actual)->toBe('MIT');
30+
});
31+
32+
test('It can use custom installed.json path', function () {
33+
$packageName = 'filp/whoops';
34+
$actual = (new Parser(__DIR__ . '/../tests/assets/fakeInstalled.json'))->parse($packageName, 'extra.branch-alias.dev-master');
35+
expect($actual)->toBe('2.7-dev');
2136
});

tests/assets/fakeInstalled.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "filp/whoops",
5+
"version": "2.15.1",
6+
"version_normalized": "2.15.1.0",
7+
"source": {
8+
"type": "git",
9+
"url": "https://github.com/filp/whoops.git",
10+
"reference": "e864ac957acd66e1565f25efda61e37791a5db0b"
11+
},
12+
"dist": {
13+
"type": "zip",
14+
"url": "https://api.github.com/repos/filp/whoops/zipball/e864ac957acd66e1565f25efda61e37791a5db0b",
15+
"reference": "e864ac957acd66e1565f25efda61e37791a5db0b",
16+
"shasum": ""
17+
},
18+
"require": {
19+
"php": "^5.5.9 || ^7.0 || ^8.0",
20+
"psr/log": "^1.0.1 || ^2.0 || ^3.0"
21+
},
22+
"require-dev": {
23+
"mockery/mockery": "^0.9 || ^1.0",
24+
"phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.3",
25+
"symfony/var-dumper": "^2.6 || ^3.0 || ^4.0 || ^5.0"
26+
},
27+
"suggest": {
28+
"symfony/var-dumper": "Pretty print complex values better with var-dumper available",
29+
"whoops/soap": "Formats errors as SOAP responses"
30+
},
31+
"time": "2023-03-06T18:09:13+00:00",
32+
"type": "library",
33+
"extra": {
34+
"branch-alias": {
35+
"dev-master": "2.7-dev"
36+
}
37+
},
38+
"installation-source": "dist",
39+
"autoload": {
40+
"psr-4": {
41+
"Whoops\\": "src/Whoops/"
42+
}
43+
},
44+
"notification-url": "https://packagist.org/downloads/",
45+
"license": [
46+
"MIT"
47+
],
48+
"authors": [
49+
{
50+
"name": "Filipe Dobreira",
51+
"homepage": "https://github.com/filp",
52+
"role": "Developer"
53+
}
54+
],
55+
"description": "php error handling for cool kids",
56+
"homepage": "https://filp.github.io/whoops/",
57+
"keywords": [
58+
"error",
59+
"exception",
60+
"handling",
61+
"library",
62+
"throwable",
63+
"whoops"
64+
],
65+
"support": {
66+
"issues": "https://github.com/filp/whoops/issues",
67+
"source": "https://github.com/filp/whoops/tree/2.15.1"
68+
},
69+
"funding": [
70+
{
71+
"url": "https://github.com/denis-sokolov",
72+
"type": "github"
73+
}
74+
],
75+
"install-path": "../filp/whoops"
76+
}
77+
]
78+
}

0 commit comments

Comments
 (0)