|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Generator; |
|
6
|
|
|
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
|
7
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
|
8
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
|
9
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
|
10
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
|
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class MyFirstIo |
|
15
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
|
16
|
|
|
* @author Aydin Hassan <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class MyFirstIo extends AbstractExercise implements |
|
19
|
|
|
ExerciseInterface, |
|
20
|
|
|
StdOutExerciseCheck, |
|
21
|
|
|
FunctionRequirementsExerciseCheck |
|
22
|
|
|
{ |
|
23
|
|
|
use TemporaryDirectoryTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Filesystem |
|
27
|
|
|
*/ |
|
28
|
|
|
private $filesystem; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Generator |
|
32
|
|
|
*/ |
|
33
|
|
|
private $faker; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Filesystem $filesystem |
|
37
|
|
|
* @param Generator $faker |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->filesystem = $filesystem; |
|
42
|
|
|
$this->faker = $faker; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getName() |
|
49
|
|
|
{ |
|
50
|
|
|
return 'My First IO'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getDescription() |
|
57
|
|
|
{ |
|
58
|
|
|
return 'Read a file from the file system'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getArgs() |
|
65
|
|
|
{ |
|
66
|
|
|
$path = $this->getTemporaryPath(); |
|
67
|
|
|
$paragraphs = $this->faker->paragraphs(rand(5, 50), true); |
|
68
|
|
|
$this->filesystem->dumpFile($path, $paragraphs); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
return [$path]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return null |
|
75
|
|
|
*/ |
|
76
|
|
|
public function tearDown() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string[] |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getRequiredFunctions() |
|
85
|
|
|
{ |
|
86
|
|
|
return ['file_get_contents']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string[] |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getBannedFunctions() |
|
93
|
|
|
{ |
|
94
|
|
|
return ['file']; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.