Passed
Push — master ( ee3f53...9cc6d5 )
by Boudry
02:51
created

PairwiseStats   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 9
loc 69
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C PairwiseComparison() 9 65 9

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
    Condorcet PHP Class, with Schulze Methods and others !
4
5
    By Julien Boudry - MIT LICENSE (Please read LICENSE.txt)
6
    https://github.com/julien-boudry/Condorcet
7
*/
8
declare(strict_types=1);
9
10
11
12
/////////// TOOLS FOR MODULAR ALGORITHMS ///////////
13
14
namespace Condorcet\Algo\Tools;
15
16
use Condorcet\Algo\Pairwise;
17
18
// Generic for Algorithms
19
abstract class PairwiseStats
20
{
21 4
    public static function PairwiseComparison (Pairwise $pairwise) : array
22
    {
23 4
        $comparison = [];
24
25 4
        foreach ($pairwise as $candidate_key => $candidate_data) :
26
27 4
            $comparison[$candidate_key]['win'] = 0;
28 4
            $comparison[$candidate_key]['null'] = 0;
29 4
            $comparison[$candidate_key]['lose'] = 0;
30 4
            $comparison[$candidate_key]['balance'] = 0;
31 4
            $comparison[$candidate_key]['sum_defeat_margin'] = 0;
32 4
            $comparison[$candidate_key]['worst_pairwise_defeat_winning'] = 0;
33 4
            $comparison[$candidate_key]['worst_pairwise_defeat_margin'] = null;
34 4
            $comparison[$candidate_key]['worst_pairwise_opposition'] = 0;
35
36 4
            foreach ($candidate_data['win'] as $opponentKey => $opponentLose) :
37
38 4
                $defeat_margin = $candidate_data['lose'][$opponentKey] - $opponentLose;
39
40
                // Worst margin defeat
41 4
                if ($comparison[$candidate_key]['worst_pairwise_defeat_margin'] === null || $comparison[$candidate_key]['worst_pairwise_defeat_margin'] < $defeat_margin) :
42
43 4
                    $comparison[$candidate_key]['worst_pairwise_defeat_margin'] = $defeat_margin;
44
45
                endif;
46
47
                // Worst pairwise opposition
48 4 View Code Duplication
                if ($comparison[$candidate_key]['worst_pairwise_opposition'] < $candidate_data['lose'][$opponentKey]) :
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
49
50 4
                    $comparison[$candidate_key]['worst_pairwise_opposition'] = $candidate_data['lose'][$opponentKey];
51
                endif;
52
53
54
                // for each Win, null, Lose
55 4
                if ( $opponentLose > $candidate_data['lose'][$opponentKey] ) :
56
57 4
                    $comparison[$candidate_key]['win']++;
58 4
                    $comparison[$candidate_key]['balance']++;
59
60 4
                elseif ( $opponentLose === $candidate_data['lose'][$opponentKey] ) :
61
62 2
                    $comparison[$candidate_key]['null']++;
63
64
                else :
65
66 4
                    $comparison[$candidate_key]['lose']++;
67 4
                    $comparison[$candidate_key]['balance']--;
68
69 4
                    $comparison[$candidate_key]['sum_defeat_margin'] += $defeat_margin;
70
71
                    // Worst winning defeat
72 4 View Code Duplication
                    if ($comparison[$candidate_key]['worst_pairwise_defeat_winning'] < $candidate_data['lose'][$opponentKey]) :
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
73
74 4
                        $comparison[$candidate_key]['worst_pairwise_defeat_winning'] = $candidate_data['lose'][$opponentKey];
75
76
                    endif;
77
78
                endif;
79
80
            endforeach;
81
82
        endforeach;
83
84 4
        return $comparison;
85
    }
86
87
}
88