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

JBZoo / Composer-Diff / 9339764623

08 Feb 2024 09:20PM UTC coverage: 94.345%. Remained the same
9339764623

push

github

web-flow
Bump composer/composer from 2.6.6 to 2.7.0 (#34)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

317 of 336 relevant lines covered (94.35%)

74.11 hits per line

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

100.0
/src/Commands/DiffAction.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Composer-Diff.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Composer-Diff
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\ComposerDiff\Commands;
18

19
use JBZoo\Cli\CliCommand;
20
use JBZoo\ComposerDiff\Comparator;
21
use JBZoo\ComposerDiff\Renders\AbstractRender;
22
use Symfony\Component\Console\Input\InputOption;
23

24
/**
25
 * @psalm-suppress PropertyNotSetInConstructor
26
 */
27
final class DiffAction extends CliCommand
28
{
29
    /**
30
     * {@inheritDoc}
31
     */
32
    protected function configure(): void
33
    {
34
        $envs          = \implode(', ', [Comparator::ENV_BOTH, Comparator::ENV_PROD, Comparator::ENV_DEV]);
66✔
35
        $outputFormats = \implode(', ', [AbstractRender::CONSOLE, AbstractRender::MARKDOWN, AbstractRender::JSON]);
66✔
36

37
        $this
66✔
38
            ->setName('diff')
66✔
39
            ->setDescription('Show difference between two versions of composer.lock files')
66✔
40
            // File paths
66✔
41
            ->addOption(
66✔
42
                'source',
66✔
43
                null,
66✔
44
                InputOption::VALUE_REQUIRED,
66✔
45
                'The file, git ref, or git ref with filename to compare FROM',
66✔
46
                'HEAD:composer.lock',
66✔
47
            )
66✔
48
            ->addOption(
66✔
49
                'target',
66✔
50
                null,
66✔
51
                InputOption::VALUE_REQUIRED,
66✔
52
                'The file, git ref, or git ref with filename to compare TO',
66✔
53
                './composer.lock',
66✔
54
            )
66✔
55
            // Options
66✔
56
            ->addOption(
66✔
57
                'env',
66✔
58
                null,
66✔
59
                InputOption::VALUE_REQUIRED,
66✔
60
                "Show only selected environment. Available options: <info>{$envs}</info>",
66✔
61
                Comparator::ENV_BOTH,
66✔
62
            )
66✔
63
            ->addOption(
66✔
64
                'output',
66✔
65
                null,
66✔
66
                InputOption::VALUE_REQUIRED,
66✔
67
                "Output format. Available options: <info>{$outputFormats}</info>",
66✔
68
                AbstractRender::CONSOLE,
66✔
69
            )
66✔
70
            ->addOption('no-links', null, InputOption::VALUE_NONE, 'Hide all links in tables')
66✔
71
            ->addOption('strict', null, InputOption::VALUE_NONE, 'Return exit code if you have any difference');
66✔
72

73
        parent::configure();
66✔
74
    }
75

76
    /**
77
     * {@inheritDoc}
78
     */
79
    protected function executeAction(): int
80
    {
81
        $sourcePath   = $this->getOptString('source');
66✔
82
        $targetPath   = $this->getOptString('target');
66✔
83
        $outputFormat = \strtolower($this->getOptString('output'));
66✔
84
        $env          = \strtolower($this->getOptString('env'));
66✔
85
        $isStrictMode = $this->getOptBool('strict');
66✔
86

87
        $params = [
66✔
88
            'show-links'  => !$this->getOptBool('no-links'),
66✔
89
            'strict-mode' => $isStrictMode,
66✔
90
        ];
66✔
91

92
        $fullChangeLog = Comparator::compare($sourcePath, $targetPath);
66✔
93

94
        $errorCode = 0;
66✔
95

96
        if (\in_array($env, [Comparator::ENV_BOTH, Comparator::ENV_PROD], true)) {
66✔
97
            $changeLog = $fullChangeLog[Comparator::ENV_PROD];
60✔
98
            $this->renderOutput($outputFormat, $changeLog, Comparator::ENV_PROD, $params);
60✔
99
            if ($isStrictMode && \count($changeLog) > 0) {
60✔
100
                $errorCode++;
12✔
101
            }
102
        }
103

104
        if (\in_array($env, [Comparator::ENV_BOTH, Comparator::ENV_DEV], true)) {
66✔
105
            $changeLog = $fullChangeLog[Comparator::ENV_DEV];
48✔
106
            $this->renderOutput($outputFormat, $changeLog, Comparator::ENV_DEV, $params);
48✔
107
            if ($isStrictMode && \count($changeLog) > 0) {
48✔
108
                $errorCode++;
12✔
109
            }
110
        }
111

112
        return $errorCode;
66✔
113
    }
114

115
    private function renderOutput(string $outputFormat, array $fullChangeLog, string $env, array $params): void
116
    {
117
        AbstractRender::factory($outputFormat, $params)
66✔
118
            ->setFullChangeLog($fullChangeLog)
66✔
119
            ->setEnv($env)
66✔
120
            ->render($this->outputMode->getOutput());
66✔
121
    }
122
}
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

© 2025 Coveralls, Inc