Completed
Push — master ( 6343d0...45320b )
by Bernhard
06:10
created

ChainVersioner   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 61.02 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 36
loc 59
ccs 15
cts 20
cp 0.75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseVersion() 17 17 4
A updateVersion() 19 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the webmozart/json package.
5
 *
6
 * (c) Bernhard Schussek <[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 Puli\Manager\Json;
13
14
use stdClass;
15
use Webmozart\Json\Versioning\CannotParseVersionException;
16
use Webmozart\Json\Versioning\CannotUpdateVersionException;
17
use Webmozart\Json\Versioning\JsonVersioner;
18
19
/**
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24
class ChainVersioner implements JsonVersioner
25
{
26
    /**
27
     * @var JsonVersioner[]
28
     */
29
    private $versioners;
30
31
    /**
32
     * @param JsonVersioner[] $versioners
33
     */
34 26
    public function __construct(array $versioners)
35
    {
36 26
        $this->versioners = $versioners;
37 26
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 26 View Code Duplication
    public function parseVersion(stdClass $jsonData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        /** @var CannotParseVersionException $firstException */
45 26
        $firstException = null;
46
47 26
        foreach ($this->versioners as $versioner) {
48
            try {
49 26
                return $versioner->parseVersion($jsonData);
50 26
            } catch (CannotParseVersionException $e) {
51 26
                if (null === $firstException) {
52 26
                    $firstException = $e;
53
                }
54
            }
55
        }
56
57
        throw $firstException;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 26 View Code Duplication
    public function updateVersion(stdClass $jsonData, $version)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        /** @var CannotUpdateVersionException $firstException */
66 26
        $firstException = null;
67
68 26
        foreach ($this->versioners as $versioner) {
69
            try {
70 26
                $versioner->updateVersion($jsonData, $version);
71
72 26
                return;
73
            } catch (CannotUpdateVersionException $e) {
74
                if (null === $firstException) {
75
                    $firstException = $e;
76
                }
77
            }
78
        }
79
80
        throw $firstException;
81
    }
82
}
83