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