Completed
Push — master ( efafaa...1ce8a3 )
by Avtandil
05:26
created

StrTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 26.42 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 28
loc 106
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A add_zeros() 0 7 1
A format_balance() 0 7 1
A snake_case_to_camel_case() 0 5 1
A camel_case_to_snake_case() 0 8 1
A convert_spaces_to_dashes() 0 8 1
A limit_middle() 0 7 1
A hash() 0 14 1
A to_dot_notation() 14 14 1
A convert_to_utf8() 14 14 1

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
declare(strict_types=1);
4
5
namespace Tests\Unit\Support;
6
7
use Illuminate\Foundation\Testing\DatabaseTransactions;
8
use Longman\LaravelLodash\Support\Str;
9
use stdClass;
10
use Tests\Unit\TestCase;
11
12
use function mb_strlen;
13
14
class StrTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
15
{
16
    use DatabaseTransactions;
17
18
    /** @test */
19
    public function add_zeros(): void
20
    {
21
        $string = '12345678';
22
23
        $this->assertSame('0012345678', Str::addZeros($string, 10, 'left'));
24
        $this->assertSame('1234567800', Str::addZeros($string, 10, 'right'));
25
    }
26
27
    /** @test */
28
    public function format_balance(): void
29
    {
30
        $int = 12345;
31
32
        $this->assertSame('123.45', Str::formatBalance($int, 2));
33
        $this->assertSame('123.450', Str::formatBalance($int, 3));
34
    }
35
36
    /** @test */
37
    public function snake_case_to_camel_case(): void
38
    {
39
        $string = 'Lorem_ipsum_dolores';
40
        $this->assertSame('LoremIpsumDolores', Str::snakeCaseToCamelCase($string));
41
    }
42
43
    /** @test */
44
    public function camel_case_to_snake_case(): void
45
    {
46
        $string = 'LoremIpsumDolores';
47
        $this->assertSame('lorem_ipsum_dolores', Str::camelCaseToSnakeCase($string));
48
49
        $string = 'არჩევანისგარემოსუზრუნველყოფისსისტემა';
50
        $this->assertSame('არჩევანისგარემოსუზრუნველყოფისსისტემა', Str::camelCaseToSnakeCase($string));
51
    }
52
53
    /** @test */
54
    public function convert_spaces_to_dashes(): void
55
    {
56
        $string = 'Lorem Ipsum Dolores';
57
        $this->assertSame('Lorem-Ipsum-Dolores', Str::convertSpacesToDashes($string));
58
59
        $string = 'არჩევანის გარემოს უზრუნველყოფის სისტემა';
60
        $this->assertSame('არჩევანის-გარემოს-უზრუნველყოფის-სისტემა', Str::convertSpacesToDashes($string));
61
    }
62
63
    /** @test */
64
    public function limit_middle(): void
65
    {
66
        $string = 'არჩევანის გარემოს უზრუნველყოფის სისტემა';
67
68
        $this->assertSame('არჩევანის ...ის სისტემა', Str::limitMiddle($string, 20, '...'));
69
        $this->assertSame(23, mb_strlen(Str::limitMiddle($string, 20, '...')));
70
    }
71
72
    /** @test */
73
    public function hash(): void
74
    {
75
        $data = ['aa' => 1, 'bb' => 2, 'cc' => 3];
76
        $this->assertSame('899a999da95e9f021fc63c6af006933fd4dc3aa1', Str::hash($data));
77
78
        $data = new stdClass();
79
        $data->aaa = 1;
80
        $data->bbb = 2;
81
        $data->ccc = 3;
82
        $this->assertSame('41d162b72eab4e7cfb6bb853d651fbaa2ae0573b', Str::hash($data));
83
84
        $data = null;
85
        $this->assertSame('eef19c54306daa69eda49c0272623bdb5e2b341f', Str::hash($data));
86
    }
87
88
    /** @test */
89 View Code Duplication
    public function to_dot_notation(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91
        $string = 'data[first][]';
92
        $this->assertSame('data.first[]', Str::toDotNotation($string));
93
94
        $string = 'data[first][second]';
95
        $this->assertSame('data.first.second', Str::toDotNotation($string));
96
97
        $string = 'data[first][second]third';
98
        $this->assertSame('data.first.secondthird', Str::toDotNotation($string));
99
100
        $string = 'data[first][second][0]';
101
        $this->assertSame('data.first.second.0', Str::toDotNotation($string));
102
    }
103
104
    /** @test */
105 View Code Duplication
    public function convert_to_utf8(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
106
    {
107
        $data = 'hello žš, გამარჯობა';
108
        $this->assertSame('hello žš, გამარჯობა', Str::convertToUtf8($data));
109
110
        $data = 'Hírek';
111
        $this->assertSame('Hírek', Str::convertToUtf8($data));
112
113
        $data = 'H�rek';
114
        $this->assertSame('H�rek', Str::convertToUtf8($data));
115
116
        $data = "Fédération Camerounaise de Football\n";
117
        $this->assertSame('Fédération Camerounaise de Football', Str::convertToUtf8($data));
118
    }
119
}
120