ergebnis / composer-normalize

Compare 78a2d7c ... +249 ... 865c9ae

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 4 of 104 files from the diff.
Newly tracked file
src/Version.php created.
Other files ignored by Codecov
.php-cs-fixer.php has changed.
.gitignore has changed.
phpstan.neon has changed.
CHANGELOG.md has changed.
LICENSE.md has changed.
README.md has changed.
Makefile has changed.
psalm.xml has changed.
phive.xml was deleted.
composer.json has changed.
.gitattributes has changed.
composer.lock has changed.

@@ -3,7 +3,7 @@
Loading
3 3
declare(strict_types=1);
4 4
5 5
/**
6 -
 * Copyright (c) 2018-2020 Andreas Möller
6 +
 * Copyright (c) 2018-2021 Andreas Möller
7 7
 *
8 8
 * For the full copyright and license information, please view
9 9
 * the LICENSE.md file that was distributed with this source code.
@@ -18,6 +18,7 @@
Loading
18 18
use Composer\Factory;
19 19
use Composer\IO;
20 20
use Ergebnis\Composer\Normalize\Exception;
21 +
use Ergebnis\Composer\Normalize\Version;
21 22
use Ergebnis\Json\Normalizer;
22 23
use Localheinz\Diff;
23 24
use Symfony\Component\Console;
@@ -27,14 +28,6 @@
Loading
27 28
 */
28 29
final class NormalizeCommand extends Command\BaseCommand
29 30
{
30 -
    /**
31 -
     * @var array<string, string>
32 -
     */
33 -
    private static $indentStyles = [
34 -
        'space' => ' ',
35 -
        'tab' => "\t",
36 -
    ];
37 -
38 31
    private $factory;
39 32
40 33
    private $normalizer;
@@ -90,9 +83,15 @@
Loading
90 83
                Console\Input\InputOption::VALUE_REQUIRED,
91 84
                \sprintf(
92 85
                    'Indent style (one of "%s"); should be used with the --indent-size option',
93 -
                    \implode('", "', \array_keys(self::$indentStyles))
86 +
                    \implode('", "', \array_keys(Normalizer\Format\Indent::CHARACTERS))
94 87
                )
95 88
            ),
89 +
            new Console\Input\InputOption(
90 +
                'no-check-lock',
91 +
                null,
92 +
                Console\Input\InputOption::VALUE_NONE,
93 +
                'Do not check if lock file is up to date'
94 +
            ),
96 95
            new Console\Input\InputOption(
97 96
                'no-update-lock',
98 97
                null,
@@ -106,8 +105,18 @@
Loading
106 105
    {
107 106
        $io = $this->getIO();
108 107
108 +
        $io->write([
109 +
            \sprintf(
110 +
                'Running %s.',
111 +
                Version::long()
112 +
            ),
113 +
            '',
114 +
        ]);
115 +
116 +
        $indent = null;
117 +
109 118
        try {
110 -
            $indent = self::indentFrom($input);
119 +
            $indentFromInput = self::indentFromInput($input);
111 120
        } catch (\RuntimeException $exception) {
112 121
            $io->writeError(\sprintf(
113 122
                '<error>%s</error>',
@@ -117,9 +126,14 @@
Loading
117 126
            return 1;
118 127
        }
119 128
129 +
        if (null !== $indentFromInput) {
130 +
            $indent = $indentFromInput;
131 +
        }
132 +
120 133
        $composerFile = $input->getArgument('file');
121 134
122 135
        if (null === $composerFile) {
136 +
            /** @var string $composerFile */
123 137
            $composerFile = Factory::getComposerFile();
124 138
        }
125 139
@@ -128,6 +142,25 @@
Loading
128 142
            $composerFile
129 143
        );
130 144
145 +
        try {
146 +
            $indentFromExtra = self::indentFromExtra($composer->getPackage()->getExtra());
147 +
        } catch (\RuntimeException $exception) {
148 +
            $io->writeError(\sprintf(
149 +
                '<error>%s</error>',
150 +
                $exception->getMessage()
151 +
            ));
152 +
153 +
            return 1;
154 +
        }
155 +
156 +
        if (null !== $indentFromExtra) {
157 +
            $indent = $indentFromExtra;
158 +
        }
159 +
160 +
        if (null !== $indentFromInput && null !== $indentFromExtra) {
161 +
            $io->write('<warning>Configuration provided via options and composer extra. Using configuration from composer extra.</warning>');
162 +
        }
163 +
131 164
        if (false === $input->getOption('dry-run') && !\is_writable($composerFile)) {
132 165
            $io->writeError(\sprintf(
133 166
                '<error>%s is not writable.</error>',
@@ -139,7 +172,7 @@
Loading
139 172
140 173
        $locker = $composer->getLocker();
141 174
142 -
        if ($locker->isLocked() && !$locker->isFresh()) {
175 +
        if (false === $input->getOption('no-check-lock') && $locker->isLocked() && !$locker->isFresh()) {
143 176
            $io->writeError('<error>The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update --lock`.</error>');
144 177
145 178
            return 1;
@@ -248,13 +281,9 @@
Loading
248 281
    }
249 282
250 283
    /**
251 -
     * @param Console\Input\InputInterface $input
252 -
     *
253 284
     * @throws \RuntimeException
254 -
     *
255 -
     * @return null|Normalizer\Format\Indent
256 285
     */
257 -
    private static function indentFrom(Console\Input\InputInterface $input): ?Normalizer\Format\Indent
286 +
    private static function indentFromInput(Console\Input\InputInterface $input): ?Normalizer\Format\Indent
258 287
    {
259 288
        /** @var null|string $indentSize */
260 289
        $indentSize = $input->getOption('indent-size');
@@ -273,7 +302,7 @@
Loading
273 302
        if (null === $indentStyle) {
274 303
            throw new \RuntimeException(\sprintf(
275 304
                'When using the indent-size option, an indent style (one of "%s") needs to be specified using the indent-style option.',
276 -
                \implode('", "', \array_keys(self::$indentStyles))
305 +
                \implode('", "', \array_keys(Normalizer\Format\Indent::CHARACTERS))
277 306
            ));
278 307
        }
279 308
@@ -284,26 +313,104 @@
Loading
284 313
            ));
285 314
        }
286 315
287 -
        try {
288 -
            $indent = Normalizer\Format\Indent::fromSizeAndStyle(
289 -
                (int) $indentSize,
316 +
        if (!\array_key_exists($indentStyle, Normalizer\Format\Indent::CHARACTERS)) {
317 +
            throw new \RuntimeException(\sprintf(
318 +
                'Indent style needs to be one of "%s", but "%s" is not.',
319 +
                \implode('", "', \array_keys(Normalizer\Format\Indent::CHARACTERS)),
290 320
                $indentStyle
291 -
            );
292 -
        } catch (Normalizer\Exception\InvalidIndentSizeException $exception) {
321 +
            ));
322 +
        }
323 +
324 +
        return Normalizer\Format\Indent::fromSizeAndStyle(
325 +
            (int) $indentSize,
326 +
            $indentStyle
327 +
        );
328 +
    }
329 +
330 +
    /**
331 +
     * @throws \RuntimeException
332 +
     */
333 +
    private static function indentFromExtra(array $extra): ?Normalizer\Format\Indent
334 +
    {
335 +
        if (!\array_key_exists('composer-normalize', $extra)) {
336 +
            return null;
337 +
        }
338 +
339 +
        $configuration = $extra['composer-normalize'];
340 +
341 +
        $requiredKeys = [
342 +
            'indent-size',
343 +
            'indent-style',
344 +
        ];
345 +
346 +
        if (!\is_array($configuration)) {
347 +
            throw new \RuntimeException(\sprintf(
348 +
                'Configuration in composer extra requires keys "%s" with corresponding values."',
349 +
                \implode('", "', $requiredKeys)
350 +
            ));
351 +
        }
352 +
353 +
        $missingKeys = \array_diff(
354 +
            $requiredKeys,
355 +
            \array_keys($configuration)
356 +
        );
357 +
358 +
        if ([] !== $missingKeys) {
359 +
            throw new \RuntimeException(\sprintf(
360 +
                'Configuration in composer extra requires keys "%s" with corresponding values."',
361 +
                \implode('", "', $requiredKeys)
362 +
            ));
363 +
        }
364 +
365 +
        $extraKeys = \array_diff(
366 +
            \array_keys($configuration),
367 +
            $requiredKeys
368 +
        );
369 +
370 +
        if ([] !== $extraKeys) {
293 371
            throw new \RuntimeException(\sprintf(
294 -
                'Indent size needs to be an integer greater than %d, but "%s" is not.',
295 -
                $exception->minimumSize(),
296 -
                $exception->size()
372 +
                'Configuration in composer extra does not allow extra keys "%s"."',
373 +
                \implode('", "', $extraKeys)
297 374
            ));
298 -
        } catch (Normalizer\Exception\InvalidIndentStyleException $exception) {
375 +
        }
376 +
377 +
        $indentSize = $configuration['indent-size'];
378 +
379 +
        if (!\is_int($indentSize)) {
380 +
            throw new \RuntimeException(\sprintf(
381 +
                'Indent size needs to be an integer, got %s instead.',
382 +
                \gettype($indentSize)
383 +
            ));
384 +
        }
385 +
386 +
        if (1 > $indentSize) {
387 +
            throw new \RuntimeException(\sprintf(
388 +
                'Indent size needs to be an integer greater than 0, but %d is not.',
389 +
                $indentSize
390 +
            ));
391 +
        }
392 +
393 +
        $indentStyle = $configuration['indent-style'];
394 +
395 +
        if (!\is_string($indentStyle)) {
396 +
            throw new \RuntimeException(\sprintf(
397 +
                'Indent style needs to be a string, got %s instead.',
398 +
                \gettype($indentStyle)
399 +
            ));
400 +
        }
401 +
402 +
        if (!\array_key_exists($indentStyle, Normalizer\Format\Indent::CHARACTERS)) {
299 403
            throw new \RuntimeException(\sprintf(
300 404
                'Indent style needs to be one of "%s", but "%s" is not.',
301 -
                \implode('", "', \array_keys(self::$indentStyles)),
405 +
                \implode('", "', \array_keys(Normalizer\Format\Indent::CHARACTERS)),
302 406
                $indentStyle
303 407
            ));
304 408
        }
305 409
306 -
        return $indent;
410 +
        return Normalizer\Format\Indent::fromSizeAndStyle(
411 +
            $indentSize,
412 +
            $indentStyle
413 +
        );
307 414
    }
308 415
309 416
    private static function showValidationErrors(IO\IOInterface $io, string ...$errors): void
@@ -354,13 +461,7 @@
Loading
354 461
    /**
355 462
     * @see https://getcomposer.org/doc/03-cli.md#update
356 463
     *
357 -
     * @param Console\Application            $application
358 -
     * @param Console\Output\OutputInterface $output
359 -
     * @param string                         $workingDirectory
360 -
     *
361 464
     * @throws \Exception
362 -
     *
363 -
     * @return int
364 465
     */
365 466
    private static function updateLockerInWorkingDirectory(
366 467
        Console\Application $application,
@@ -370,6 +471,7 @@
Loading
370 471
        return $application->run(
371 472
            new Console\Input\ArrayInput([
372 473
                'command' => 'update',
474 +
                '--ignore-platform-reqs' => true,
373 475
                '--lock' => true,
374 476
                '--no-autoloader' => true,
375 477
                '--no-plugins' => true,

@@ -3,7 +3,7 @@
Loading
3 3
declare(strict_types=1);
4 4
5 5
/**
6 -
 * Copyright (c) 2018-2020 Andreas Möller
6 +
 * Copyright (c) 2018-2021 Andreas Möller
7 7
 *
8 8
 * For the full copyright and license information, please view
9 9
 * the LICENSE.md file that was distributed with this source code.

@@ -3,7 +3,7 @@
Loading
3 3
declare(strict_types=1);
4 4
5 5
/**
6 -
 * Copyright (c) 2018-2020 Andreas Möller
6 +
 * Copyright (c) 2018-2021 Andreas Möller
7 7
 *
8 8
 * For the full copyright and license information, please view
9 9
 * the LICENSE.md file that was distributed with this source code.

@@ -0,0 +1,50 @@
Loading
1 +
<?php
2 +
3 +
declare(strict_types=1);
4 +
5 +
/**
6 +
 * Copyright (c) 2018-2021 Andreas Möller
7 +
 *
8 +
 * For the full copyright and license information, please view
9 +
 * the LICENSE.md file that was distributed with this source code.
10 +
 *
11 +
 * @see https://github.com/ergebnis/composer-normalize
12 +
 */
13 +
14 +
namespace Ergebnis\Composer\Normalize;
15 +
16 +
/**
17 +
 * @internal
18 +
 */
19 +
final class Version
20 +
{
21 +
    /**
22 +
     * @see https://github.com/box-project/box/blob/master/doc/configuration.md#pretty-git-tag-placeholder-git
23 +
     *
24 +
     * @var string
25 +
     */
26 +
    private static $version = '@git@';
27 +
28 +
    public static function long(): string
29 +
    {
30 +
        $name = 'ergebnis/composer-normalize';
31 +
        $attribution = 'by <info>Andreas Möller</info> and contributors';
32 +
33 +
        $version = self::$version;
34 +
35 +
        if ('@' . 'git@' === $version) {
36 +
            return \sprintf(
37 +
                '<info>%s</info> %s',
38 +
                $name,
39 +
                $attribution
40 +
            );
41 +
        }
42 +
43 +
        return \sprintf(
44 +
            '<info>%s</info> %s %s',
45 +
            $name,
46 +
            $version,
47 +
            $attribution
48 +
        );
49 +
    }
50 +
}

Learn more Showing 3 files with coverage changes found.

Changes in src/NormalizePlugin.php
-1
Loading file...
New file src/Version.php
New
Loading file...
Changes in src/Command/NormalizeCommand.php
-10
-3
Loading file...

251 Commits

Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 1 contexual commits Hiding 2 contexual commits
Hiding 1 contexual commits