Failed Conditions
Pull Request — master (#7)
by Mickael
01:14
created

PackageCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 27.18 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 28
loc 103
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getByName() 0 7 2
A getByNamespace() 0 7 2
A hasByName() 0 4 1
A hasByNamespace() 0 4 1
A offsetSet() 0 8 2
A getIndexedByName() 14 14 4
A getIndexedByNamespace() 14 14 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
 * Author: mickael Louzet @micklouzet
5
 * File: Package.php
6
 * Created: 07/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockParser\Package;
12
13
use ComposerLockParser\Package\Package;
14
use ArrayObject;
15
16
class PackageCollection extends ArrayObject
17
{
18
19
    /** @var array */
20
    private $indexedBy;
21
22
    /**
23
     * @param string $name
24
     *
25
     * @return Package
26
     */
27
    public function getByName(string $name): Package
28
    {
29
        if (!$this->hasByName($name)) {
30
            throw new \UnexpectedValueException(sprintf('Sorry, package %s not found !', $name));
31
        }
32
        return $this->getIndexedByName()[$name];
33
    }
34
35
    /**
36
     * @param string $namespace
37
     *
38
     * @return Package
39
     */
40
    public function getByNamespace(string $namespace): Package
41
    {
42
        if (!$this->hasByNamespace($namespace)) {
43
            throw new \UnexpectedValueException(sprintf('Sorry, namespace %s not found !', $namespace));
44
        }
45
        return $this->getIndexedByNamespace()[$namespace];
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return bool
52
     */
53
    public function hasByName(string $name): bool
54
    {
55
        return array_key_exists($name, $this->getIndexedByName());
56
    }
57
58
    /**
59
     * @param string $namespace
60
     *
61
     * @return bool
62
     */
63
    public function hasByNamespace($namespace): bool
64
    {
65
        return array_key_exists($namespace, $this->getIndexedByNamespace());
66
    }
67
68
    /**
69
     * @param mixed $index
70
     * @param mixed $package
71
     *
72
     * @return void
73
     */
74
    public function offsetSet($index, $package): void
75
    {
76
        if ($package instanceof Package) {
77
            $this->indexedBy['name'][$package->getName()] = $package;
78
            $this->indexedBy['namespace'][$package->getNamespace()] = $package;
79
        }
80
        parent::offsetSet($index, $package);
81
    }
82
    
83
    /**
84
     * @return array
85
     */
86 View Code Duplication
    private function getIndexedByName(): array
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...
87
    {
88
        if (!empty($this->indexedBy['name'])) {
89
            return $this->indexedBy['name'];
90
        }
91
        /** @var Package $package */
92
        foreach($this->getArrayCopy() as $package) {
93
            if (!($package instanceof Package)) {
94
                continue;
95
            }
96
            $this->indexedBy['name'][$package->getName()] = $package;
97
        }
98
        return $this->indexedBy['name'];
99
    }
100
101
    /**
102
     * @return array
103
     */
104 View Code Duplication
    private function getIndexedByNamespace(): array
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...
105
    {
106
        if (!empty($this->indexedBy['namespace'])) {
107
            return $this->indexedBy['namespace'];
108
        }
109
        /** @var Package $package */
110
        foreach($this->getArrayCopy() as $package) {
111
            if (!($package instanceof Package)) {
112
                continue;
113
            }
114
            $this->indexedBy['namespace'][$package->getNamespace()] = $package;
115
        }
116
        return $this->indexedBy['namespace'];
117
    }
118
}
119