-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
421 lines (329 loc) · 7.94 KB
/
index.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
# Object Calisthenics
# https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/Pragmatic%20Programmers/The%20ThoughtWorks%20Anthology.pdf
# The Rules
# 1. Use only one level of indentation per method.
# 2. Don’t use the else keyword.
# 3. Wrap all primitives and strings.
# 4. Use only one dot per line.
# 5. Don’t abbreviate.
# 6. Keep all entities small.
# 7. Don’t use any classes with more than two instance variables.
# 8. Use first-class collections.
# 9. Don’t use any getters/setters/properties.
# 1. Use only one level of indentation per method.
# it keeps more readable and helpful to understand what is happening, use the extract method stategy to achive that.
# Wrong
function moreIdentationFunction()
{
$counter = [];
for ($i = 0; $i < 10; $i++) { # level zero
$counter[] = $i;
foreach ($counter as $value) { # level one
if ($value == 5) { # level two
continue;
}
$counter[$value] = $value . ' result';
}
}
}
# Correct
function lessIdentationFunction()
{
$counter = [];
for ($i = 0; $i < 10; $i++) { # level zero
$counter[] = $i;
$counter = generateResult($counter);
}
}
function generateResult($counter)
{
foreach ($counter as $value) { # level zero
if ($value == 5) { # level one
continue;
}
$counter[$value] = $value . ' result';
}
return $counter;
}
# 2. Don’t use the else keyword.
# Most of cases you could avoid the else keyword when you adapt your algorithm.
function dontUseElse($age)
{
# Wrong
if ($age < 18) {
echo 'Under 18';
} else {
echo 'Over 18';
}
# Correct
if ($age < 18) {
echo 'Under 18';
}
echo 'Over 18';
}
# 3. Wrap all primitives and strings.
# Creating a class that encapsulates and provides a consistent interface for working with primitive values and strings.
class WrapAllPrimitives
{
private $value;
public function __construct($value)
{
if (!is_string($value) || !is_numeric($value) || !is_bool($value)) {
throw new InvalidArgumentException('Value must be a string, number or boolean');
}
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
$stringWrapper = new WrapAllPrimitives('Hello, world!');
echo $stringWrapper->getValue(); // Outputs: Hello, world!
$numberWrapper = new WrapAllPrimitives(42);
echo $numberWrapper->getValue(); // Outputs: 42
$boolWrapper = new WrapAllPrimitives(true);
echo $boolWrapper->getValue(); // Outputs: 1
# 4. Use only one dot per line (don't aplly in fluent API)
# Avoid the break of the open closed principle and also avoid mysterious exceptions, more easy to see on the debug stacktrace.
# Wrong
class PersonWrong
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string|object
{
return $this->name;
}
public function getFormattedName(): string|object
{
return strtoupper($this->name);
}
}
$personWrong = new PersonWrong('Charlie');
// Bad Example: Chained method calls with multiple dots in one line
$personWrong->getName()->getFormattedName();
# Correct
class PersonCorrect
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
}
class CarCorrect
{
private $owner;
public function __construct(PersonCorrect $owner)
{
$this->owner = $owner;
}
public function getOwnerName(): string
{
return $this->owner->getName();
}
}
$personCorrect = new PersonCorrect('Alice');
$carCorrect = new CarCorrect($personCorrect);
$carCorrect->getOwnerName();
# 5. Don’t abbreviate.
# Explain what the method or the variable means, avoid redundant names on class methods.
# Wrong
$cpm = 199;
class UserWrong
{
# people know the Add is for user so it is not a good name becaus eis redundant
function AddUser()
{
}
function DeleteUser()
{
}
}
# Correct
$costsPerMill = 199;
class UserCorrect
{
# Keep the context
function Add()
{
}
function Delete()
{
}
}
# 6. Keep all entities small.
# keep classes with not so long almost 50 lines
class FifityLinesForever
{
public function __construct()
{
throw new Exception('Follow the rule if you want ');
}
}
# 7. Don’t use any classes with more than two instance variables.
# Don't use more than two variables in a class.
# Wrong
class TooMuchVariables
{
public function __construct(
private array $data1,
private array $data2,
private array $data3,
private array $data4
) {
}
}
# Correct
class OnlyTwoVariables
{
public function __construct(
private array $data1,
private array $data2
) {
}
}
# 8. Use first-class collections.
# Then you could provide only the real things you want to happen with your data set.
# Wrong
class ClientCollectionWrong
{
private array $clientsData;
public function __construct()
{
$this->clientsData = [
1 => 'Morfeu',
2 => 'Neo'
];
}
function get($id)
{
return $this->clientsData[$id];
}
function add(string $name)
{
return array_push($this->clientsData, $name);
}
}
class RenderClientWrong
{
public function __construct(public ClientCollectionWrong $clients)
{
}
function show($id)
{
echo $this->clients->get($id);
}
function insert($name)
{
echo $this->clients->add($name);
}
}
$render = new RenderClientWrong((new ClientCollectionWrong));
$render->show(2);
$render->insert('Trinity');
$render->show(3);
# Correct
class ClientCollectionCorrect
{
private array $clients;
public function __construct()
{
$this->clients = [];
}
public function addClient(string $name): void
{
$this->clients[] = $name;
}
public function getClientById(int $id): ?string
{
return $this->clients[$id] ?? null;
}
public function getAllClients(): array
{
return $this->clients;
}
}
class RenderClientCorrect
{
private ClientCollectionCorrect $clients;
public function __construct(ClientCollectionCorrect $clients)
{
$this->clients = $clients;
}
public function showClientById(int $id): void
{
$client = $this->clients->getClientById($id);
if ($client !== null) {
echo $client;
}
}
public function insertClient(string $name): void
{
$this->clients->addClient($name);
}
}
$clients = new ClientCollectionCorrect();
$clients->addClient('Morfeu');
$clients->addClient('Neo');
$render = new RenderClientCorrect($clients);
$render->showClientById(1);
$render->insertClient('Trinity');
$render->showClientById(2);
# 9. Don’t use any getters/setters/properties.
# Use methods to do the actions for you instead of the set or get methods.
# Wrong
class WithGetAndSet
{
private int $score;
public function __construct(int $score)
{
$this->score = $score;
}
public function getScore(): int
{
return $this->score;
}
public function setScore(int $score): void
{
$this->score = $score;
}
public function increase(): int
{
$this->score += 2;
return $this->score;
}
public function decrease(): int
{
$this->score -= 2;
return $this->score;
}
}
# Correct
class WithouttGetAndSet
{
private int $score;
public function __construct(int $score)
{
$this->score = $score;
}
public function increase(): int
{
$this->score += 2;
return $this->score;
}
public function decrease(): int
{
$this->score -= 2;
return $this->score;
}
}