Number2WordFilter   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 89
Duplicated Lines 4.49 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 4
loc 89
rs 10
wmc 25
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 4 77 25

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
namespace App\Services\Filters;
4
5
use Nette\Object;
6
7
class Number2WordFilter extends Object
8
{
9
10
	/**
11
	 * převod desitkového čísla vyjádřeného matematicky na číslo vyjádřené slovně
12
	 * param int $number - číslo v desítkové soustavě (od 0 do 999999)
13
	 * param bool $zero - true:když bude $number 0 zobrazí se na výstupu nula; false:když bude $number 0 zobrazí se na výstupu (bool)false
14
	 * return string nebo bool false
15
	 * (C) Martin Bumba, http://mbumba.cz
16
	 */
17
	public function __invoke($number, $zero = false)
18
	{
19
		$units = ['', 'jedna', 'dva', 'tři', 'čtyři', 'pět', 'šest', 'sedm', 'osm', 'devět'];
20
		$between = [
21
			11 => 'jedenáct',
22
			12 => 'dvanáct',
23
			13 => 'třináct',
24
			14 => 'čtrnáct',
25
			15 => 'patnáct',
26
			16 => 'šestnáct',
27
			17 => 'sedmnáct',
28
			18 => 'osmnáct',
29
			19 => 'devatenáct'
30
		];
31
		$dozens = [
32
			'',
33
			'deset',
34
			'dvacet',
35
			'třicet',
36
			'čtyřicet',
37
			'padesát',
38
			'šedesát',
39
			'sedmdesát',
40
			'osmdesát',
41
			'devadesát'
42
		];
43
		$number = (string) ltrim(round($number), 0);
44
		$length = strlen($number);
45
46
		// treatment of 0
47
		if($number == 0) return $zero ? 'nula' : false;
48
		// 1 regulation - units
49
		elseif($length == 1) return $units[$number];
50
		// 2 regulations - desítky
51
		elseif($length == 2) {
52
			$dozensAndUnits = $number{0} . $number{1};
53
			if($dozensAndUnits == 10) echo 'deset';
54
			elseif($dozensAndUnits < 20) {
55
				return $between[$dozensAndUnits];
56
			}
57
			else {
58
				return $dozens[$number{0}] . $units[$number{1}];
59
			}
60
		}
61
		// 3 regulations - hundreds
62
		elseif($length == 3) {
63
			if($number{0} == 1) return 'sto' . self::__invoke(substr($number, 1));
64
			elseif($number{0} == 2) return 'dvěstě' . self::__invoke(substr($number, 1));
65 View Code Duplication
			elseif($number{0} == 3 || $number{0} == 4) return $units[$number{0}] . 'sta' . self::__invoke(substr($number, 1));
0 ignored issues
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...
66
			else return $units[$number{0}] . 'set' . self::__invoke(substr($number, 1));
67
		}
68
		// 4 regulations - thousands
69
		elseif($length == 4) {
70
			if($number{0} == 1) return 'tisíc' . self::__invoke(substr($number, 1));
71 View Code Duplication
			elseif($number{0} < 5) return $units[$number{0}] . 'tisíce' . self::__invoke(substr($number, 1));
0 ignored issues
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...
72
			else return $units[$number{0}] . 'tisíc' . self::__invoke(substr($number, 1));
73
		}
74
		// 5 regulations - tens of thousands
75
		elseif($length == 5) {
76
			$tensOfThousands = $number{0} . $number{1};
77
			if($tensOfThousands == 10) return 'desettisíc' . self::__invoke(substr($number, 2));
78
			elseif($tensOfThousands < 20) return $between[$tensOfThousands] . 'tisíc' . self::__invoke(substr($number, 2));
79 View Code Duplication
			elseif($tensOfThousands < 100) return $dozens[$number{0}] . $units[$number{1}] . 'tisíc' . self::__invoke(substr($number, 2));
0 ignored issues
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...
80
		}
81
		// 6 regulations - hundreds of thousands
82
		elseif($length == 6) {
83
			if($number{0} == 1) {
84
				if($number{1} . $number{2} == 00) return 'stotisíc' . self::__invoke(substr($number, 3));
85
				else return 'sto' . self::__invoke(substr($number, 1));
86
			}
87
			elseif($number{0} == 2) return 'dvěstě' . self::__invoke(substr($number, 1));
88 View Code Duplication
			elseif($number{0} == 3 || $number{0} == 4) return $units[$number{0}] . 'sta' . self::__invoke(substr($number, 1));
0 ignored issues
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...
89
			else return $units[$number{0}] . 'set' . self::__invoke(substr($number, 1));
90
		}
91
92
		return false;
93
	}
94
95
}
96