Passed
Push — master ( 40d218...d516cb )
by James
09:17 queued 11s
created

Steam::cleanString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 54
rs 9.0909
cc 1
nc 1
nop 1

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
declare(strict_types=1);
3
/**
4
 * Steam.php
5
 * Copyright (c) 2020 [email protected]
6
 *
7
 * This file is part of the Firefly III CSV importer
8
 * (https://github.com/firefly-iii/csv-importer).
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Support;
25
26
/**
27
 * Class Steam
28
 */
29
class Steam
30
{
31
    /**
32
     * Convert a value.
33
     *
34
     * @param $value
35
     *
36
     * @return mixed
37
     *
38
     */
39
    public function cleanString($value): string
40
    {
41
        $value  = (string) $value;
42
        $search = [
43
            "\u{0001}", // start of heading
44
            "\u{0002}", // start of text
45
            "\u{0003}", // end of text
46
            "\u{0004}", // end of transmission
47
            "\u{0005}", // enquiry
48
            "\u{0006}", // ACK
49
            "\u{0007}", // BEL
50
            "\u{0008}", // backspace
51
            "\u{000E}", // shift out
52
            "\u{000F}", // shift in
53
            "\u{0010}", // data link escape
54
            "\u{0011}", // DC1
55
            "\u{0012}", // DC2
56
            "\u{0013}", // DC3
57
            "\u{0014}", // DC4
58
            "\u{0015}", // NAK
59
            "\u{0016}", // SYN
60
            "\u{0017}", // ETB
61
            "\u{0018}", // CAN
62
            "\u{0019}", // EM
63
            "\u{001A}", // SUB
64
            "\u{001B}", // escape
65
            "\u{001C}", // file separator
66
            "\u{001D}", // group separator
67
            "\u{001E}", // record separator
68
            "\u{001F}", // unit separator
69
            "\u{007F}", // DEL
70
            "\u{00A0}", // non-breaking space
71
            "\u{1680}", // ogham space mark
72
            "\u{180E}", // mongolian vowel separator
73
            "\u{2000}", // en quad
74
            "\u{2001}", // em quad
75
            "\u{2002}", // en space
76
            "\u{2003}", // em space
77
            "\u{2004}", // three-per-em space
78
            "\u{2005}", // four-per-em space
79
            "\u{2006}", // six-per-em space
80
            "\u{2007}", // figure space
81
            "\u{2008}", // punctuation space
82
            "\u{2009}", // thin space
83
            "\u{200A}", // hair space
84
            "\u{200B}", // zero width space
85
            "\u{202F}", // narrow no-break space
86
            "\u{3000}", // ideographic space
87
            "\u{FEFF}", // zero width no -break space
88
            "\t", // TAB
89
            "\r", // CARRIAGE RETURN!
90
        ];
91
92
        return trim(str_replace($search, "\x20", $value));
93
    }
94
95
    /**
96
     * @param $value
97
     *
98
     * @return string
99
     */
100
    public function cleanStringAndNewlines($value): string
101
    {
102
        $string = $this->cleanString($value);
103
104
        return trim(str_replace("\n", '', $string));
105
    }
106
107
}
108