GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UpdateHandler::getOutput()   C
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 70
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 52
cts 52
cp 1
rs 6.4909
c 0
b 0
f 0
cc 8
eloc 45
nc 11
nop 2
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the composer-changelogs project.
5
 *
6
 * (c) Loïck Piera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pyrech\ComposerChangelogs\OperationHandler;
13
14
use Composer\DependencyResolver\Operation\OperationInterface;
15
use Composer\DependencyResolver\Operation\UpdateOperation;
16
use Composer\Package\Version\VersionParser;
17
use Composer\Semver\Comparator;
18
use Pyrech\ComposerChangelogs\UrlGenerator\UrlGenerator;
19
use Pyrech\ComposerChangelogs\Version;
20
21
class UpdateHandler implements OperationHandler
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 14
    public function supports(OperationInterface $operation)
27
    {
28 14
        return $operation instanceof UpdateOperation;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 20
    public function extractSourceUrl(OperationInterface $operation)
35
    {
36 20
        if (!($operation instanceof UpdateOperation)) {
37 2
            throw new \LogicException('Operation should be an instance of UpdateOperation');
38
        }
39
40 18
        return $operation->getTargetPackage()->getSourceUrl();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 22
    public function getOutput(OperationInterface $operation, UrlGenerator $urlGenerator = null)
47
    {
48 22
        if (!($operation instanceof UpdateOperation)) {
49 2
            throw new \LogicException('Operation should be an instance of UpdateOperation');
50
        }
51
52 20
        $output = [];
53
54 20
        $initialPackage = $operation->getInitialPackage();
55 20
        $targetPackage = $operation->getTargetPackage();
56
57 20
        $versionFrom = new Version(
58 20
            $initialPackage->getVersion(),
59 20
            $initialPackage->getPrettyVersion(),
60 20
            method_exists($initialPackage, 'getFullPrettyVersion') // This method was added after composer v1.0.0-alpha10
61 20
                ? $initialPackage->getFullPrettyVersion()
62 20
                : VersionParser::formatVersion($initialPackage)
63 20
        );
64 20
        $versionTo = new Version(
65 20
            $targetPackage->getVersion(),
66 20
            $targetPackage->getPrettyVersion(),
67 20
            method_exists($targetPackage, 'getFullPrettyVersion') // This method was added after composer v1.0.0-alpha10
68 20
                ? $targetPackage->getFullPrettyVersion()
69 20
                : VersionParser::formatVersion($targetPackage)
70 20
        );
71
72 20
        $action = 'updated';
73
74 20
        if (Comparator::greaterThan($versionFrom->getName(), $versionTo->getName())) {
75 2
            $action = 'downgraded';
76 2
        }
77
78 20
        $output[] = sprintf(
79 20
            ' - <fg=green>%s</fg=green> %s from <fg=yellow>%s</fg=yellow> to <fg=yellow>%s</fg=yellow>',
80 20
            $initialPackage->getName(),
81 20
            $action,
82 20
            $versionFrom->getPretty(),
83 20
            $versionTo->getPretty()
84 20
        );
85
86 20
        if ($urlGenerator) {
87 16
            $compareUrl = $urlGenerator->generateCompareUrl(
88 16
                $initialPackage->getSourceUrl(),
89 16
                $versionFrom,
90 16
                $targetPackage->getSourceUrl(),
91
                $versionTo
92 16
            );
93
94 16
            if (!empty($compareUrl)) {
95 14
                $output[] = sprintf(
96 14
                    '   See changes: %s',
97
                    $compareUrl
98 14
                );
99 14
            }
100
101 16
            $releaseUrl = $urlGenerator->generateReleaseUrl(
102 16
                $this->extractSourceUrl($operation),
103
                $versionTo
104 16
            );
105
106 16
            if (!empty($releaseUrl)) {
107 14
                $output[] = sprintf(
108 14
                    '   Release notes: %s',
109
                    $releaseUrl
110 14
                );
111 14
            }
112 16
        }
113
114 20
        return $output;
115
    }
116
}
117