1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kata\PHP7Kata; |
4
|
|
|
|
5
|
|
|
use Kata\OtherExamples\{Form,Example,NotAnExample}; |
6
|
|
|
|
7
|
|
|
final class LegacyToMigrate |
8
|
|
|
{ |
9
|
|
|
public const ANIMALS = [ |
10
|
|
|
'dog', |
11
|
|
|
'cat', |
12
|
|
|
'bird' |
13
|
|
|
]; |
14
|
|
|
|
15
|
1 |
|
public function getStringOrNull(?string $param): ?string |
16
|
|
|
{ |
17
|
1 |
|
return $param; |
18
|
|
|
} |
19
|
|
|
|
20
|
1 |
|
public function sumOfIntegers(int ...$ints) |
21
|
|
|
{ |
22
|
1 |
|
return array_sum($ints); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
public function arraysSum(array ...$arrays) |
26
|
|
|
{ |
27
|
1 |
|
return $arrays; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function getUserName($user): string |
31
|
|
|
{ |
32
|
1 |
|
return $user['username'] ?? 'nobody'; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function strcmpFromC($a, $b) |
36
|
|
|
{ |
37
|
1 |
|
return $a <=> $b; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function saySomething() |
41
|
|
|
{ |
42
|
1 |
|
$example = new Example(); |
43
|
1 |
|
$not_an_example = new NotAnExample(); |
44
|
|
|
|
45
|
1 |
|
return $example->greetings() . ' ' . $not_an_example->greetings() . '!'; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function divideEnters($a, $b) |
49
|
|
|
{ |
50
|
1 |
|
return intdiv($a, $b); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function searchInText() |
54
|
|
|
{ |
55
|
1 |
|
$credit_card = 'daniel:madurell:visa:999888777'; |
56
|
|
|
|
57
|
1 |
|
$credit_card = preg_replace_callback_array([ |
58
|
|
|
'/([a-z]{1})/' => function ($matches) { |
59
|
1 |
|
return strtoupper($matches[1]); |
60
|
1 |
|
}, |
61
|
|
|
'/([0-9]{1})/' => function ($matches) { |
|
|
|
|
62
|
1 |
|
return 'XXX'; |
63
|
1 |
|
} |
64
|
|
|
], $credit_card); |
65
|
|
|
|
66
|
1 |
|
return $credit_card; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function callingClosures() |
70
|
|
|
{ |
71
|
1 |
|
$form = new Form(1); |
72
|
1 |
|
$form2 = new Form(2); |
73
|
|
|
|
74
|
1 |
|
$getX = $form->getX(); |
75
|
1 |
|
return $getX->call($form2); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function nothingToReturn(&$left, &$right): void |
79
|
|
|
{ |
80
|
1 |
|
if ($left === $right) |
81
|
|
|
{ |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$tmp = $left; |
86
|
1 |
|
$left = $right; |
87
|
1 |
|
$right = $tmp; |
88
|
|
|
|
89
|
1 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function getComposedName($data, $number_user) |
93
|
|
|
{ |
94
|
1 |
|
[$id, $name] = $data[$number_user]; |
|
|
|
|
95
|
|
|
|
96
|
1 |
|
return $id . '_' . $name; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function getAllComposedNames($data) |
100
|
|
|
{ |
101
|
1 |
|
$all_composed_names = ''; |
102
|
|
|
|
103
|
1 |
|
foreach ($data as [$id, $name]) |
104
|
|
|
{ |
105
|
1 |
|
$all_composed_names .= $id . '_' . $name . '|'; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $all_composed_names; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function tooMuchExceptionsMakeUglyCode() |
112
|
|
|
{ |
113
|
|
|
try |
114
|
|
|
{ |
115
|
1 |
|
$this->functionThatFails(); |
116
|
|
|
$this->anotherFunctionThatAlsoFails(); |
117
|
|
|
} |
118
|
1 |
|
catch (FirstException | SecondException $e) |
119
|
|
|
{ |
120
|
1 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
private function functionThatFails() |
127
|
|
|
{ |
128
|
1 |
|
throw new FirstException; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function anotherFunctionThatAlsoFails() |
132
|
|
|
{ |
133
|
|
|
throw new SecondException; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function getLastCharOfAString($a_string) |
137
|
|
|
{ |
138
|
1 |
|
return $a_string[-1]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function printForEachValue() |
142
|
|
|
{ |
143
|
1 |
|
$gen = function () |
144
|
|
|
{ |
145
|
1 |
|
yield 1; |
146
|
1 |
|
}; |
147
|
|
|
|
148
|
1 |
|
foreach ($gen() as $key => $value) |
149
|
|
|
{ |
150
|
1 |
|
$this->doSomethingTooHeavy($key, $value); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return $value; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
private function doSomethingTooHeavy($key, $value) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
//... |
159
|
1 |
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.