Skip to content

Commit

Permalink
Enhancement: Implement VersionConstraintNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 19, 2018
1 parent aea2b72 commit 45bafad
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 3 deletions.
20 changes: 20 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`](#versionconstraintnormalizer)

### `BinNormalizer`

Expand Down Expand Up @@ -79,6 +80,25 @@ 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.

### `VersionConstraintNormalizer`

If `composer.json` contains version constraints in the

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

sections, the `VersionConstraintNormalizer` will ensure that

* all constraints are trimmed
* *and* constraints are separated by a comma (`,`)
* *or* constraints are separated by double-pipe with a single space before and after (` || `)
* *range* constraints are separated by a single space (` `)

:bulb: Find out more about version constraints at https://getcomposer.org/doc/articles/versions.md.

## 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 VersionConstraintNormalizer()
));
}

Expand Down
102 changes: 102 additions & 0 deletions src/Normalizer/VersionConstraintNormalizer.php
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 Localheinz\Json\Normalizer\NormalizerInterface;

final class VersionConstraintNormalizer implements NormalizerInterface
{
/**
* @var string[]
*/
private static $properties = [
'conflict',
'provide',
'replaces',
'require',
'require-dev',
];

/**
* @var array
*/
private static $map = [
'and' => [
'{\s*,\s*}',
',',
],
'or' => [
'{\s*\|\|?\s*}',
' || ',
],
'range' => [
'{\s+}',
' ',
],
];

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} = \array_map(function (string $versionConstraint) {
return $this->normalizeVersionConstraint($versionConstraint);
}, $packages);
}

return \json_encode($decoded);
}

private function normalizeVersionConstraint(string $versionConstraint): string
{
$normalized = $versionConstraint;

foreach (self::$map as list($pattern, $glue)) {
$split = \preg_split(
$pattern,
$normalized
);

$normalized = \implode(
$glue,
$split
);
}

return \trim($normalized);
}
}
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\VersionConstraintNormalizer;
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,
VersionConstraintNormalizer::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
Loading

0 comments on commit 45bafad

Please sign in to comment.