Skip to content

Commit c0384b6

Browse files
authored
Fix the scoping of JSON files (#207)
The JSON files should be decode with `assoc` set to true. Otherwise encoding it again may change the schema as if `assoc` is set to false, both `[]` and `{}` will be decoded as an empty array so encoding it again may change an empty object into an empty array which may screw up a JSON schema validation.
1 parent 2e82c70 commit c0384b6

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/Scoper/Composer/AutoloadPrefixer.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,48 @@
1414

1515
namespace Humbug\PhpScoper\Scoper\Composer;
1616

17-
use function array_key_exists;
17+
use stdClass;
1818

1919
/**
2020
* @private
2121
*/
2222
final class AutoloadPrefixer
2323
{
2424
/**
25-
* @param array $contents Decoded JSON
26-
* @param string $prefix
25+
* @param stdClass $contents Decoded JSON
26+
* @param string $prefix
2727
*
2828
* @return array Prefixed decoded JSON
2929
*/
30-
public static function prefixPackageAutoloads(array $contents, string $prefix): array
30+
public static function prefixPackageAutoloads(stdClass $contents, string $prefix): stdClass
3131
{
32-
if (isset($contents['autoload'])) {
33-
$contents['autoload'] = self::prefixAutoloads($contents['autoload'], $prefix);
32+
if (isset($contents->autoload)) {
33+
$contents->autoload = self::prefixAutoloads($contents->autoload, $prefix);
3434
}
3535

36-
if (isset($contents['autoload-dev'])) {
37-
$contents['autoload-dev'] = self::prefixAutoloads($contents['autoload-dev'], $prefix);
36+
if (isset($contents->{'autoload-dev'})) {
37+
$contents->{'autoload-dev'} = self::prefixAutoloads($contents->{'autoload-dev'}, $prefix);
3838
}
3939

4040
return $contents;
4141
}
4242

43-
private static function prefixAutoloads(array $autoload, string $prefix): array
43+
private static function prefixAutoloads(stdClass $autoload, string $prefix): stdClass
4444
{
45-
if (false === array_key_exists('psr-4', $autoload) && false === array_key_exists('psr-0', $autoload)) {
45+
if (false === isset($autoload->{'psr-4'}) && false === isset($autoload->{'psr-0'})) {
4646
return $autoload;
4747
}
4848

49-
if (isset($autoload['psr-0'])) {
50-
$autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4'] ?? []);
49+
if (isset($autoload->{'psr-0'})) {
50+
$autoload->{'psr-4'} = self::mergePSR0And4(
51+
(array) $autoload->{'psr-0'},
52+
(array) ($autoload->{'psr-4'} ?? new stdClass())
53+
);
5154
}
52-
unset($autoload['psr-0']);
55+
unset($autoload->{'psr-0'});
5356

54-
if (isset($autoload['psr-4'])) {
55-
$autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
57+
if (isset($autoload->{'psr-4'})) {
58+
$autoload->{'psr-4'} = self::prefixAutoload((array) $autoload->{'psr-4'}, $prefix);
5659
}
5760

5861
return $autoload;

src/Scoper/Composer/InstalledPackagesScoper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function scope(string $filePath, string $contents, string $prefix, array
3838
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
3939
}
4040

41-
$decodedJson = json_decode($contents, true);
41+
$decodedJson = json_decode($contents);
4242

43-
$decodedJson = $this->prefixLockPackages($decodedJson, $prefix);
43+
$decodedJson = $this->prefixLockPackages((array) $decodedJson, $prefix);
4444

4545
return json_encode(
4646
$decodedJson,

src/Scoper/Composer/JsonFileScoper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function scope(string $filePath, string $contents, string $prefix, array
3636
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
3737
}
3838

39-
$decodedJson = json_decode($contents, true);
39+
$decodedJson = json_decode($contents);
4040

4141
$decodedJson = AutoloadPrefixer::prefixPackageAutoloads($decodedJson, $prefix);
4242

tests/Scoper/Composer/InstalledPackagesScoperTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,8 @@ public function provideInstalledPackagesFiles()
134134
}
135135
],
136136
"description": "Thin assertion library for input validation in business models.",
137-
"keywords": [
138-
"assert",
139-
"assertion",
140-
"validation"
141-
]
137+
"keywords": [],
138+
"platform": {}
142139
}
143140
]
144141

@@ -197,11 +194,8 @@ public function provideInstalledPackagesFiles()
197194
}
198195
],
199196
"description": "Thin assertion library for input validation in business models.",
200-
"keywords": [
201-
"assert",
202-
"assertion",
203-
"validation"
204-
]
197+
"keywords": [],
198+
"platform": {}
205199
}
206200
]
207201
JSON

tests/Scoper/Composer/JsonFileScoperTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public function provideComposerFiles()
9090
},
9191
"files": [
9292
"src/functions.php"
93-
]
93+
],
94+
"classmap": []
9495
},
9596
"autoload-dev": {
9697
"psr-4": {
@@ -99,7 +100,8 @@ public function provideComposerFiles()
99100
"files": [
100101
"tests/functions.php"
101102
]
102-
}
103+
},
104+
"config": {}
103105
}
104106

105107
JSON
@@ -115,7 +117,8 @@ public function provideComposerFiles()
115117
},
116118
"files": [
117119
"src\/functions.php"
118-
]
120+
],
121+
"classmap": []
119122
},
120123
"autoload-dev": {
121124
"psr-4": {
@@ -124,7 +127,8 @@ public function provideComposerFiles()
124127
"files": [
125128
"tests\/functions.php"
126129
]
127-
}
130+
},
131+
"config": {}
128132
}
129133
JSON
130134
];

0 commit comments

Comments
 (0)