Repository navigation
Expand file tree
/
Copy pathNestedTryCatchSource.php
More file actions
87 lines (73 loc) · 2.37 KB
/
Copy pathNestedTryCatchSource.php
File metadata and controls
87 lines (73 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
namespace Rector\Bench\Source;
use InvalidArgumentException;
use RuntimeException;
use Throwable;
final class NestedTryCatchSource
{
public function run(array $items): int
{
$count = 0;
foreach ($items as $item) {
try {
$count += $this->process($item);
} catch (InvalidArgumentException $invalidArgumentException) {
try {
$count += $this->retry($item, $invalidArgumentException->getMessage());
} catch (RuntimeException $runtimeException) {
try {
$count += $this->fallback($item, $runtimeException->getCode());
} catch (Throwable $throwable) {
try {
$this->log($throwable->getMessage() . ' ' . $item);
} catch (Throwable) {
$count -= 1;
}
}
}
}
}
return $count;
}
public function sequential(string $value): string
{
try {
$value = $this->normalize($value);
} catch (InvalidArgumentException $invalidArgumentException) {
$value = $invalidArgumentException->getMessage();
}
try {
$value = strtoupper($this->normalize($value));
} catch (RuntimeException $runtimeException) {
try {
$value = $this->normalize($runtimeException->getMessage());
} catch (Throwable) {
$value = '';
}
} finally {
$value .= '!';
}
return $value;
}
private function process(mixed $item): int
{
return is_int($item) ? $item : throw new InvalidArgumentException('not int');
}
private function retry(mixed $item, string $reason): int
{
return is_numeric($item) ? (int) $item : throw new RuntimeException($reason);
}
private function fallback(mixed $item, int $code): int
{
return $code + (is_string($item) ? strlen($item) : 0);
}
private function log(string $message): void
{
error_log($message);
}
private function normalize(string $value): string
{
return trim($value) !== '' ? trim($value) : throw new InvalidArgumentException('empty');
}
}