Skip to content

Commit

Permalink
Merge pull request #45 from php-school/problem-file-updates
Browse files Browse the repository at this point in the history
Problem file updates for cloud compat
  • Loading branch information
AydinHassan authored Feb 9, 2023
2 parents a888a76 + 0789d83 commit d871ff8
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 184 deletions.
255 changes: 127 additions & 128 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions exercises/a-match-made-in-heaven/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
You have been given a piece of code (look for `a-match-made-in-heaven.php` in {{context cli 'your working directory'}} {{context cloud 'the editor'}}) which is full of bugs.
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.

The piece of code is supposed to take a string representing a keyboard keypress and convert it to its equivalent ANSI decimal code.
Expand All @@ -20,8 +20,7 @@ The key presses will be provided as strings via command line arguments. Only one
----------------------------------------------------------------------
## HINTS

Documentation on the `match` expression can be found by pointing your browser here:
[https://www.php.net/manual/en/control-structures.match.php]()
{{ doc match en control-structures.match.php }}

Remember the first argument will be a randomly picked supported keypress.

Expand Down
4 changes: 2 additions & 2 deletions exercises/a-safe-space-for-nulls/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ And finally, the most important part, all properties which may be `NULL` should

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.

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


----------------------------------------------------------------------
## EXTRA
Expand Down
6 changes: 2 additions & 4 deletions exercises/all-mixed-up/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ We will call your function automatically with a bunch of different types to make

We will specifically check that you used the `mixed` type. Omitting the type will not pass.

Documentation on the `mixed` type can be found by pointing your browser here:
[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.mixed]()
{{ doc 'mixed' en language.types.declarations.php#language.types.declarations.mixed }}

Documentation on `get_debug_type` can be found by pointing your browser here:
[https://www.php.net/manual/en/function.get-debug-type.php]()
{{ doc 'get_debug_type' en function.get-debug-type.php }}

----------------------------------------------------------------------
## EXTRA
Expand Down
6 changes: 3 additions & 3 deletions exercises/caution-with-catches/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For this exercise this function will always throw an exception but unfortunately

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"`.

PHP 8 allows you to handle the exception without capturing the exception itself which will ensure this message is not leaked further.
PHP 8 allows you to handle the exception without capturing the exception itself in to a variable, which will ensure this message is not leaked further.

### The advantages of non capturing catches

Expand All @@ -24,5 +24,5 @@ PHP 8 allows you to handle the exception without capturing the exception itself
----------------------------------------------------------------------
## HINTS

Documentation on the non-capturing catches feature is sparse without examples, so the RFC has the most amount of detail:
[https://wiki.php.net/rfc/non-capturing_catches]()
Documentation on the non-capturing catches feature is sparse and without examples, so the RFC has the most amount of detail:
[https://wiki.php.net/rfc/non-capturing_catches](https://wiki.php.net/rfc/non-capturing_catches)
11 changes: 4 additions & 7 deletions exercises/have-the-last-say/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ htmlspecialchars($string, ENT_COMPAT | ENT_HTML, 'UTF-8', false);

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.

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

```php
htmlspecialchars($string, double_encode: false);
Expand All @@ -51,14 +51,11 @@ You will most likely need a loop to process all the data in the file.

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.

Documentation on the `fopen` function can be found by pointing your browser here:
[https://www.php.net/manual/en/function.fopen.php]()
{{ doc 'fopen' en function.fopen.php }}

Documentation on the `fgetcsv` function can be found by pointing your browser here:
[https://www.php.net/manual/en/function.fgetcsv.php]()
{{ doc 'fgetcsv' en function.fgetcsv.php }}

Documentation on the `feof` function can be found by pointing your browser here:
[https://www.php.net/manual/en/function.feof.php]()
{{ doc 'feof' en function.feof.php }}

----------------------------------------------------------------------
## EXTRA
Expand Down
4 changes: 2 additions & 2 deletions exercises/infinite-divisions/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Based on those values you should print a specific message followed by a new line
----------------------------------------------------------------------
## HINTS

Documentation on the fdiv function can be found by pointing your browser here:
[https://www.php.net/manual/en/function.fdiv.php]()
{{ doc 'fdiv' en function.fdiv.php }}


16 changes: 11 additions & 5 deletions exercises/lord-of-the-strings/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ The result column should be `true` or `false` based on the result of the corresp
----------------------------------------------------------------------
## HINTS

{{ cli }}
Point your browser to [https://getcomposer.org/doc/00-intro.md](https://getcomposer.org/doc/00-intro.md) which will walk you through **Installing Composer** if you haven't already!

Use `composer init` to create your `composer.json` file with interactive search.
{{ cli }}

{{ cloud }}
Composer is installed and ready to go on cloud, use the `Composer Deps` button in the editor to search for and install your dependencies. While you should read the documentation for [Composer](https://getcomposer.org/doc/00-intro.md), it's important to note that the way we manage dependencies on PHP School cloud, is not how you would manage them in your own projects. We abstract away the `composer.json` file to keep it simple.
{{ cloud }}

For more details look at the docs for...

**Composer** - [https://getcomposer.org/doc/01-basic-usage.md](https://getcomposer.org/doc/01-basic-usage.md)
**Symfony Console** - [https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html)
**str_contains** - [https://www.php.net/manual/en/function.str-contains.php](https://www.php.net/manual/en/function.str-contains.php)
**str_starts_with** - [https://www.php.net/manual/en/function.str-starts-with.php](https://www.php.net/manual/en/function.str-starts-with.php)
**str_ends_with** - [https://www.php.net/manual/en/function.str-ends-with.php](https://www.php.net/manual/en/function.str-ends-with.php)
* **Composer** - [https://getcomposer.org/doc/01-basic-usage.md](https://getcomposer.org/doc/01-basic-usage.md)
* **Symfony Console** - [https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html)
* **str_contains** - [https://www.php.net/manual/en/function.str-contains.php](https://www.php.net/manual/en/function.str-contains.php)
* **str_starts_with** - [https://www.php.net/manual/en/function.str-starts-with.php](https://www.php.net/manual/en/function.str-starts-with.php)
* **str_ends_with** - [https://www.php.net/manual/en/function.str-ends-with.php](https://www.php.net/manual/en/function.str-ends-with.php)

For Symfony Console you will want to look specifically for the Table Helper.

Expand Down
11 changes: 5 additions & 6 deletions exercises/php-gets-a-promotion/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
You have been given a piece of code (look for `php-gets-a-promotion.php` in your working directory) which provides a class written for PHP 7.
You have been given a piece of code (look for `php-gets-a-promotion.php` in {{context cli 'your working directory'}} {{context cloud 'the editor'}}) which provides a class written for PHP 7.

The code itself works well, assigning constructor arguments to the class properties.

Expand All @@ -18,12 +18,11 @@ Focus on converting the constructor to a terse format using constructor property
----------------------------------------------------------------------
## HINTS

Documentation on the constructor property promotion feature can be found by pointing your browser here:
[https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion]()
{{ doc 'Constructor Property Promotion' en language.oop5.decon.php#language.oop5.decon.constructor.promotion }}

Remember to keep the same visibility for the properties
Remember to keep the same visibility for the properties.

You will be expected to make use of the constructor property promotion feature
You will be expected to make use of the constructor property promotion feature.

You should have less code than the provided initial code
You should have less code than the provided initial code.

12 changes: 5 additions & 7 deletions exercises/stringify-to-demystify/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
`__toString()` is a magic class method long-standing in PHP but never truly something you could rely on unless you rolled your own interface. All that has changed with the simple introduction of the `Stringable` interface in PHP 8.

`Stringable` is a simple interface that requires the implementation of `__toString(): string`
`Stringable` is a simple interface that requires the implementation of the method `__toString(): string`

----------------------------------------------------------------------

Expand Down Expand Up @@ -35,13 +35,11 @@ Your program may also receive successful payloads, which you should ignore for t

To easily read the request body you can use `file_get_contents('php://input')`

For more details look at the docs for...
{{ doc 'Stringable' en class.stringable.php }}

**Stringable** - [https://www.php.net/manual/en/class.stringable.php]()

Note that while the `Stringable` interface isn't required to pass the type hint check, however, it should be used if not only to show intent.
Note that while the `Stringable` interface isn't required to pass the type hint check (simply implementing the method `__toString` is enough), it should be used if not only to signal intent.

Oh, and don't forget about the basics for classes and interfaces :)

**class** - [https://www.php.net/manual/en/language.oop5.basic.php]()
**interfaces** - [https://www.php.net/manual/en/language.oop5.interfaces.php]()
* **class** - [https://www.php.net/manual/en/language.oop5.basic.php](https://www.php.net/manual/en/language.oop5.basic.php)
* **interfaces** - [https://www.php.net/manual/en/language.oop5.interfaces.php](https://www.php.net/manual/en/language.oop5.interfaces.php)
19 changes: 13 additions & 6 deletions exercises/the-attributes-of-success/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
You have been given a few pieces of code (look for `attributes.php`, `deserialize.php` & `the-attributes-of-success.php` in your working directory).
You have been given a few pieces of code (look for `attributes.php`, `deserialize.php` & `the-attributes-of-success.php` in {{context cli 'your working directory'}} {{context cloud 'the editor'}}).

Your entry point is `the-attributes-of-success.php`. This is the file you should edit and work on. The other files should not be modified. However, they are included by `the-attributes-of-success.php`.

{{ cli }}
You can run and verify your program like so:

```sh
$ {appname} run the-attributes-of-success.php
$ {appname} verify the-attributes-of-success.php
```
{{ cli }}

Your task is split into two sections. The overall task is to write a class using properties and attributes which describe how to map data to an instance of the class.
Your task is split in to two sections. The overall task is to write a class using properties and attributes which describe how to map data to an instance of the class.

The data will be passed to you in a JSON encoded string via the first command line argument.

Expand Down Expand Up @@ -68,13 +70,19 @@ Use the `Skip` attribute on the `id` property of our `Review` class to tell our

By now you should be able to call the `deserialize` function with the `JSON` data and your class name.

{{ cli }}
When executing your program with

```sh
$ {appname} run the-attributes-of-success.php
```

You should see a dump of your `Review` instance.
{{ cli }}

{{ cloud }}
When you execute your program by pressing `Run` you should see a dump of your `Review` instance.
{{ cloud }}

Here comes our problem: The reviewers name is not anonymous. We have to comply with strict privacy laws, we cannot display this data without the reviewer's permission.

Expand Down Expand Up @@ -126,19 +134,18 @@ The last task is to dump your object instance out using the PHP function `var_du
----------------------------------------------------------------------
## HINTS

Documentation on the Attributes feature can be found by pointing your browser here:
[https://www.php.net/manual/en/language.attributes.overview.php]()
{{ doc 'Attributes' en language.attributes.overview.php }}

Remember, do not edit `attributes.php` or `deserialize.php` - verification will fail if you do. Feel free to read the files to get a better understanding of the deserialization process.

You must call the `deserialize` function and you must use the `var_dump` function to output your deserialized object.

If you want to see the `JSON` data - use `var_dump` to dump it out.

For verification purposes, the order the properties defined in your `Review` class is important. Define them in the same order they are described to you.
For verification purposes, the order the properties are defined in your `Review` class is important. Define them in the same order they are described to you.

## Extra

If you're not sure how to access command line arguments - you should maybe try a different workshop which covers that topic. Try `learnyouphp`.
If you're not sure how to access command line arguments - you should maybe try a different workshop which covers that topic. Try the Learn you PHP workshop.

`json_decode` can fail if it is passed a malformed string. Wrap the decode in a `try\catch` statement and pass the `JSON_THROW_ON_ERROR` flag to `json_decode`'s fourth parameter.
12 changes: 9 additions & 3 deletions exercises/the-return-of-static/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
You have been given a piece of code (look for `the-return-of-static.php` in your working directory) which is using static return types.
You have been given a piece of code (look for `the-return-of-static.php` in {{context cli 'your working directory'}} {{context cloud 'the editor'}}) which is using static return types.

You will find two classes. `File`, a base class, and `Image` a class extending and adding behavior to `File`. We instantiate `Image`, set some properties using a fluent interface and then dump the object using `var_dump`.

{{ cli }}
If you run the code using `{appname} run the-return-of-static.php` you will see it is broken.
{{ cli }}

{{ cloud }}
If you run the code by clicking the `Run` button in the editor you will see it is broken.
{{ cloud }}

Locate and fix the issue!

Expand All @@ -14,8 +20,8 @@ Locate and fix the issue!
----------------------------------------------------------------------
## HINTS

(Brief) Documentation on the static return type feature can be found by pointing your browser here:
[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.static]()
(Brief) documentation on the static return type feature can be found by pointing your browser here:
[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.static](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.static)

The static return type enforces methods to return an instance of the class that the method was called from, rather than the one it was defined in.

9 changes: 4 additions & 5 deletions exercises/throw-an-expression/problem/problem.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
You have been given a piece of code (look for `throw-an-expression.php` in your working directory) which is checking the requested URL and throwing an exception when a secret area of the website is accessed.
You have been given a piece of code (look for `throw-an-expression.php` in {{context cli 'your working directory'}} {{context cloud 'the editor'}}) which is checking the requested URL and throwing an exception when a secret area of the website is accessed.

If the request is allowed, `Welcome!` is printed out.


Traditionally, pre PHP 8, an exception throw has been a statement. There are certain places where statements cannot be used, and only expressions can be used. For example, in ternaries and short closures, only expressions can be used.

Now with PHP 8, throw statements are expressions making them available to use in pretty much all locations.
Now with PHP 8, throw statements are expressions, making them available to use in pretty much all locations.

----------------------------------------------------------------------
Your task is to convert the `if` statement to one line of code, using the ternary operator.

### The advantages of throwing being an exception
### The advantages of throw being an exception

* It is possible to throw an exception in a short closure
* It is possible to throw an exception in a ternary or coalesce operation

----------------------------------------------------------------------
## HINTS

Documentation on throwing exception can be found by pointing your browser here:
[https://www.php.net/manual/en/language.exceptions.php]()
{{ doc 'throwing exceptions' en language.exceptions.php }}

3 changes: 1 addition & 2 deletions exercises/unite-the-types/problem/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ The function you implement must be called `adder`.

It is up to you to pass the numbers to your function.

Documentation on union types can be found by pointing your browser here:
[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.union]()
{{ doc 'union types' en language.types.declarations.php#language.types.declarations.union }}

----------------------------------------------------------------------
## EXTRA
Expand Down
8 changes: 7 additions & 1 deletion test/Exercise/LordOfTheStringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
use PhpSchool\PhpWorkshop\Application;
use PhpSchool\PhpWorkshop\Result\ComposerFailure;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
use PhpSchool\PhpWorkshop\TestUtils\WorkshopExerciseTest;
Expand All @@ -25,7 +26,12 @@ public function testWithNoComposerFile(): void
$this->runExercise('solution-no-code.php');

$this->assertVerifyWasNotSuccessful();
$this->assertResultsHasFailure(Failure::class, 'No composer.json file found');
$this->assertResultsHasFailureAndMatches(ComposerFailure::class, function (ComposerFailure $composerFailure) {
self::assertTrue($composerFailure->isMissingComponent());
self::assertEquals('composer.json', $composerFailure->getMissingComponent());

return true;
});
}

public function testWithNoCode(): void
Expand Down

0 comments on commit d871ff8

Please sign in to comment.