Completed
Push — master ( baf2a3...ee3f53 )
by Boudry
02:57
created

PairwiseStats::PairwiseComparison()   C

Complexity

Conditions 9
Paths 18

Size

Total Lines 65
Code Lines 35

Duplication

Lines 9
Ratio 13.85 %

Code Coverage

Tests 27
CRAP Score 9.0036

Importance

Changes 0
Metric Value
dl 9
loc 65
ccs 27
cts 28
cp 0.9643
rs 6.4766
c 0
b 0
f 0
cc 9
eloc 35
nc 18
nop 1
crap 9.0036

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
                    $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