-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from localheinz/feature/package-hash-normalizer
Enhancement: Implement PackageHashNormalizer
- Loading branch information
Showing
4 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/composer-normalize | ||
*/ | ||
|
||
namespace Localheinz\Composer\Normalize\Normalizer; | ||
|
||
use Composer\Repository; | ||
use Localheinz\Json\Normalizer\NormalizerInterface; | ||
|
||
final class PackageHashNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private static $properties = [ | ||
'conflict', | ||
'provide', | ||
'replaces', | ||
'require', | ||
'require-dev', | ||
'suggest', | ||
]; | ||
|
||
public function normalize(string $json): string | ||
{ | ||
$decoded = \json_decode($json); | ||
|
||
if (null === $decoded && JSON_ERROR_NONE !== \json_last_error()) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
} | ||
|
||
$objectProperties = \array_intersect_key( | ||
\get_object_vars($decoded), | ||
\array_flip(self::$properties) | ||
); | ||
|
||
if (!\count($objectProperties)) { | ||
return $json; | ||
} | ||
|
||
foreach ($objectProperties as $name => $value) { | ||
$decoded->{$name} = $this->sortPackages((array) $decoded->{$name}); | ||
} | ||
|
||
return \json_encode($decoded); | ||
} | ||
|
||
/** | ||
* This code is adopted from composer/composer (originally licensed under MIT by Nils Adermann <naderman@naderman.de> | ||
* and Jordi Boggiano <j.boggiano@seld.be>). | ||
* | ||
* @see https://github.com/composer/composer/blob/1.6.2/src/Composer/Json/JsonManipulator.php#L110-L146 | ||
* | ||
* @param string[] $packages | ||
* | ||
* @return string[] | ||
*/ | ||
private function sortPackages(array $packages): array | ||
{ | ||
$prefix = function ($requirement) { | ||
if (\preg_match(Repository\PlatformRepository::PLATFORM_PACKAGE_REGEX, $requirement)) { | ||
return \preg_replace( | ||
[ | ||
'/^php/', | ||
'/^hhvm/', | ||
'/^ext/', | ||
'/^lib/', | ||
'/^\D/', | ||
], | ||
[ | ||
'0-$0', | ||
'1-$0', | ||
'2-$0', | ||
'3-$0', | ||
'4-$0', | ||
], | ||
$requirement | ||
); | ||
} | ||
|
||
return '5-' . $requirement; | ||
}; | ||
|
||
\uksort($packages, function ($a, $b) use ($prefix) { | ||
return \strnatcmp($prefix($a), $prefix($b)); | ||
}); | ||
|
||
return $packages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/composer-normalize | ||
*/ | ||
|
||
namespace Localheinz\Composer\Normalize\Test\Unit\Normalizer; | ||
|
||
use Localheinz\Composer\Normalize\Normalizer\PackageHashNormalizer; | ||
|
||
final class PackageHashNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
public function testNormalizeDoesNotModifyOtherProperty() | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new PackageHashNormalizer(); | ||
|
||
$this->assertSame($json, $normalizer->normalize($json)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerProperty | ||
* | ||
* @param string $property | ||
*/ | ||
public function testNormalizeSortsPackageHashIfPropertyExists(string $property) | ||
{ | ||
$json = <<<JSON | ||
{ | ||
"${property}": { | ||
"localheinz/test-util": "Provides utilities for tests.", | ||
"hhvm": "Okay", | ||
"lib-baz": "Maybe it helps.", | ||
"localheinz/php-cs-fixer-config": "Provides a configuration factory and multiple rule sets for friendsofphp/php-cs-fixer.", | ||
"ext-foo": "Could be useful", | ||
"php": "Because why not, it's great." | ||
}, | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalized = <<<JSON | ||
{ | ||
"${property}": { | ||
"php": "Because why not, it's great.", | ||
"hhvm": "Okay", | ||
"ext-foo": "Could be useful", | ||
"lib-baz": "Maybe it helps.", | ||
"localheinz/php-cs-fixer-config": "Provides a configuration factory and multiple rule sets for friendsofphp/php-cs-fixer.", | ||
"localheinz/test-util": "Provides utilities for tests." | ||
}, | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new PackageHashNormalizer(); | ||
|
||
$this->assertSame(\json_encode(\json_decode($normalized)), $normalizer->normalize($json)); | ||
} | ||
|
||
public function providerProperty(): \Generator | ||
{ | ||
$values = [ | ||
'conflict', | ||
'provide', | ||
'replaces', | ||
'require', | ||
'require-dev', | ||
'suggest', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
} |