SubsetContainer   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 18.1 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
cbo 1
dl 19
loc 105
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 10 3
A setPartition() 0 8 2
A addItemsToSubset() 0 6 2
A addItemToSubset() 0 7 1
A getSubsets() 10 11 2
A getSubsetsAndItemsAsArray() 9 10 2
A setAlgo() 0 4 1
A getAlgo() 0 4 1

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
namespace drupol\phpartition;
4
5
class SubsetContainer extends \SplHeap
6
{
7
8
  /**
9
   * @var BasePartitionAlgorithm
10
   */
11
    protected $algo;
12
13
  /**
14
   * Override compare method.
15
   *
16
   * @param Subset $a
17
   * @param Subset $b
18
   *
19
   * @return int
20
   */
21
    public function compare($a, $b)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
22
    {
23
        $al = $a->getAlgo()->getSubsetWeight($a);
0 ignored issues
show
Bug introduced by
The method getSubsetWeight() does not seem to exist on object<drupol\phpartition\BasePartitionAlgorithm>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $al. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
24
        $bl = $b->getAlgo()->getSubsetWeight($b);
0 ignored issues
show
Bug introduced by
The method getSubsetWeight() does not seem to exist on object<drupol\phpartition\BasePartitionAlgorithm>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $bl. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
25
26
        if ($al == $bl) {
27
            return 0;
28
        }
29
        return ($al > $bl) ? -1 : +1;
30
    }
31
32
  /**
33
   * @param $partition
34
   */
35
    public function setPartition($partition)
36
    {
37
        for ($i=0; $i<$partition; $i++) {
38
            $subset = new Subset();
39
            $subset->setAlgo($this->getAlgo());
40
            $this->insert($subset);
41
        }
42
    }
43
44
  /**
45
   * @param SubsetItem[] $items
46
   */
47
    public function addItemsToSubset(array $items = array())
48
    {
49
        foreach ($items as $item) {
50
            $this->addItemToSubset($item);
51
        }
52
    }
53
54
  /**
55
   * @param \drupol\phpartition\SubsetItem $item
56
   */
57
    public function addItemToSubset(SubsetItem $item)
58
    {
59
        $this->top();
60
        $subset = $this->extract();
61
        $subset->addItem($item);
62
        $this->insert($subset);
63
    }
64
65
  /**
66
   * @return Subset[]
67
   */
68 View Code Duplication
    public function getSubsets()
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...
69
    {
70
        $data = array();
71
        $clone = clone $this;
72
73
        for ($clone->top(); $clone->valid(); $clone->next()) {
74
            $data[] = $clone->current();
75
        }
76
77
        return $data;
78
    }
79
80
  /**
81
   * @return array
82
   */
83 View Code Duplication
    public function getSubsetsAndItemsAsArray()
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...
84
    {
85
        $data = array();
86
87
        foreach ($this->getSubsets() as $subset) {
88
            $data[] = $subset->getRawItems();
89
        }
90
91
        return array_values(array_filter($data));
92
    }
93
94
  /**
95
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
96
   */
97
    public function setAlgo(BasePartitionAlgorithm $algo)
98
    {
99
        $this->algo = $algo;
100
    }
101
102
  /**
103
   * @return BasePartitionAlgorithm
104
   */
105
    public function getAlgo()
106
    {
107
        return $this->algo;
108
    }
109
}
110