Completed
Push — master ( c8ccf3...5429d4 )
by Wanderson
01:41
created

strings.php ➔ strEscape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * FUNÇÕES DE TRATAMENTO DE STRINGS
5
 * Funções que tratam uma string e a retorna modificada começam com nome: str[...]
6
 * Funções que convertem uma string para outro formato começam com nome: strTo[...]
7
 * Funções de conversão deve ter o nome no formato:  [...]To[...]
8
 */
9
10
setlocale(LC_ALL, 'pt_BR.UTF8');
11
12
/**
13
 * Corta um texto, sem cortar a última palavra.
14
 * @param string $string [string a ser cortada]
15
 * @param int $length [tamanho da string cortada]
16
 * @param bool $rep  [define se corta antes ou depois do tamanho maximo]
17
 * @return string $string [string resumida ]
18
 */
19
function strTruncate($string, $length, $rep = false) {
20
	if (strlen($string) <= $length) {
21
		return $string;
22
	}
23
24
	if ($rep == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
25
		$oc = strrpos(substr($string, 0, $length), ' ');
26
	}
27
	if ($rep == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
28
		$oc = strpos(substr($string, $length), ' ') + $length;
29
	}
30
31
	$string = substr($string, 0, $oc) . '...';
0 ignored issues
show
Bug introduced by
The variable $oc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
32
	return $string;
33
}
34
35
/**
36
 * Limpa a string de caracteres inválidos
37
 * @param string $string
38
 * @return string
39
 */
40
function strClear($string) {
41
	return trim(strip_tags(str_replace('"', '&quot;', $string)));
42
}
43
44
/**
45
 * Criptografa a string utilizando MD5 + SALT
46
 * @param string $string [string a ser criptografada]
47
 * @return string $stringEncript [string criptografada]
48
 */
49
function strEncrypt($string, $salt = APP_ID) {
50
	return md5($salt . $string);
51
}
52
53
/**
54
 * Retorna total de caracteres da string UTF-8
55
 * @param string
56
 * @return int
57
 */
58
function strLength($string) {
59
	return mb_strlen($string);
60
}
61
62
/**
63
 * Retorna a string Maiúscula UTF-8
64
 * @param string $string
65
 * @return string
66
 */
67
function strUpper($string) {
68
	return mb_strtoupper($string);
69
}
70
71
/**
72
 * Retorna a string Minúscula UTF-8
73
 * @param string $string
74
 * @return string
75
 */
76
function strLower($string) {
77
	return mb_strtolower($string);
78
}
79
80
/**
81
 * Retorna a string somente com a primeira letra maiúscula UTF-8
82
 * @param string $string
83
 * @return string
84
 */
85
function strFirst($string) {
86
	return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
87
}
88
89
/**
90
 * Limpa a string, retirando espaços e tags html
91
 * @param string $string
92
 * @return string [retorna string limpa]
93
 */
94
function strStrip($string) {
95
	return trim(strip_tags($string));
96
}
97
98
/**
99
 * Formata o número com zeros à esquerda
100
 * @param int $int [numero a ser formatado]
101
 * @param int $length [quantidade de caracteres que o numero deverá ter]
102
 * @return string [número formatado. Ex: 01, 02 , 001, 003]
103
 */
104
function strLengthFormat($int = 0, $length = 2) {
105
	return str_pad($int, $length, "0", STR_PAD_LEFT);
106
}
107
108
/**
109
 * Converte uma string, nome ou frase em URL válida
110
 * @param string $string  [Ex: Minha Notícia da 'Página 2000 especial' ]
111
 * @return string $url [Ex: minha-noticia-da-pagina-2000-especial ]
112
 */
113
function strToURL($string) {
114
	$url = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
115
	$url = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $url);
116
	$url = strtolower(trim($url, '-'));
117
	$url = preg_replace("/[\/_| -]+/", '-', $url);
118
	return $url;
119
}
120
121
/**
122
 * Converte String para Float
123
 * @param string $string
124
 * @return float
125
 */
126
function strToFloat($string) {
127
	$f = str_replace(array('.', 'R$', '%'), array('', '', ''), $string);
128
	$f2 = str_replace(',', '.', $f);
129
	return (float) ($f2);
130
}
131
132
/**
133
 * Converte float para Moeda (em Real R$)
134
 * @param float $float
135
 * @return string
136
 */
137
function floatToMoney($float) {
138
	return number_format((float) $float, 2, ',', '.');
139
}
140
141
/**
142
 * Converte boolean para String 
143
 * @param boolean $boolean
144
 * @return string (Sim/Não)
145
 */
146
function booleanToString($boolean) {
147
	if ($boolean == true):
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
148
		return 'Sim';
149
	else:
150
		return 'Não';
151
	endif;
152
}
153
154
/**
155
 * Retorna no formato de ID, ex:  #00009 
156
 * @param int
157
 * @return string
158
 */
159
function formatId($id = 0) {
160
	return '#' . strLengthFormat($id, 6);
161
}
162