Provides a composer plugin for normalizing composer.json
.
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.
Run
$ composer global require localheinz/composer-normalize
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 ofcomposer.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
file
: Path to composer.json file (optional, defaults tocomposer.json
in working directory)
--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
The ComposerJsonNormalizer
composes normalizers provided by localheinz/json-normalizer
:
as well as the following normalizers provided by this package:
Localheinz\Composer\Normalize\Normalizer\BinNormalizer
Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer
Localheinz\Composer\Normalize\Normalizer\PackageHashNormalizer
Localheinz\Composer\Normalize\Normalizer\VersionConstraintNormalizer
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.
If composer.json
contains any configuration in the config
section,
the ConfigHashNormalizer
will sort the config
section by key in ascending order.
💡 Find out more about the config
section at https://getcomposer.org/doc/06-config.md.
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.
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 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.
Please have a look at CONTRIBUTING.md
.
Please have a look at CODE_OF_CONDUCT.md
.
This package is licensed using the MIT License.
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
.
localheinz/composer-normalize
is currently in use by FlintCI, see https://flintci.io/docs#composernormalize.