Skip to content

Commit

Permalink
Enhancement: Implement VersionOrConstraintNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 15, 2018
1 parent d45da47 commit 5b21027
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ as well as the following normalizers provided by this package:
* [`Localheinz\Composer\Normalize\Normalizer\BinNormalizer`](#binnormalizer)
* [`Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer`](#confighashnormalizer)
* [`Localheinz\Composer\Normalize\Normalizer\PackageHashNormalizer`](#packagehashnormalizer)
* [`Localheinz\Composer\Normalize\Normalizer\VersionOrConstraintNormalizer`](#versionorconstraintnormalizer)

### `BinNormalizer`

Expand Down Expand Up @@ -79,6 +80,19 @@ sections, the `PackageHashNormalizer` will sort the content of these sections.
the `--sort-packages` flag and configuration at https://getcomposer.org/doc/06-config.md#sort-packages
and https://getcomposer.org/doc/03-cli.md#require.

### `VersionOrConstraintNormalizer`

If `composer.json` contains any or (`|` or `||`) version constraints in the

* `conflict`
* `provide`
* `replaces`
* `require`
* `require-dev`

sections, the `VersionOrConstraintNormalizer` will ensure that a `||` is
used instead of a `|`, as well as ensure a single space around them.

## Contributing

Please have a look at [`CONTRIBUTING.md`](.github/CONTRIBUTING.md).
Expand Down
3 changes: 2 additions & 1 deletion src/Normalizer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function __construct(string $schemaUri = 'https://getcomposer.org/schema.
new SchemaNormalizer($schemaUri),
new BinNormalizer(),
new ConfigHashNormalizer(),
new PackageHashNormalizer()
new PackageHashNormalizer(),
new VersionOrConstraintNormalizer()
));
}

Expand Down
82 changes: 82 additions & 0 deletions src/Normalizer/VersionOrConstraintNormalizer.php
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);
}
}
6 changes: 4 additions & 2 deletions test/Unit/Normalizer/ComposerJsonNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Localheinz\Composer\Normalize\Normalizer\ComposerJsonNormalizer;
use Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer;
use Localheinz\Composer\Normalize\Normalizer\PackageHashNormalizer;
use Localheinz\Composer\Normalize\Normalizer\VersionOrConstraintNormalizer;
use Localheinz\Json\Normalizer\AutoFormatNormalizer;
use Localheinz\Json\Normalizer\ChainNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
Expand All @@ -41,6 +42,7 @@ public function testComposesNormalizers()
BinNormalizer::class,
ConfigHashNormalizer::class,
PackageHashNormalizer::class,
VersionOrConstraintNormalizer::class,
];

$this->assertComposesNormalizers($normalizerClassNames, $chainNormalizer);
Expand Down Expand Up @@ -84,7 +86,7 @@ public function testNormalizeNormalizes()
"require-dev": {
"localheinz/test-util": "0.6.1",
"phpunit/phpunit": "^6.5.5",
"localheinz/php-cs-fixer-config": "~1.11.0"
"localheinz/php-cs-fixer-config": "~1.0.0|~1.11.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -126,7 +128,7 @@ public function testNormalizeNormalizes()
"localheinz/json-printer": "^1.0.0"
},
"require-dev": {
"localheinz/php-cs-fixer-config": "~1.11.0",
"localheinz/php-cs-fixer-config": "~1.0.0 || ~1.11.0",
"localheinz/test-util": "0.6.1",
"phpunit/phpunit": "^6.5.5"
},
Expand Down
106 changes: 106 additions & 0 deletions test/Unit/Normalizer/VersionOrConstraintNormalizerTest.php
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,
];
}
}
}

0 comments on commit 5b21027

Please sign in to comment.