Skip to content

Commit d650ffa

Browse files
melhorias no código e elevado para o nivel 10 no actions do github
1 parent e7777a5 commit d650ffa

2 files changed

Lines changed: 70 additions & 78 deletions

File tree

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323

2424
# Run PHPStan
2525
- name: Run PHPStan
26-
run: vendor/bin/phpstan analyse conf src --level=9 --configuration=phpstan.neon --autoload-file=src/resource/ConstantPhpStan.php --memory-limit=512M
26+
run: vendor/bin/phpstan analyse conf src --level=10 --configuration=phpstan.neon --autoload-file=src/resource/ConstantPhpStan.php --memory-limit=512M

src/Compare.php

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,49 @@
77

88
class Compare
99
{
10-
public static function daysDifferenceBetweenData(string $dtIni, string $dtFin): string
10+
private static function normalizeDateFormat(string $date): string
1111
{
12-
if (strpos($dtIni, '/') > -1) {
13-
$dtIni = implode('-', array_reverse(explode('/', $dtIni)));
12+
if (str_contains($date, '/')) {
13+
return implode('-', array_reverse(explode('/', $date)));
1414
}
15-
if (strpos($dtFin, '/') > -1) {
16-
$dtFin = implode('-', array_reverse(explode('/', $dtFin)));
15+
return $date;
16+
}
17+
18+
private static function normalizeUrl(string $url): string
19+
{
20+
return strtoupper(str_replace('/', '', $url));
21+
}
22+
23+
private static function convertTimeToSeconds(string $time): int
24+
{
25+
[$hours, $minutes, $seconds] = explode(':', $time);
26+
return ((int) $hours * 3600) + ((int) $minutes * 60) + (int) $seconds;
27+
}
28+
29+
private static function formatSecondsToTime(int $totalSeconds): string
30+
{
31+
$hours = floor($totalSeconds / 3600);
32+
$remainingSeconds = $totalSeconds - ($hours * 3600);
33+
$minutes = floor($remainingSeconds / 60);
34+
$seconds = $remainingSeconds - ($minutes * 60);
35+
36+
if (str_starts_with((string) $hours, '-')) {
37+
$formattedHours = '-' . str_pad(substr((string) $hours, 1), 2, '0', STR_PAD_LEFT);
38+
} else {
39+
$formattedHours = str_pad((string) $hours, 2, '0', STR_PAD_LEFT);
1740
}
1841

42+
$formattedMinutes = str_pad((string) $minutes, 2, '0', STR_PAD_LEFT);
43+
$formattedSeconds = str_pad((string) $seconds, 2, '0', STR_PAD_LEFT);
44+
45+
return "{$formattedHours}:{$formattedMinutes}:{$formattedSeconds}";
46+
}
47+
48+
public static function daysDifferenceBetweenData(string $dtIni, string $dtFin): string
49+
{
50+
$dtIni = self::normalizeDateFormat($dtIni);
51+
$dtFin = self::normalizeDateFormat($dtFin);
52+
1953
$datetime1 = new DateTime($dtIni);
2054
$datetime2 = new DateTime($dtFin);
2155
$interval = $datetime1->diff($datetime2);
@@ -25,123 +59,81 @@ public static function daysDifferenceBetweenData(string $dtIni, string $dtFin):
2559

2660
public static function startDateLessThanEnd(?string $dtIni, ?string $dtFin): bool
2761
{
28-
if (!empty($dtIni) && !empty($dtFin)) {
29-
if (str_replace('+', '', self::daysDifferenceBetweenData($dtIni, $dtFin)) < '0') {
30-
return false;
31-
}
32-
} else {
62+
if (empty($dtIni) || empty($dtFin)) {
3363
return false;
3464
}
35-
return true;
65+
66+
$daysDifference = (int) str_replace('+', '', self::daysDifferenceBetweenData($dtIni, $dtFin));
67+
return $daysDifference >= 0;
3668
}
3769

3870
public static function startHourLessThanEnd(
3971
string $hourIni,
4072
string $hourFin,
4173
string $msg = 'Hora Inicial não pode ser maior que a Hora Final!',
4274
): ?string {
43-
if (!empty($hourIni) && !empty($hourFin)) {
44-
$diff = self::differenceBetweenHours($hourIni, $hourFin);
45-
if (substr($diff, 0, 1) === '-') {
46-
return $msg;
47-
}
48-
} else {
75+
if (empty($hourIni) || empty($hourFin)) {
4976
return 'Um ou mais campos horas não foram preenchidos!';
5077
}
78+
79+
$diff = self::differenceBetweenHours($hourIni, $hourFin);
80+
if (str_starts_with($diff, '-')) {
81+
return $msg;
82+
}
83+
5184
return null;
5285
}
5386

5487
public static function calculateAgeInYears(string $date): int
5588
{
56-
if (strpos($date, '/') > -1) {
57-
$date = implode('-', array_reverse(explode('/', $date)));
58-
}
89+
$date = self::normalizeDateFormat($date);
5990
$dateBirth = new DateTime($date, new DateTimeZone('America/Sao_Paulo'));
6091
$dataNow = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
6192
$diff = $dataNow->diff($dateBirth);
62-
return intval($diff->format('%y'));
93+
return (int) $diff->format('%y');
6394
}
6495

6596
public static function differenceBetweenHours(string $hourIni, string $hourFin): string
6697
{
67-
$i = 1;
68-
$timeTotal = null;
69-
$times = [$hourFin, $hourIni];
70-
71-
foreach ($times as $time) {
72-
$seconds = 0;
73-
list($h, $m, $s) = explode(':', $time);
98+
$secondsIni = self::convertTimeToSeconds($hourIni);
99+
$secondsFin = self::convertTimeToSeconds($hourFin);
100+
$totalSeconds = $secondsFin - $secondsIni;
74101

75-
$seconds += intval($h) * 3600;
76-
$seconds += intval($m) * 60;
77-
$seconds += intval($s);
78-
79-
$timeTotal[$i] = $seconds;
80-
$i++;
81-
}
82-
$seconds = $timeTotal[1] - $timeTotal[2];
83-
$hours = floor($seconds / 3600);
84-
$seconds -= $hours * 3600;
85-
$minutes = str_pad(strval((floor($seconds / 60))), 2, '0', STR_PAD_LEFT);
86-
$seconds -= intval($minutes) * 60;
87-
88-
if (substr(strval($hours), 0, 1) === '-') {
89-
$hours = '-' . str_pad(substr(strval($hours), 1, 2), 2, '0', STR_PAD_LEFT);
90-
} else {
91-
$hours = str_pad(strval($hours), 2, '0', STR_PAD_LEFT);
92-
}
93-
return "$hours:$minutes:$seconds";
102+
return self::formatSecondsToTime($totalSeconds);
94103
}
95104

96105
public static function checkDataEquality(
97106
string $firstValue,
98-
string $secoundValue,
107+
string $secondValue,
99108
bool $caseSensitive = true,
100109
): bool {
101-
if ($caseSensitive) {
102-
if ($firstValue !== $secoundValue) {
103-
return false;
104-
}
105-
} else {
106-
if (0 !== strcasecmp($firstValue, $secoundValue)) {
107-
return false;
108-
}
109-
}
110-
return true;
110+
return $caseSensitive
111+
? $firstValue === $secondValue
112+
: strcasecmp($firstValue, $secondValue) === 0;
111113
}
112114

113115
public static function contains(string $value, string $search): bool
114116
{
115-
return strpos($value, $search) !== false;
117+
return str_contains($value, $search);
116118
}
117119

118120
public static function compareStringFrom(string $search, string $str, int $start, int $length): bool
119121
{
120-
if ($str === $search) {
121-
return true;
122-
}
123-
if (substr($str, $start, $length) === $search) {
124-
return true;
125-
}
126-
return false;
122+
return $str === $search || substr($str, $start, $length) === $search;
127123
}
128124

129125
public static function beginUrlWith(string $search, string $url): bool
130126
{
131-
$newSearch = strtoupper(str_replace('/', '', $search));
132-
$urlLessDivideBar = strtoupper(str_replace('/', '', $url));
133-
return self::compareStringFrom($newSearch, $urlLessDivideBar, 0, strlen($newSearch));
127+
$normalizedSearch = self::normalizeUrl($search);
128+
$normalizedUrl = self::normalizeUrl($url);
129+
return self::compareStringFrom($normalizedSearch, $normalizedUrl, 0, strlen($normalizedSearch));
134130
}
135131

136132
public static function finishUrlWith(string $search, string $url): bool
137133
{
138-
$newSearch = strtoupper(str_replace('/', '', $search));
139-
$urlLessDivideBar = strtoupper(str_replace('/', '', $url));
140-
return self::compareStringFrom(
141-
$newSearch,
142-
$urlLessDivideBar,
143-
(strlen($urlLessDivideBar) - strlen($newSearch)),
144-
strlen($urlLessDivideBar)
145-
);
134+
$normalizedSearch = self::normalizeUrl($search);
135+
$normalizedUrl = self::normalizeUrl($url);
136+
$startPosition = strlen($normalizedUrl) - strlen($normalizedSearch);
137+
return self::compareStringFrom($normalizedSearch, $normalizedUrl, $startPosition, strlen($normalizedUrl));
146138
}
147139
}

0 commit comments

Comments
 (0)