• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / version-bumper / 24195556183

09 Apr 2026 02:26PM UTC coverage: 87.893% (-0.6%) from 88.488%
24195556183

Pull #122

github

eliashaeussler
[BUGFIX] Update composer.lock file after composer.json has changed
Pull Request #122: [BUGFIX] Update composer.lock file after composer.json has changed

40 of 53 new or added lines in 1 file covered. (75.47%)

5 existing lines in 1 file now uncovered.

922 of 1049 relevant lines covered (87.89%)

4.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.05
/src/Command/BumpVersionCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/version-bumper".
7
 *
8
 * Copyright (C) 2024-2026 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\VersionBumper\Command;
25

26
use Composer\Command;
27
use Composer\Composer;
28
use Composer\Console\Application;
29
use CuyZ\Valinor;
30
use EliasHaeussler\VersionBumper\Config;
31
use EliasHaeussler\VersionBumper\Enum;
32
use EliasHaeussler\VersionBumper\Exception;
33
use EliasHaeussler\VersionBumper\Result;
34
use EliasHaeussler\VersionBumper\Version;
35
use GitElephant\Command\Caller;
36
use Symfony\Component\Console;
37
use Symfony\Component\Filesystem;
38

39
use function array_filter;
40
use function array_map;
41
use function count;
42
use function dirname;
43
use function getcwd;
44
use function implode;
45
use function is_string;
46
use function method_exists;
47
use function reset;
48
use function sprintf;
49
use function trim;
50

51
/**
52
 * BumpVersionCommand.
53
 *
54
 * @author Elias Häußler <elias@haeussler.dev>
55
 * @license GPL-3.0-or-later
56
 */
57
final class BumpVersionCommand extends Command\BaseCommand
58
{
59
    private readonly Version\VersionBumper $bumper;
60
    private readonly Config\ConfigReader $configReader;
61
    private readonly Version\VersionRangeDetector $versionRangeDetector;
62
    private readonly Version\VersionReleaser $releaser;
63
    private Console\Style\SymfonyStyle $io;
64

65
    public function __construct(
15✔
66
        ?Composer $composer = null,
67
        ?Caller\CallerInterface $caller = null,
68
    ) {
69
        if (null !== $composer) {
15✔
70
            $this->setComposer($composer);
1✔
71
        }
72

73
        parent::__construct('bump-version');
15✔
74

75
        $this->bumper = new Version\VersionBumper();
15✔
76
        $this->configReader = new Config\ConfigReader();
15✔
77
        $this->versionRangeDetector = new Version\VersionRangeDetector($caller);
15✔
78
        $this->releaser = new Version\VersionReleaser($caller);
15✔
79
    }
80

81
    protected function configure(): void
15✔
82
    {
83
        $this->setAliases(['bv']);
15✔
84
        $this->setDescription('Bump package version in specific files during release preparations');
15✔
85

86
        $this->addArgument(
15✔
87
            'range',
15✔
88
            Console\Input\InputArgument::OPTIONAL,
15✔
89
            sprintf(
15✔
90
                'Version range (one of "%s") or explicit version to bump in configured files',
15✔
91
                implode('", "', Enum\VersionRange::all()),
15✔
92
            ),
15✔
93
        );
15✔
94

95
        $this->addOption(
15✔
96
            'config',
15✔
97
            'c',
15✔
98
            Console\Input\InputOption::VALUE_REQUIRED,
15✔
99
            'Path to configuration file (JSON, YAML or PHP) with files in which to bump new versions',
15✔
100
            $this->readConfigFileFromRootPackage(),
15✔
101
        );
15✔
102
        $this->addOption(
15✔
103
            'release',
15✔
104
            'r',
15✔
105
            Console\Input\InputOption::VALUE_NONE,
15✔
106
            'Create a new Git tag after versions are bumped',
15✔
107
        );
15✔
108
        $this->addOption(
15✔
109
            'dry-run',
15✔
110
            null,
15✔
111
            Console\Input\InputOption::VALUE_NONE,
15✔
112
            'Do not perform any write operations, just calculate version bumps',
15✔
113
        );
15✔
114
        $this->addOption(
15✔
115
            'strict',
15✔
116
            null,
15✔
117
            Console\Input\InputOption::VALUE_NONE,
15✔
118
            'Fail if any unmatched file pattern is reported',
15✔
119
        );
15✔
120
    }
121

122
    protected function initialize(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): void
15✔
123
    {
124
        $this->io = new Console\Style\SymfonyStyle($input, $output);
15✔
125
    }
126

127
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
15✔
128
    {
129
        $rootPath = (string) getcwd();
15✔
130
        $rangeOrVersion = $input->getArgument('range');
15✔
131
        $configFile = $input->getOption('config') ?? $this->configReader->detectFile($rootPath);
15✔
132
        $release = $input->getOption('release');
15✔
133
        $dryRun = $input->getOption('dry-run');
15✔
134
        $strict = $input->getOption('strict');
15✔
135

136
        if (null === $configFile) {
15✔
137
            $this->io->error('Please provide a config file path using the --config option.');
1✔
138

139
            return self::INVALID;
1✔
140
        }
141

142
        if (Filesystem\Path::isRelative($configFile)) {
14✔
143
            $configFile = Filesystem\Path::makeAbsolute($configFile, $rootPath);
1✔
144
        } else {
145
            $rootPath = dirname($configFile);
13✔
146
        }
147

148
        try {
149
            $config = $this->configReader->readFromFile($configFile);
14✔
150

151
            // Override root path from config file
152
            if (null !== $config->rootPath()) {
13✔
153
                $rootPath = $config->rootPath();
13✔
154
            }
155

156
            $results = $this->bumpVersions($config, $rangeOrVersion, $rootPath, $dryRun);
13✔
157

158
            if (null === $results) {
8✔
159
                return self::FAILURE;
2✔
160
            }
161

162
            if (!$this->updateComposerLockIfNeeded($input, $output, $rootPath, $results)) {
6✔
NEW
163
                return self::FAILURE;
×
164
            }
165

166
            if ($release && !$this->releaseVersion($results, $rootPath, $config->releaseOptions(), $dryRun)) {
6✔
167
                return self::FAILURE;
5✔
168
            }
169
        } catch (Valinor\Mapper\MappingError $error) {
7✔
170
            $this->decorateMappingError($error, $configFile);
1✔
171

172
            return self::FAILURE;
1✔
173
        } catch (Exception\Exception $exception) {
6✔
174
            $this->io->error($exception->getMessage());
6✔
175

176
            return self::FAILURE;
6✔
177
        } finally {
178
            if ($dryRun) {
14✔
179
                $this->io->note('No write operations were performed (dry-run mode).');
7✔
180
            }
181
        }
182

183
        if ($strict) {
5✔
184
            foreach ($results as $versionBumpResult) {
1✔
185
                if ($versionBumpResult->hasUnmatchedReports()) {
1✔
186
                    return self::FAILURE;
1✔
187
                }
188
            }
189
        }
190

191
        return self::SUCCESS;
4✔
192
    }
193

194
    /**
195
     * @return list<Result\VersionBumpResult>|null
196
     *
197
     * @throws Exception\Exception
198
     */
199
    private function bumpVersions(
13✔
200
        Config\VersionBumperConfig $config,
201
        ?string $rangeOrVersion,
202
        string $rootPath,
203
        bool $dryRun,
204
    ): ?array {
205
        // Auto-detect version range from indicators
206
        if (null !== $rangeOrVersion) {
13✔
207
            $versionRange = Enum\VersionRange::tryFromInput($rangeOrVersion) ?? $rangeOrVersion;
9✔
208
        } elseif ([] !== $config->versionRangeIndicators()) {
4✔
209
            $versionRange = $this->versionRangeDetector->detect($rootPath, $config->versionRangeIndicators());
3✔
210
        } else {
211
            $this->io->error('Please provide a version range or explicit version to bump in configured files.');
1✔
212
            $this->io->block(
1✔
213
                'You can also enable auto-detection by adding version range indicators to your configuration file.',
1✔
214
                null,
1✔
215
                'fg=cyan',
1✔
216
                '💡 ',
1✔
217
            );
1✔
218

219
            return null;
1✔
220
        }
221

222
        // Exit early if version range detection fails
223
        if (null === $versionRange) {
11✔
224
            $this->io->error('Unable to auto-detect version range. Please provide a version range or explicit version instead.');
1✔
225

226
            return null;
1✔
227
        }
228

229
        $this->decorateAppliedPresets($config->presets());
10✔
230

231
        $results = $this->bumper->bump($config->filesToModify(), $rootPath, $versionRange, $dryRun);
10✔
232

233
        $this->decorateVersionBumpResults($results, $rootPath);
6✔
234

235
        return $results;
6✔
236
    }
237

238
    /**
239
     * @param list<Result\VersionBumpResult> $results
240
     *
241
     * @throws Exception\Exception
242
     */
243
    private function releaseVersion(array $results, string $rootPath, Config\ReleaseOptions $options, bool $dryRun): bool
2✔
244
    {
245
        $this->io->title('Release');
2✔
246

247
        try {
248
            $releaseResult = $this->releaser->release($results, $rootPath, $options, $dryRun);
2✔
249

250
            $this->decorateVersionReleaseResult($releaseResult);
1✔
251

252
            return true;
1✔
253
        } catch (Exception\Exception $exception) {
1✔
254
            throw $exception;
1✔
UNCOV
255
        } catch (\Exception $exception) {
×
256
            $this->io->error('Git error during release: '.$exception->getMessage());
×
257
        }
258

UNCOV
259
        return false;
×
260
    }
261

262
    /**
263
     * @param list<Config\Preset\Preset> $presets
264
     */
265
    private function decorateAppliedPresets(array $presets): void
10✔
266
    {
267
        if ([] === $presets || !$this->io->isVerbose()) {
10✔
268
            return;
9✔
269
        }
270

271
        $this->io->title('Applied presets');
1✔
272

273
        $this->io->listing(
1✔
274
            array_map(
1✔
275
                static fn (Config\Preset\Preset $preset) => sprintf(
1✔
276
                    '%s <fg=gray>(%s)</>',
1✔
277
                    $preset::getDescription(),
1✔
278
                    $preset::getIdentifier(),
1✔
279
                ),
1✔
280
                $presets,
1✔
281
            ),
1✔
282
        );
1✔
283
    }
284

285
    /**
286
     * @param list<Result\VersionBumpResult> $results
287
     */
288
    private function decorateVersionBumpResults(array $results, string $rootPath): void
6✔
289
    {
290
        $titleDisplayed = false;
6✔
291

292
        foreach ($results as $result) {
6✔
293
            if (!$result->hasOperations()) {
6✔
294
                continue;
4✔
295
            }
296

297
            $path = $result->file()->path();
6✔
298
            $groupedOperations = $result->groupedOperations();
6✔
299
            $hasOnlySkippedOperations = [] === array_filter(
6✔
300
                $groupedOperations,
6✔
301
                static fn (array $operations) => Enum\OperationState::Skipped !== reset($operations)->state(),
6✔
302
            );
6✔
303

304
            if (Filesystem\Path::isAbsolute($path)) {
6✔
UNCOV
305
                $path = Filesystem\Path::makeRelative($path, $rootPath);
×
306
            }
307

308
            if (!$titleDisplayed) {
6✔
309
                $this->io->title('Bumped versions');
6✔
310
                $titleDisplayed = true;
6✔
311
            }
312

313
            $this->io->section($path);
6✔
314

315
            foreach ($groupedOperations as $operations) {
6✔
316
                $operation = reset($operations);
6✔
317
                $numberOfOperations = count($operations);
6✔
318
                $state = $operation->state();
6✔
319

320
                if (Enum\OperationState::Skipped === $state && !$hasOnlySkippedOperations) {
6✔
321
                    continue;
3✔
322
                }
323

324
                $message = match ($state) {
6✔
325
                    Enum\OperationState::Modified => sprintf(
6✔
326
                        '✅ Bumped version from "%s" to "%s"',
6✔
327
                        $operation->source()?->full() ?? '',
6✔
328
                        $operation->target()?->full() ?? '',
6✔
329
                    ),
6✔
330
                    Enum\OperationState::Skipped => '⏩ Skipped file due to unmodified contents',
4✔
331
                    Enum\OperationState::Unmatched => '❓ Unmatched file pattern: '.$operation->pattern()->original(),
4✔
332
                };
6✔
333

334
                if ($numberOfOperations > 1) {
6✔
335
                    $message .= sprintf(' (%dx)', $numberOfOperations);
3✔
336
                }
337

338
                $this->io->writeln($message);
6✔
339
            }
340
        }
341
    }
342

343
    private function decorateVersionReleaseResult(Result\VersionReleaseResult $result): void
1✔
344
    {
345
        $numberOfCommittedFiles = count($result->committedFiles());
1✔
346
        $releaseInformation = [
1✔
347
            sprintf('Added %d file%s.', $numberOfCommittedFiles, 1 !== $numberOfCommittedFiles ? 's' : ''),
1✔
348
            sprintf('Committed: <info>%s</info>', $result->commitMessage()),
1✔
349
        ];
1✔
350

351
        if (null !== $result->commitId()) {
1✔
UNCOV
352
            $releaseInformation[] = sprintf('Commit hash: %s', $result->commitId());
×
353
        }
354

355
        $releaseInformation[] = sprintf('Tagged: <info>%s</info>', $result->tagName());
1✔
356

357
        $this->io->listing($releaseInformation);
1✔
358
    }
359

360
    private function decorateMappingError(Valinor\Mapper\MappingError $error, string $configFile): void
1✔
361
    {
362
        $errorMessages = [];
1✔
363
        $errors = $error->messages()->errors();
1✔
364

365
        $this->io->error(
1✔
366
            sprintf('The config file "%s" is invalid.', $configFile),
1✔
367
        );
1✔
368

369
        foreach ($errors as $propertyError) {
1✔
370
            $errorMessages[] = sprintf('%s: %s', $propertyError->path(), $propertyError->toString());
1✔
371
        }
372

373
        $this->io->listing($errorMessages);
1✔
374
    }
375

376
    private function readConfigFileFromRootPackage(): ?string
15✔
377
    {
378
        $composer = $this->getComposerInstance();
15✔
379

380
        if (null === $composer) {
15✔
381
            return null;
15✔
382
        }
383

384
        $extra = $composer->getPackage()->getExtra();
1✔
385
        /* @phpstan-ignore offsetAccess.nonOffsetAccessible */
386
        $configFile = $extra['version-bumper']['config-file'] ?? null;
1✔
387

388
        if (is_string($configFile) && '' !== trim($configFile)) {
1✔
389
            return $configFile;
1✔
390
        }
391

UNCOV
392
        return null;
×
393
    }
394

395
    /**
396
     * @param list<Result\VersionBumpResult> $results
397
     */
398
    private function updateComposerLockIfNeeded(
6✔
399
        Console\Input\InputInterface $input,
400
        Console\Output\OutputInterface $output,
401
        string $rootPath,
402
        array &$results,
403
    ): bool {
404
        $this->resetComposer();
6✔
405

406
        $composer = $this->getComposerInstance();
6✔
407
        $locker = $composer?->getLocker();
6✔
408

409
        if (null === $locker || !$locker->isLocked() || $locker->isFresh()) {
6✔
410
            return true;
5✔
411
        }
412

413
        $this->io->title('File integrity');
1✔
414

415
        $application = new Application();
1✔
416
        $application->setAutoExit(false);
1✔
417

418
        $commandOutput = new Console\Output\BufferedOutput(
1✔
419
            $output->getVerbosity(),
1✔
420
            $output->isDecorated(),
1✔
421
            $output->getFormatter(),
1✔
422
        );
1✔
423

424
        $parameters = [
1✔
425
            'command' => 'update',
1✔
426
            '--ignore-platform-reqs' => true,
1✔
427
            '--lock' => true,
1✔
428
            '--no-autoloader' => true,
1✔
429
            '--working-dir' => $rootPath,
1✔
430
        ];
1✔
431

432
        if ($input->hasParameterOption('--no-ansi')) {
1✔
NEW
433
            $parameters['--no-ansi'] = $input->getParameterOption('--no-ansi');
×
434
        }
435

436
        if ($input->hasParameterOption('--no-plugins')) {
1✔
NEW
437
            $parameters['--no-plugins'] = $input->getParameterOption('--no-plugins');
×
438
        }
439

440
        if ($input->hasParameterOption('--no-scripts')) {
1✔
NEW
441
            $parameters['--no-scripts'] = $input->getParameterOption('--no-scripts');
×
442
        }
443

444
        try {
445
            $result = $application->run(new Console\Input\ArrayInput($parameters), $commandOutput);
1✔
NEW
446
        } catch (\Exception $exception) {
×
NEW
447
            $this->io->write($commandOutput->fetch());
×
NEW
448
            $this->io->error($exception->getMessage());
×
449

NEW
450
            return false;
×
451
        }
452

453
        if (self::SUCCESS === $result) {
1✔
454
            $this->io->writeln('✅ Updated <info>composer.lock</info> file (content hash change)');
1✔
455

456
            // Add modified lock file to results array to include it in subsequent release operations
457
            foreach ($results as $result) {
1✔
458
                if ($result->file()->fullPath($rootPath) === $composer->getConfig()->getConfigSource()->getName()) {
1✔
459
                    $results[] = new Result\VersionBumpResult(
1✔
460
                        new Config\FileToModify($locker->getJsonFile()->getPath()),
1✔
461
                        $result->operations(),
1✔
462
                    );
1✔
463

464
                    break;
1✔
465
                }
466
            }
467

468
            return true;
1✔
469
        }
470

NEW
471
        if ($output->isVerbose()) {
×
NEW
472
            $this->io->write($commandOutput->fetch());
×
473
        }
474

NEW
475
        $this->io->error('An error occurred while updating the composer.lock file.');
×
476

NEW
477
        return false;
×
478
    }
479

480
    private function getComposerInstance(): ?Composer
15✔
481
    {
482
        // Composer >= 2.3
483
        if (method_exists($this, 'tryComposer')) {
15✔
484
            return $this->tryComposer();
15✔
485
        }
486

487
        // Composer < 2.3
NEW
488
        return $this->getComposer(false);
×
489
    }
490
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc