|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kata\PHP7Kata; |
|
4
|
|
|
|
|
5
|
|
|
use Kata\OtherExamples\Form; |
|
6
|
|
|
use Kata\OtherExamples\Example; |
|
7
|
|
|
use Kata\OtherExamples\NotAnExample; |
|
8
|
|
|
|
|
9
|
|
|
final class LegacyToMigrate |
|
10
|
|
|
{ |
|
11
|
|
|
const ANIMALS = [ |
|
12
|
|
|
'dog', |
|
13
|
|
|
'cat', |
|
14
|
|
|
'bird' |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
1 |
|
public function getStringOrNull($param) |
|
18
|
|
|
{ |
|
19
|
1 |
|
return $param; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function sumOfIntegers(...$ints) |
|
23
|
|
|
{ |
|
24
|
|
|
// let's review that all items are integers! |
|
25
|
1 |
|
$result = array_map( |
|
26
|
|
|
function ($value) |
|
27
|
|
|
{ |
|
28
|
1 |
|
return (int) $value; |
|
29
|
1 |
|
}, |
|
30
|
|
|
$ints |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
1 |
|
return array_sum($result); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function arraysSum(...$arrays) |
|
37
|
|
|
{ |
|
38
|
1 |
|
return array_map( |
|
39
|
|
|
function (array $array) |
|
40
|
|
|
{ |
|
41
|
1 |
|
return ($array); |
|
42
|
1 |
|
}, |
|
43
|
|
|
$arrays |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function getUserName($user): string |
|
48
|
|
|
{ |
|
49
|
1 |
|
return isset($user['username']) ? $user['username'] : 'nobody'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function strcmpFromC($a, $b) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if ($a == $b) |
|
55
|
|
|
{ |
|
56
|
1 |
|
return 0; |
|
57
|
|
|
} |
|
58
|
1 |
|
elseif ($a > $b) |
|
59
|
|
|
{ |
|
60
|
|
|
return -1; |
|
61
|
|
|
} |
|
62
|
|
|
else |
|
63
|
|
|
{ |
|
64
|
1 |
|
return 1; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function saySomething() |
|
69
|
|
|
{ |
|
70
|
1 |
|
$example = new Example(); |
|
71
|
1 |
|
$not_an_example = new NotAnExample(); |
|
72
|
|
|
|
|
73
|
1 |
|
return $example->greetings() . ' ' . $not_an_example->greetings() . '!'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function divideEnters($a, $b) |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (0 === $b) |
|
79
|
|
|
{ |
|
80
|
|
|
throw new DivisionByZeroError(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
if ($a === PHP_INT_MIN && -1 === $b) |
|
84
|
|
|
{ |
|
85
|
|
|
throw new ArithmeticError(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return round($a / $b, 0); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function searchInText() |
|
92
|
|
|
{ |
|
93
|
1 |
|
$credit_card = 'daniel:madurell:visa:999888777'; |
|
94
|
|
|
|
|
95
|
1 |
|
$credit_card = preg_replace_callback( |
|
96
|
1 |
|
'/([a-z]{1})/', |
|
97
|
|
|
function ($m) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return strtoupper($m[1]); |
|
100
|
1 |
|
}, |
|
101
|
|
|
$credit_card |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
1 |
|
$credit_card = preg_replace_callback( |
|
105
|
1 |
|
'/([0-9]{1})/', |
|
106
|
1 |
|
function ($m) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
1 |
|
return 'XXX'; |
|
109
|
1 |
|
}, |
|
110
|
|
|
$credit_card |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
1 |
|
return $credit_card; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function callingClosures() |
|
117
|
|
|
{ |
|
118
|
1 |
|
$form = new Form(1); |
|
119
|
1 |
|
$form2 = new Form(2); |
|
120
|
|
|
|
|
121
|
1 |
|
$getX = $form->getX(); |
|
122
|
1 |
|
$getX = $getX->bindTo($form2); |
|
123
|
|
|
|
|
124
|
1 |
|
return $getX(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
public function nothingToReturn(&$left, &$right) |
|
128
|
|
|
{ |
|
129
|
1 |
|
if ($left === $right) |
|
130
|
|
|
{ |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
$tmp = $left; |
|
135
|
1 |
|
$left = $right; |
|
136
|
1 |
|
$right = $tmp; |
|
137
|
|
|
|
|
138
|
1 |
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
public function getComposedName($data, $number_user) |
|
142
|
|
|
{ |
|
143
|
1 |
|
list($id, $name) = $data[$number_user]; |
|
144
|
|
|
|
|
145
|
1 |
|
return $id . '_' . $name; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
public function getAllComposedNames($data) |
|
149
|
|
|
{ |
|
150
|
1 |
|
$all_composed_names = ''; |
|
151
|
|
|
|
|
152
|
1 |
|
foreach ($data as list($id, $name)) |
|
153
|
|
|
{ |
|
154
|
1 |
|
$all_composed_names .= $id . '_' . $name . '|'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
return $all_composed_names; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
public function tooMuchExceptionsMakeUglyCode() |
|
161
|
|
|
{ |
|
162
|
|
|
try |
|
163
|
|
|
{ |
|
164
|
1 |
|
$this->functionThatFails(); |
|
165
|
|
|
} |
|
166
|
1 |
|
catch (FirstException $e) |
|
167
|
|
|
{ |
|
168
|
1 |
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
try |
|
172
|
|
|
{ |
|
173
|
|
|
$this->anotherFunctionThatAlsoFails(); |
|
174
|
|
|
} |
|
175
|
|
|
catch (SecondException $e) |
|
176
|
|
|
{ |
|
177
|
|
|
return false; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return true; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
private function functionThatFails() |
|
184
|
|
|
{ |
|
185
|
1 |
|
throw new FirstException; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
private function anotherFunctionThatAlsoFails() |
|
189
|
|
|
{ |
|
190
|
|
|
throw new SecondException; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
public function getLastCharOfAString($a_string) |
|
194
|
|
|
{ |
|
195
|
1 |
|
return substr($a_string, -1); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
1 |
|
public function printForEachValue() |
|
199
|
|
|
{ |
|
200
|
|
|
//Yield me! |
|
201
|
1 |
|
$gen = range(1, 1); |
|
202
|
|
|
|
|
203
|
1 |
|
foreach ($gen as $key => $value) |
|
204
|
|
|
{ |
|
205
|
1 |
|
$this->doSomethingTooHeavy($key, $value); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
return $value; |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
1 |
|
private function doSomethingTooHeavy($key, $value) |
|
|
|
|
|
|
212
|
|
|
{ |
|
213
|
|
|
//... |
|
214
|
1 |
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.