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

JBZoo / Composer-Diff / 6669924894

16 Aug 2023 06:22PM UTC coverage: 94.294%. Remained the same
6669924894

push

github

web-flow
Upgrade `jbzoo/cli:^7.1.1` (#26)

1 of 1 new or added line in 1 file covered. (100.0%)

314 of 333 relevant lines covered (94.29%)

49.24 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]);
44✔
35
        $outputFormats = \implode(', ', [AbstractRender::CONSOLE, AbstractRender::MARKDOWN, AbstractRender::JSON]);
44✔
36

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

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

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

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

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

94
        $errorCode = 0;
44✔
95

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

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

112
        return $errorCode;
44✔
113
    }
114

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