Passed
Push — master ( 506188...263cb0 )
by Serhii
03:10 queued 01:33
created

timeago.getRules   F

Complexity

Conditions 14

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 13
dl 0
loc 15
ccs 1
cts 1
cp 1
crap 14
rs 3.6
c 0
b 0
f 0
nop 2

How to fix   Complexity   

Complexity

Complex classes like timeago.getRules often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package timeago
2
3
import "github.com/SerhiiCho/timeago/models"
4
5
func getRules(number, lastDigit int) map[string]models.Rule {
6 1
	return map[string]models.Rule{
7
		"en": {
8
			Single: number == 1,
9
			Plural: number > 1 || number == 0,
10
		},
11
		"ru": {
12
			Special: (number >= 5 && number <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
13
			Single:  lastDigit == 1 || number == 0,
14
			Plural:  lastDigit >= 2 && lastDigit < 5,
15
		},
16
		"uk": {
17
			Special: (number >= 5 && number <= 20) || lastDigit == 0 || (lastDigit >= 5 && lastDigit <= 9),
18
			Single:  lastDigit == 1 || number == 0,
19
			Plural:  lastDigit >= 2 && lastDigit < 5,
20
		},
21
	}
22
}
23