Passed
Push — master ( aa9b83...655a42 )
by Allan
01:58
created

VersionCommand::printException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Commands;
7
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
use Vaimo\ComposerChangelogs\Factories;
12
13
class VersionCommand extends \Composer\Command\BaseCommand
14
{
15
    protected function configure()
16
    {
17
        $this->setName('changelog:version');
18
19
        $this->setDescription(
20
            'Display version information from changelog. <comment>[default: latest stable]</comment>'
21
        );
22
23
        $this->addArgument(
24
            'name',
25
            \Symfony\Component\Console\Input\InputArgument::OPTIONAL,
26
            'Targeted package name. Default: root package'
27
        );
28
29
        $this->addOption(
30
            '--from-source',
31
            null,
32
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
33
            'Extract configuration from vendor package instead of using global installation data'
34
        );
35
36
        $this->addOption(
37
            '--format',
38
            null,
39
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
40
            'Format of the output (regex)'
41
        );
42
43
        $this->addOption(
44
            '--segments',
45
            null,
46
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
47
            'Number of segments of the version to return. <comment>[default: all segments]</comment>'
48
        );
49
        
50
        $this->addOption(
51
            '--upcoming',
52
            null,
53
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
54
            'Show upcoming version (if there is one)'
55
        );
56
57
        $this->addOption(
58
            '--branch',
59
            null,
60
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
61
            'Match release branch (if provided in changelog item)'
62
        );
63
64
        $this->addOption(
65
            '--tip',
66
            null,
67
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
68
            'Show LATEST version (might be latest release, might be upcoming)'
69
        );
70
    }
71
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $packageName = $input->getArgument('name');
75
        $fromSource = $input->getOption('from-source');
76
        $format = $input->getOption('format');
77
        $branch = $input->getOption('branch');
78
        $segmentsCount = $input->getOption('segments');
79
80
        $showUpcoming = $input->getOption('upcoming');
81
        $showTip = $input->getOption('tip');
82
        
83
        $chLogRepoFactory = new Factories\ChangelogRepositoryFactory(
0 ignored issues
show
Bug introduced by
The type Vaimo\ComposerChangelogs...ngelogRepositoryFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
            $this->getComposer(),
85
            $output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL ? $output : null
86
        );
87
        
88
        $chLogRepo = $chLogRepoFactory->create($fromSource);
89
90
        $changelog = $chLogRepo->getByPackageName($packageName);
91
92
        if ($changelog === null) {
93
            return 1;
94
        }
95
96
        $releases = $changelog->getReleases();
97
        
98
        $releaseResolver = new \Vaimo\ComposerChangelogs\Resolvers\ChangelogReleaseResolver();
99
100
        $version = key($changelog);
101
102
        if (!$showTip) {
103
            $version = $releaseResolver->resolveLatestVersionedRelease($releases, $branch);
104
105
            if ($showUpcoming) {
106
                $version = $releaseResolver->resolveUpcomingRelease($releases, $branch);
107
            }
108
        }
109
110
        if (!$version) {
111
            return 0;
112
        }
113
114
        $versionResolver = new \Vaimo\ComposerChangelogs\Resolvers\VersionResolver();
115
116
        $version = $versionResolver->resolveValidVersion($version);
117
            
118
        if ($format == 'regex') {
119
            $version = preg_quote($version);
120
        }
121
        
122
        if ($segmentsCount) {
123
            $version = implode('.', array_slice(explode('.', $version), 0, $segmentsCount));
0 ignored issues
show
Bug introduced by
$segmentsCount of type string|string[]|true is incompatible with the type integer expected by parameter $length of array_slice(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
            $version = implode('.', array_slice(explode('.', $version), 0, /** @scrutinizer ignore-type */ $segmentsCount));
Loading history...
124
        }
125
        
126
        $output->writeln($version);
127
        
128
        return 0;
129
    }
130
}
131