Skip to content

Commit

Permalink
Fix the scoping of JSON files (#207)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theofidry authored May 19, 2018
1 parent 2e82c70 commit c0384b6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
33 changes: 18 additions & 15 deletions src/Scoper/Composer/AutoloadPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,48 @@

namespace Humbug\PhpScoper\Scoper\Composer;

use function array_key_exists;
use stdClass;

/**
* @private
*/
final class AutoloadPrefixer
{
/**
* @param array $contents Decoded JSON
* @param string $prefix
* @param stdClass $contents Decoded JSON
* @param string $prefix
*
* @return array Prefixed decoded JSON
*/
public static function prefixPackageAutoloads(array $contents, string $prefix): array
public static function prefixPackageAutoloads(stdClass $contents, string $prefix): stdClass
{
if (isset($contents['autoload'])) {
$contents['autoload'] = self::prefixAutoloads($contents['autoload'], $prefix);
if (isset($contents->autoload)) {
$contents->autoload = self::prefixAutoloads($contents->autoload, $prefix);
}

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

return $contents;
}

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

if (isset($autoload['psr-0'])) {
$autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4'] ?? []);
if (isset($autoload->{'psr-0'})) {
$autoload->{'psr-4'} = self::mergePSR0And4(
(array) $autoload->{'psr-0'},
(array) ($autoload->{'psr-4'} ?? new stdClass())
);
}
unset($autoload['psr-0']);
unset($autoload->{'psr-0'});

if (isset($autoload['psr-4'])) {
$autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix);
if (isset($autoload->{'psr-4'})) {
$autoload->{'psr-4'} = self::prefixAutoload((array) $autoload->{'psr-4'}, $prefix);
}

return $autoload;
Expand Down
4 changes: 2 additions & 2 deletions src/Scoper/Composer/InstalledPackagesScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function scope(string $filePath, string $contents, string $prefix, array
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
}

$decodedJson = json_decode($contents, true);
$decodedJson = json_decode($contents);

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

return json_encode(
$decodedJson,
Expand Down
2 changes: 1 addition & 1 deletion src/Scoper/Composer/JsonFileScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function scope(string $filePath, string $contents, string $prefix, array
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
}

$decodedJson = json_decode($contents, true);
$decodedJson = json_decode($contents);

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

Expand Down
14 changes: 4 additions & 10 deletions tests/Scoper/Composer/InstalledPackagesScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ public function provideInstalledPackagesFiles()
}
],
"description": "Thin assertion library for input validation in business models.",
"keywords": [
"assert",
"assertion",
"validation"
]
"keywords": [],
"platform": {}
}
]

Expand Down Expand Up @@ -197,11 +194,8 @@ public function provideInstalledPackagesFiles()
}
],
"description": "Thin assertion library for input validation in business models.",
"keywords": [
"assert",
"assertion",
"validation"
]
"keywords": [],
"platform": {}
}
]
JSON
Expand Down
12 changes: 8 additions & 4 deletions tests/Scoper/Composer/JsonFileScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public function provideComposerFiles()
},
"files": [
"src/functions.php"
]
],
"classmap": []
},
"autoload-dev": {
"psr-4": {
Expand All @@ -99,7 +100,8 @@ public function provideComposerFiles()
"files": [
"tests/functions.php"
]
}
},
"config": {}
}

JSON
Expand All @@ -115,7 +117,8 @@ public function provideComposerFiles()
},
"files": [
"src\/functions.php"
]
],
"classmap": []
},
"autoload-dev": {
"psr-4": {
Expand All @@ -124,7 +127,8 @@ public function provideComposerFiles()
"files": [
"tests\/functions.php"
]
}
},
"config": {}
}
JSON
];
Expand Down

0 comments on commit c0384b6

Please sign in to comment.