|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhp\Exercise; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Generator; |
|
6
|
|
|
use PhpParser\Parser; |
|
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\SelfCheck; |
|
13
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
|
14
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
|
15
|
|
|
use PhpSchool\PhpWorkshop\Input\Input; |
|
16
|
|
|
use PhpSchool\PhpWorkshop\Result\Failure; |
|
17
|
|
|
use PhpSchool\PhpWorkshop\Result\ResultInterface; |
|
18
|
|
|
use PhpSchool\PhpWorkshop\Result\Success; |
|
19
|
|
|
use PhpSchool\PhpWorkshop\Solution\DirectorySolution; |
|
20
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
|
21
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
22
|
|
|
use PhpParser\Node\Expr\Include_; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class ConcernedAboutSeparation |
|
26
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
|
27
|
|
|
* @author Aydin Hassan <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ConcernedAboutSeparation extends AbstractExercise implements ExerciseInterface, CliExercise, SelfCheck |
|
30
|
|
|
{ |
|
31
|
|
|
use TemporaryDirectoryTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Filesystem |
|
35
|
|
|
*/ |
|
36
|
|
|
private $filesystem; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Generator |
|
40
|
|
|
*/ |
|
41
|
|
|
private $faker; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Parser |
|
45
|
|
|
*/ |
|
46
|
|
|
private $parser; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Filesystem $filesystem |
|
50
|
|
|
* @param Generator $faker |
|
51
|
|
|
* @param Parser $parser |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker, Parser $parser) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->filesystem = $filesystem; |
|
56
|
|
|
$this->faker = $faker; |
|
57
|
|
|
$this->parser = $parser; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getName() |
|
64
|
|
|
{ |
|
65
|
|
|
return 'Concerned about Separation?'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getDescription() |
|
72
|
|
|
{ |
|
73
|
|
|
return 'Separate code and utilise files and classes'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getArgs() |
|
80
|
|
|
{ |
|
81
|
|
|
$folder = $this->getTemporaryPath(); |
|
82
|
|
|
|
|
83
|
|
|
$files = [ |
|
84
|
|
|
"learnyouphp.dat", |
|
85
|
|
|
"learnyouphp.txt", |
|
86
|
|
|
"learnyouphp.sql", |
|
87
|
|
|
"txt", |
|
88
|
|
|
"sql", |
|
89
|
|
|
"api.html", |
|
90
|
|
|
"html", |
|
91
|
|
|
"README.md", |
|
92
|
|
|
"CHANGELOG.md", |
|
93
|
|
|
"LICENCE.md", |
|
94
|
|
|
"md", |
|
95
|
|
|
"data.json", |
|
96
|
|
|
"json", |
|
97
|
|
|
"data.dat", |
|
98
|
|
|
"words.dat", |
|
99
|
|
|
"w00t.dat", |
|
100
|
|
|
"w00t.txt", |
|
101
|
|
|
"wrrrrongdat", |
|
102
|
|
|
"dat", |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
$this->filesystem->mkdir($folder); |
|
106
|
|
|
array_walk($files, function ($file) use ($folder) { |
|
107
|
|
|
$this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), ''); |
|
108
|
|
|
}); |
|
109
|
|
|
|
|
110
|
|
|
$ext = ''; |
|
111
|
|
|
while ($ext === '') { |
|
112
|
|
|
$index = array_rand($files); |
|
113
|
|
|
$ext = pathinfo($files[$index], PATHINFO_EXTENSION); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return [$folder, $ext]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return SolutionInterface |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getSolution() |
|
123
|
|
|
{ |
|
124
|
|
|
return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/concerned-about-separation/solution'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return null |
|
129
|
|
|
*/ |
|
130
|
|
|
public function tearDown() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param Input $input |
|
137
|
|
|
* @return ResultInterface |
|
138
|
|
|
*/ |
|
139
|
|
|
public function check(Input $input) |
|
140
|
|
|
{ |
|
141
|
|
|
$statements = $this->parser->parse(file_get_contents($input->getArgument('program'))); |
|
142
|
|
|
|
|
143
|
|
|
$include = null; |
|
144
|
|
|
foreach ($statements as $statement) { |
|
|
|
|
|
|
145
|
|
|
if ($statement instanceof Include_) { |
|
146
|
|
|
$include = $statement; |
|
147
|
|
|
break; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (null === $include) { |
|
152
|
|
|
return Failure::fromNameAndReason($this->getName(), 'No require statement found'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return new Success($this->getName()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return ExerciseType |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getType() |
|
162
|
|
|
{ |
|
163
|
|
|
return ExerciseType::CLI(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.