localheinz/composer-normalize

Provides a composer plugin for normalizing composer.json.

0.8.0 2018-04-24 13:26 UTC

README

Build Status Build status codecov Latest Stable Version Total Downloads

Provides a composer plugin for normalizing composer.json.

Motivation

If you have been working with composer on more than one project, you might have noticed that each composer.json ends up being structured differently.

I certainly have noticed, and rather than

  • ignoring it
  • manually structuring composer.json
  • asking others to structure composer.json

I decided to build something that structures composer.json in an automated fashion, but without changing the initial intent.

In my opinion, the advantages of using localheinz/composer-normalize are

  • no need to think (or argue) about where to add a new section
  • no need to think (or argue) about proper formatting
  • no need to worry about keeping items in a consistent order where they can't be kept in order by other means
  • can be used in a Continuous Integration environment

💡 If you are interested in finding out more before giving it a try, I have written a blog post about Normalizing composer.json.

Installation

Run

$ composer global require localheinz/composer-normalize

Usage

Run

$ composer normalize

to normalize composer.json in the working directory.

The NormalizeCommand provided by the NormalizePlugin within this package will

  • determine whether a composer.json exists
  • determine whether a composer.lock exists, and if so, whether it is up to date
  • use the ComposerJsonNormalizer to normalize the content of composer.json
  • format the normalized content (either as sniffed, or as specified using the --indent-size and --indent-style options)
  • write the normalized and formatted content of composer.json back to the file
  • update the hash in composer.lock if it exists and if an update is necessary

Arguments

  • file: Path to composer.json file (optional, defaults to composer.json in working directory)

Options

  • --dry-run: Show the results of normalizing, but do not modify any files
  • --indent-size: Indent size (an integer greater than 0); should be used with the --indent-style option
  • --indent-style: Indent style (one of "space", "tab"); should be used with the --indent-size option
  • --no-update-lock: Do not update lock file if it exists

Normalizers

The ComposerJsonNormalizer composes normalizers provided by localheinz/json-normalizer:

as well as the following normalizers provided by this package:

BinNormalizer

If composer.json contains an array of scripts in the bin section, the BinNormalizer will sort the elements of the bin section by value in ascending order.

💡 Find out more about the bin section at https://getcomposer.org/doc/04-schema.md#bin.

ConfigHashNormalizer

If composer.json contains any configuration in the

  • config
  • extra

sections, the ConfigHashNormalizer will sort the content of these sections by key in ascending order.

💡 Find out more about the config section at https://getcomposer.org/doc/06-config.md.

PackageHashNormalizer

If composer.json contains any configuration in the

  • conflict
  • provide
  • replace
  • require
  • require-dev
  • suggest

sections, the PackageHashNormalizer will sort the content of these sections.

💡 This transfers the behaviour from using the --sort-packages or sort-packages configuration flag to other sections. Find out more about 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
  • replace
  • require
  • require-dev

sections, the VersionConstraintNormalizer will ensure that

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

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

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

License

This package is licensed using the MIT License.

Credits

The algorithm for sorting packages in the PackageHashNormalizer has been adopted from Composer\Json\JsonManipulator::sortPackages() (originally licensed under MIT by Nils Adermann and Jordi Boggiano), which I initially contributed to composer/composer with composer/composer#3549 and composer/composer#3872.

Services

localheinz/composer-normalize is currently in use by FlintCI, see https://flintci.io/docs#composernormalize.