Skip to content

Commit 970f1d1

Browse files
authored
Merge pull request #35 from php-school/fixes
Fixes
2 parents 73ab22a + fdce646 commit 970f1d1

File tree

18 files changed

+115
-84
lines changed

18 files changed

+115
-84
lines changed

app/bootstrap.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
2626
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
2727
use PhpSchool\PHP8Appreciate\Exercise\InfiniteDivisions;
28-
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
28+
use PhpSchool\PHP8Appreciate\Exercise\PhpGetsAPromotion;
2929
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
3030
use PhpSchool\PHP8Appreciate\Exercise\TheAttributesOfSuccess;
3131
use PhpSchool\PHP8Appreciate\Exercise\TheReturnOfStatic;
@@ -36,18 +36,18 @@
3636

3737
$app = new Application('', __DIR__ . '/config.php');
3838

39+
$app->addExercise(PhpGetsAPromotion::class);
3940
$app->addExercise(AMatchMadeInHeaven::class);
40-
$app->addExercise(HaveTheLastSay::class);
41-
$app->addExercise(PhpPromotion::class);
41+
$app->addExercise(ThrowAnExpression::class);
4242
$app->addExercise(CautionWithCatches::class);
43-
$app->addExercise(LordOfTheStrings::class);
43+
$app->addExercise(AllMixedUp::class);
44+
$app->addExercise(HaveTheLastSay::class);
4445
$app->addExercise(UniteTheTypes::class);
4546
$app->addExercise(InfiniteDivisions::class);
4647
$app->addExercise(ASafeSpaceForNulls::class);
47-
$app->addExercise(AllMixedUp::class);
48-
$app->addExercise(TheReturnOfStatic::class);
49-
$app->addExercise(ThrowAnExpression::class);
5048
$app->addExercise(StringifyToDemystify::class);
49+
$app->addExercise(LordOfTheStrings::class);
50+
$app->addExercise(TheReturnOfStatic::class);
5151
$app->addExercise(TheAttributesOfSuccess::class);
5252

5353
$art = <<<ART

app/config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
77
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
88
use PhpSchool\PHP8Appreciate\Exercise\InfiniteDivisions;
9-
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
9+
use PhpSchool\PHP8Appreciate\Exercise\PhpGetsAPromotion;
1010
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
1111
use PhpSchool\PHP8Appreciate\Exercise\TheAttributesOfSuccess;
1212
use PhpSchool\PHP8Appreciate\Exercise\TheReturnOfStatic;
@@ -28,8 +28,8 @@
2828
HaveTheLastSay::class => function (ContainerInterface $c) {
2929
return new HaveTheLastSay($c->get(PhpParser\Parser::class));
3030
},
31-
PhpPromotion::class => function (ContainerInterface $c) {
32-
return new PhpPromotion($c->get(PhpParser\Parser::class));
31+
PhpGetsAPromotion::class => function (ContainerInterface $c) {
32+
return new PhpGetsAPromotion($c->get(PhpParser\Parser::class));
3333
},
3434
CautionWithCatches::class => function (ContainerInterface $c) {
3535
return new CautionWithCatches($c->get(PhpParser\Parser::class), $c->get(\Faker\Generator::class));

exercises/a-match-made-in-heaven/problem/problem.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
You have been given a piece of code (look for `a-match-made-in-heaven.php` in your working directory) which is full of bugs.
2-
The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced
3-
`match` expression in PHP 8.
2+
The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced `match` expression in PHP 8.
43

54
The piece of code is supposed to take a string representing a keyboard keypress and convert it to its equivalent ANSI decimal code.
65

76
There are only four key presses supported at the minute (enter, up, down & escape). It should stay like that for now.
87

98
Focus on converting the switch statement to a match expression.
109

11-
The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation
12-
but it will be randomly picked from the four supported key presses.
10+
The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation but it will be randomly picked from the four supported key presses.
1311

1412
### The advantages of match
1513

1614
* Match uses strict equality, unlike switch which uses weak comparison and can lead to subtle bugs.
1715
* Each match arm does not fall through without a break statement, unlike switch.
18-
* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown
16+
* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown.
1917
* Match is an expression and thus returns a value, reducing unnecessary variables and reducing the risk of accessing undefined variables.
2018

2119

exercises/a-safe-space-for-nulls/problem/problem.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ With the null safe operator it is possible to access variables like so:
1010
$capitalPopulation = $country?->capital?->population;
1111
```
1212

13-
If the `capital` property is null, the variable `$capitalPopulation` will also be null. Previously, without the null safe operator, this would be achieved like so:
13+
If the `$capital` property is null, the variable `$capitalPopulation` will also be null. Previously, without the null safe operator, this would be achieved like so:
1414

1515
```php
1616
$capitalPopulation = null;
@@ -44,25 +44,24 @@ Export the `$user` data to a CSV with the following columns:
4444

4545
`First Name`, `Last Name`, `Age`, `House num`, `Addr 1`, `Addr 2`
4646

47-
* The CSV should be comma delimited
48-
* The columns should read exactly as above, any mistake will trigger a failure
49-
* There should be one row for the column headers and one for the data
50-
* Any properties which are null on the user should be printed as empty fields in the CSV
51-
* The file should be named `users.csv` and exist next to your submission file (eg in the same directory)
47+
* The CSV should be comma delimited.
48+
* The columns should read exactly as above, any mistake will trigger a failure.
49+
* There should be one row for the column headers and one for the data.
50+
* Any properties which are null on the user should be printed as empty fields in the CSV.
51+
* The file should be named `users.csv` and exist next to your submission file (eg in the same directory).
5252

5353
And finally, the most important part, all properties which may be `NULL` should be accessed using the null safe operator!
5454

5555
### Advantages of the null safe operator
5656

57-
* Much less code for simple operations where null is a valid value
57+
* Much less code for simple operations where null is a valid value.
5858
* If the operator is part of a chain anything to the right of the null will not be executed, the statements will be short-circuited.
59-
* Can be used on methods where null coalescing cannot `$user->getCreatedAt()->format() ?? null` where `getCreatedAt()` could return null or a `\DateTime` instance
59+
* Can be used on methods where null coalescing cannot `$user->getCreatedAt()->format() ?? null` where `getCreatedAt()` could return null or a `\DateTime` instance.
6060

6161
----------------------------------------------------------------------
6262
## HINTS
6363

64-
Remember your program will be passed no arguments. There will be a `User` object populated for you under the variable `$user`.
65-
It is available at the beginning of your script.
64+
Remember your program will be passed no arguments. There will be a `User` object populated for you under the variable `$user`. It is available at the beginning of your script.
6665

6766
Documentation on the Null Safe Operator can be found by pointing your browser here:
6867
[https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.nullsafe]()

exercises/all-mixed-up/problem/problem.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
As alluded to earlier in `Unite The Types` PHP's type system has been constantly and consistently growing for a long time.
1+
PHP's type system has been constantly and consistently growing and evolving for a long time.
22

3-
PHP8 comes with a new type `mixed` which is essentially a union of `array|bool|callable|int|float|object|resource|string|null`.
3+
PHP 8 comes with a new type `mixed` which is essentially a union of `array|bool|callable|int|float|object|resource|string|null`.
44

55
This means that any value which can be produced in PHP will pass the check. Which is exactly the same as no type check.
66

@@ -11,7 +11,7 @@ If anything - it signals intent. Your function really does accept anything. It's
1111
----------------------------------------------------------------------
1212
Create a program which contains one function named `logParameter`. It should have one parameter, and it's type must be `mixed`.
1313

14-
Within your function you should log the type of the parameter that was passed. For this, you can use a new PHP8 function called `get_debug_type`.
14+
Within your function you should log the type of the parameter that was passed. For this, you can use a new PHP 8 function called `get_debug_type`.
1515

1616
This function will give you a string identifier representing the type of any PHP variable.
1717

@@ -51,6 +51,6 @@ Documentation on `get_debug_type` can be found by pointing your browser here:
5151

5252
You might want to delete or empty your log file each time your program starts, otherwise it will grow and grow and the comparison will fail.
5353

54-
Think about the return type of your `adder` function - you could declare it as `void`.
54+
Think about the return type of your `logParameter` function - you could declare it as `void`.
5555

5656
If you are curious how we compare the output of your program when it includes time, we simply check that it matches the format, rather than comparing exactly. More specifically, we use the regex `/\d{2}:\d{2}:\d{2}/`.

exercises/all-mixed-up/solution/solution.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
if (file_exists('param.log')) {
4+
unlink('param.log');
5+
}
6+
37
function logParameter(mixed $parameter): void
48
{
59
file_put_contents(

exercises/caution-with-catches/problem/problem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function verify_password(string $password): bool;
1111

1212
For this exercise this function will always throw an exception but unfortunately the exception message contains the password in plain text!
1313

14-
To pass this exercise you will need to call the `verify_password` function with the password provided, handle the exception and output `"Given password is invalid, please try again`.
14+
To pass this exercise you will need to call the `verify_password` function with the password provided, handle the exception and output `"Given password is invalid, please try again"`.
1515

1616
PHP 8 allows you to handle the exception without capturing the exception itself which will ensure this message is not leaked further.
1717

exercises/have-the-last-say/problem/problem.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@ Create a program that reads a CSV file using `fgetcsv` and change the delimiter
22

33
The first argument is the file name of the CSV which you should read.
44

5-
The CSV file contains two columns separated with `|` (the pipe operator). The first column is `country`
6-
and the second column is `capital`. You should print each row to the console like the
7-
following (with a new line after each row):
5+
The CSV file contains two columns separated with `|` (the pipe operator). The first column is `country` and the second column is `capital`.
6+
7+
You should print each row to the console like the following (with a new line after each row):
88

99
```
1010
Country: Austria, Capital: Vienna
1111
```
1212

1313
The list of countries will be picked at random, so the CSV will be different each time your program runs.
1414

15-
When using `fgetcsv` there's a bunch of arguments which change the behaviour of the way the CSV is parsed. In our case we
16-
want to change the `separator` argument, which defaults to a comma `,`. We need it to be the
17-
pipe `|` character.
15+
When using `fgetcsv` there's a bunch of arguments which change the behaviour of the way the CSV is parsed. In our case we want to change the `separator` argument, which defaults to a comma `,`. We need it to be the pipe `|` character.
1816

19-
We don't want to specify the rest of the arguments, so aside from the file pointer which is the first argument,
20-
we only want to specify the `separator` argument.
17+
We don't want to specify the rest of the arguments, so aside from the file pointer which is the first argument, we only want to specify the `separator` argument.
2118

2219
Named arguments are a great way to change argument default values without having to specify all the defaults again.
20+
2321
For example, if you only want to change the value of the last argument to a function,
24-
you can do so, without specifying all the other arguments. For example:
22+
you can do so, without specifying all the other arguments:
2523

2624
```php
27-
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
25+
htmlspecialchars($string, ENT_COMPAT | ENT_HTML, 'UTF-8', false);
2826
```
2927

30-
We only want to change the last argument (double_encode) of the function to false (the default is true). However,
31-
we are forced to specify all the other arguments, but they have not changed from the defaults.
28+
We only want to change the last argument (double_encode) of the function to false (the default is true). However, we are forced to specify all the other arguments, even though they have not changed from the defaults.
3229

3330
Named arguments allows to write the same, but in a more succinct fashion:
3431

@@ -40,8 +37,8 @@ Note: only the values changed from the defaults are specified!
4037

4138
### Advantages of named arguments
4239

43-
* Possible to skip defaults in between the arguments you want to change
44-
* The code is better documented since the argument label is specified with the value, very useful for booleans
40+
* Possible to skip defaults in between the arguments you want to change.
41+
* The code is better documented since the argument label is specified with the value, very useful for booleans.
4542

4643
----------------------------------------------------------------------
4744
## HINTS
@@ -52,8 +49,7 @@ You will need to open the file for writing before using `fgetcsv` you can do tha
5249

5350
You will most likely need a loop to process all the data in the file.
5451

55-
You will need to keep reading from the file until it has been fully read. `feof` is your friend here to know
56-
whether there is any data left to read.
52+
You will need to keep reading from the file until it has been fully read. `feof` is your friend here and will inform you whether there is any data left to read.
5753

5854
Documentation on the `fopen` function can be found by pointing your browser here:
5955
[https://www.php.net/manual/en/function.fopen.php]()
@@ -67,6 +63,4 @@ Documentation on the `feof` function can be found by pointing your browser here:
6763
----------------------------------------------------------------------
6864
## EXTRA
6965

70-
Although not entirely necessary for a small script, it is good practise to close
71-
any open file handles so that other processes can access them, you can use `fclose`
72-
for that.
66+
Although not entirely necessary for a small script, it is good practise to close any open file handles so that other processes can access them, you can use `fclose` for that.

exercises/lord-of-the-strings/problem/problem.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
PHP has a huge standard library with the most extravagant and highly specific string and array functions available.
2-
`levenshtein` & `array_change_key_case` anyone??
1+
PHP has a huge standard library with the most extravagant and highly specific string and array functions available. `levenshtein` & `array_change_key_case` anyone??
32

43
On the flip side, historically it has missed some very common string operations. There's always been a way to get the same results, but they are cumbersome and error prone.
54

@@ -19,7 +18,7 @@ Thankfully, PHP 8 has done away with all that nonsense. Please welcome the follo
1918

2019
----------------------------------------------------------------------
2120

22-
Create a program the accepts two arguments, the first is a word, the second is a sentence (random words).
21+
Create a program that accepts two arguments, the first is a word, the second is a sentence (random words).
2322

2423
You must print a table which contains the results of each of the above functions with the provided word and sentence.
2524

@@ -38,7 +37,7 @@ The result column should be `true` or `false` based on the result of the corresp
3837

3938
* Simpler and more concise.
4039
* Saner return types.
41-
* It is harder to get their usage wrong, for example checking for 0 vs false with `strpos`
40+
* It is harder to get their usage wrong, for example checking for 0 vs false with `strpos`.
4241
* The functions are faster, being implemented in C.
4342
* The operations require less function calls, for example no usages of `strlen` are required.
4443

exercises/php-gets-a-promotion/initial/php-gets-a-promotion.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
class RowVisitor
44
{
5-
private $visitor;
5+
private \Closure $visitor;
66

7-
private $key;
7+
private string $key;
88

9-
protected $basePath;
9+
protected string $basePath;
1010

1111
public function __construct(\Closure $visitor, string $key, array $config = [])
1212
{

0 commit comments

Comments
 (0)