Matrix::process()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 24
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace App\BenchMark\Reporter;
19
20
class Matrix
21
{
22
    /** @var array<string,Task> The task collection. */
23
    protected array $tasks = [];
24
25
    /** The matrix result. */
26
    protected array $matrix = [];
27
28
    /** @var Task[] The ordered task name (faster first). */
29
    protected array $ranking = [];
30
31
    /**
32
     * @param Task[] $tasks a collection of tasks
33
     */
34
    public function __construct(array $tasks = [])
35
    {
36
        foreach ($tasks as $task) {
37
            $this->tasks[$task->name()] = $task;
38
        }
39
    }
40
41
    /**
42
     * Returns the matrix result.
43
     *
44
     * @return array
45
     */
46
    public function matrix()
47
    {
48
        return $this->matrix;
49
    }
50
51
    /**
52
     * Returns the tasks ranking.
53
     *
54
     * @return array
55
     */
56
    public function ranking()
57
    {
58
        return $this->ranking;
59
    }
60
61
    /**
62
     * Builds the matrix result.
63
     *
64
     * @return self
65
     */
66
    public function process(): self
67
    {
68
        $orderedTasks = $this->tasks;
69
70
        \usort($orderedTasks, function (Task $a, Task $b) {
71
            return $a->duration() > $b->duration() ? 1 : -1;
72
        });
73
74
        $this->ranking = $orderedTasks;
75
        $matrix        = [];
76
77
        foreach ($this->ranking as $task1) {
78
            $name1          = $task1->name();
79
            $matrix[$name1] = [];
80
81
            foreach ($this->ranking as $task2) {
82
                $name2                  = $task2->name();
83
                $percent                = (int) (\round($task1->duration() / $task2->duration() * 100));
84
                $matrix[$name1][$name2] = $percent;
85
            }
86
        }
87
        $this->matrix = $matrix;
88
89
        return $this;
90
    }
91
}
92