-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement VersionOrConstraintNormalizer
- Loading branch information
1 parent
d45da47
commit 5b21027
Showing
5 changed files
with
208 additions
and
3 deletions.
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,82 @@ | ||
<?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 Localheinz\Json\Normalizer\NormalizerInterface; | ||
|
||
final class VersionOrConstraintNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private static $properties = [ | ||
'conflict', | ||
'provide', | ||
'replaces', | ||
'require', | ||
'require-dev', | ||
]; | ||
|
||
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) { | ||
$packages = (array) $decoded->{$name}; | ||
|
||
if (!\count($packages)) { | ||
continue; | ||
} | ||
|
||
$decoded->{$name} = $this->normalizeOrVersionConstraints($packages); | ||
} | ||
|
||
return \json_encode($decoded); | ||
} | ||
|
||
private function normalizeOrVersionConstraints(array $packages): array | ||
{ | ||
return \array_map(function (string $versionConstraint): string { | ||
$orConstraints = \preg_split( | ||
'{\s*\|\|?\s*}', | ||
\trim($versionConstraint) | ||
); | ||
|
||
if (!\is_array($orConstraints)) { | ||
return $versionConstraint; | ||
} | ||
|
||
return \implode( | ||
' || ', | ||
$orConstraints | ||
); | ||
}, $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
106 changes: 106 additions & 0 deletions
106
test/Unit/Normalizer/VersionOrConstraintNormalizerTest.php
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,106 @@ | ||
<?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\VersionOrConstraintNormalizer; | ||
|
||
final class VersionOrConstraintNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
public function testNormalizeDoesNotModifyOtherProperty() | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"foo": { | ||
"foo": "^1.2.3|^2.3.4", | ||
"bar": "^1.2.3||^2.3.4", | ||
"baz": "^ 1.2.3 | ^2.3.4 ", | ||
"qux": " ^1.2.3 || ^2.3.4 " | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new VersionOrConstraintNormalizer(); | ||
|
||
$this->assertSame($json, $normalizer->normalize($json)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerProperty | ||
* | ||
* @param string $property | ||
*/ | ||
public function testNormalizeIgnoresEmptyPackageHash(string $property) | ||
{ | ||
$json = <<<JSON | ||
{ | ||
"${property}": {} | ||
} | ||
JSON; | ||
|
||
$normalizer = new VersionOrConstraintNormalizer(); | ||
|
||
$this->assertSame(\json_encode(\json_decode($json)), $normalizer->normalize($json)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerProperty | ||
* | ||
* @param string $property | ||
*/ | ||
public function testNormalizeReplacesSinglePipeWithDoublePipeIfPropertyExists(string $property) | ||
{ | ||
$json = <<<JSON | ||
{ | ||
"${property}": { | ||
"foo": "^1.2.3|^2.3.4", | ||
"bar": "^1.2.3||^2.3.4", | ||
"baz": " ^1.2.3 | ^2.3.4 ", | ||
"qux": " ^1.2.3 || ^2.3.4 " | ||
} | ||
} | ||
JSON; | ||
|
||
$normalized = <<<JSON | ||
{ | ||
"${property}": { | ||
"foo": "^1.2.3 || ^2.3.4", | ||
"bar": "^1.2.3 || ^2.3.4", | ||
"baz": "^1.2.3 || ^2.3.4", | ||
"qux": "^1.2.3 || ^2.3.4" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new VersionOrConstraintNormalizer(); | ||
|
||
$this->assertSame(\json_encode(\json_decode($normalized)), $normalizer->normalize($json)); | ||
} | ||
|
||
public function providerProperty(): \Generator | ||
{ | ||
$values = [ | ||
'conflict', | ||
'provide', | ||
'replaces', | ||
'require', | ||
'require-dev', | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield $value => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
} |