Skip to content

Commit 20204e5

Browse files
authored
Merge pull request #16 from php-school/constructor-property-prmomtion
Constructor property prmomtion
2 parents 599bf0b + 41ef1a1 commit 20204e5

22 files changed

+603
-30
lines changed

app/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
use PhpSchool\PHP8Appreciate\Exercise\AMatchMadeInHeaven;
2323
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
24+
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
2425
use PhpSchool\PhpWorkshop\Application;
2526

2627
$app = new Application('PHP8 Appreciate', __DIR__ . '/config.php');
2728

2829
$app->addExercise(AMatchMadeInHeaven::class);
2930
$app->addExercise(HaveTheLastSay::class);
31+
$app->addExercise(PhpPromotion::class);
3032

3133
$art = <<<ART
3234
_ __ _

app/config.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use PhpSchool\PHP8Appreciate\AstService;
44
use PhpSchool\PHP8Appreciate\Exercise\AMatchMadeInHeaven;
55
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
6+
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
67
use Psr\Container\ContainerInterface;
78
use function DI\create;
89
use function DI\factory;
@@ -18,5 +19,7 @@
1819
HaveTheLastSay::class => function (ContainerInterface $c) {
1920
return new HaveTheLastSay($c->get(PhpParser\Parser::class));
2021
},
21-
22+
PhpPromotion::class => function (ContainerInterface $c) {
23+
return new PhpPromotion($c->get(PhpParser\Parser::class));
24+
},
2225
];

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"require-dev": {
2727
"phpunit/phpunit": "^9",
2828
"squizlabs/php_codesniffer": "^3.5",
29-
"phpstan/phpstan": "^0.12.52"
29+
"phpstan/phpstan": "^0.12.52",
30+
"timeweb/phpstan-enum": "^2.2"
3031
},
3132
"autoload": {
3233
"psr-4": {

composer.lock

Lines changed: 67 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class RowVisitor
4+
{
5+
private $visitor;
6+
7+
private $key;
8+
9+
protected $basePath;
10+
11+
public function __construct(\Closure $visitor, string $key, array $config = [])
12+
{
13+
$this->visitor = $visitor;
14+
$this->key = $key;
15+
$this->basePath = $config['basePath'] ?? '';
16+
}
17+
18+
public function readCsv(string $filePath)
19+
{
20+
// noop
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
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.
2+
The code itself works well, assigning constructor arguments to the class properties. Your job is to write the code in a more terse format, by using the newly introduced
3+
constructor property promotion in PHP 8.
4+
5+
The code is a class that would hypothetically be used to call a provided `\Closure` when reading a row of a CSV, passing in the appropriate cell value, but the implementation logic is not important and thus left out.
6+
7+
Focus on converting the constructor to a terse format using constructor property promotion.
8+
9+
### The advantages of constructor property promotion
10+
11+
* Less boilerplate code is required when writing a class
12+
* It still provides the required information for static analysis / runtime type parsing
13+
* Is compatible in conjunction with properties that cannot be promoted
14+
15+
16+
----------------------------------------------------------------------
17+
## HINTS
18+
19+
Documentation on the constructor property promotion feature can be found by pointing your browser here:
20+
[https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion]()
21+
22+
Remember to keep the same visibility for the properties
23+
24+
You will be expected to make use of the constructor property promotion feature
25+
26+
You should have less code than the provided initial code
27+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class RowVisitor
4+
{
5+
protected string $basePath;
6+
7+
public function __construct(
8+
private \Closure $visitor,
9+
private string $key,
10+
array $config = []
11+
) {
12+
$this->basePath = $config['basePath'] ?? '';
13+
}
14+
15+
public function readCsv(string $filePath)
16+
{
17+
// noop
18+
}
19+
}

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ parameters:
77

88
excludes_analyse:
99
- src/TestUtils/WorkshopExerciseTest.php
10+
11+
includes:
12+
- vendor/timeweb/phpstan-enum/extension.neon

phpunit.xml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit colors="true">
4-
<testsuite name="My PHP Workshop Test Suite">
5-
<directory>./test</directory>
6-
</testsuite>
7-
<filter>
8-
<whitelist addUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">./src</directory>
10-
</whitelist>
11-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage includeUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuite name="My PHP Workshop Test Suite">
9+
<directory>./test</directory>
10+
</testsuite>
1211
</phpunit>

src/Exercise/HaveTheLastSay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function tearDown(): void
8989

9090
public function getType(): ExerciseType
9191
{
92-
return new ExerciseType(ExerciseType::CLI);
92+
return ExerciseType::CLI();
9393
}
9494

9595
public function getRequiredFunctions(): array

0 commit comments

Comments
 (0)