1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
10
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
11
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
12
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
13
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
14
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MyFirstIo |
19
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
20
|
|
|
* @author Aydin Hassan <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class MyFirstIo extends AbstractExercise implements |
23
|
|
|
ExerciseInterface, |
24
|
|
|
CliExercise, |
25
|
|
|
FunctionRequirementsExerciseCheck |
26
|
|
|
{ |
27
|
|
|
use TemporaryDirectoryTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Filesystem |
31
|
|
|
*/ |
32
|
|
|
private $filesystem; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Generator |
36
|
|
|
*/ |
37
|
|
|
private $faker; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Filesystem $filesystem |
41
|
|
|
* @param Generator $faker |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker) |
44
|
|
|
{ |
45
|
|
|
$this->filesystem = $filesystem; |
46
|
|
|
$this->faker = $faker; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getName() |
53
|
|
|
{ |
54
|
|
|
return 'My First IO'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getDescription() |
61
|
|
|
{ |
62
|
|
|
return 'Read a file from the file system'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getArgs() |
69
|
|
|
{ |
70
|
|
|
$path = $this->getTemporaryPath(); |
71
|
|
|
$paragraphs = $this->faker->paragraphs(rand(5, 50), true); |
72
|
|
|
$this->filesystem->dumpFile($path, $paragraphs); |
73
|
|
|
|
74
|
|
|
return [$path]; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function tearDown() |
81
|
|
|
{ |
82
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string[] |
87
|
|
|
*/ |
88
|
|
|
public function getRequiredFunctions() |
89
|
|
|
{ |
90
|
|
|
return ['file_get_contents']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
public function getBannedFunctions() |
97
|
|
|
{ |
98
|
|
|
return ['file']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return ExerciseType |
103
|
|
|
*/ |
104
|
|
|
public function getType() |
105
|
|
|
{ |
106
|
|
|
return ExerciseType::CLI(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ExerciseDispatcher $dispatcher |
111
|
|
|
*/ |
112
|
|
|
public function configure(ExerciseDispatcher $dispatcher) |
113
|
|
|
{ |
114
|
|
|
$dispatcher->requireCheck(FunctionRequirementsCheck::class); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.