Passed
Pull Request — master (#2)
by Pol
03:09
created

Greedy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpartition;
6
7
use drupol\phpartition\Partition\Partition;
8
use drupol\phpartition\Partitions\Partitions;
9
10
/**
11
 * Class Greedy.
12
 */
13
class Greedy extends Partitioner
14
{
15
    /**
16
     * @var \ArrayObject
17
     */
18
    protected $dataset;
19
20
    /**
21
     * Greedy constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->dataset = new \ArrayObject();
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function add(...$values)
32
    {
33
        foreach ($values as $value) {
34
            $this->dataset->append($value);
35
        }
36
37
        return $this;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getDataset()
44
    {
45
        $dataset = $this->dataset;
46
        $partitionItemFactory = $this->getPartitionItemFactory();
47
48
        // Greedy needs a dataset sorted DESC.
49
        $copy = $dataset->getArrayCopy();
50
51
        \uasort(
52
            $copy,
53
            static function ($a, $b) use ($partitionItemFactory) {
54
                $left = $partitionItemFactory::create($a);
55
                $right = $partitionItemFactory::create($b);
56
57
                return $left->getWeight() <=> $right->getWeight();
58
            }
59
        );
60
61
        $dataset->exchangeArray(\array_reverse($copy));
62
63
        return $dataset;
64
    }
65
66
    /**
67
     * @param \drupol\phpartition\Partitions\Partitions $partitions
68
     * @param array $dataset
69
     * @param int $chunks
70
     */
71
    protected function fillPartitions(Partitions $partitions, array $dataset, int $chunks): void
72
    {
73
        foreach ($dataset as $data) {
74
            $this->findPartition($data, null, $dataset, $partitions)
75
                ->append($data);
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    private function findPartition($value, $key, array $dataset, Partitions $partitions): Partition
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

82
    private function findPartition(/** @scrutinizer ignore-unused */ $value, $key, array $dataset, Partitions $partitions): Partition

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $dataset is not used and could be removed. ( Ignorable by Annotation )

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

82
    private function findPartition($value, $key, /** @scrutinizer ignore-unused */ array $dataset, Partitions $partitions): Partition

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

82
    private function findPartition($value, /** @scrutinizer ignore-unused */ $key, array $dataset, Partitions $partitions): Partition

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        return $partitions
85
            ->sort()
86
            ->partition(0);
87
    }
88
}
89