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\ExerciseInterface; |
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
10
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
11
|
|
|
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck; |
12
|
|
|
use PhpSchool\PhpWorkshop\Result\Failure; |
13
|
|
|
use PhpSchool\PhpWorkshop\Result\ResultInterface; |
14
|
|
|
use PhpSchool\PhpWorkshop\Result\Success; |
15
|
|
|
use PhpSchool\PhpWorkshop\Solution\DirectorySolution; |
16
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
18
|
|
|
use PhpParser\Node\Expr\Include_; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ConcernedAboutSeparation |
22
|
|
|
* @package PhpSchool\LearnYouPhp\Exercise |
23
|
|
|
* @author Aydin Hassan <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ConcernedAboutSeparation extends AbstractExercise implements |
26
|
|
|
ExerciseInterface, |
27
|
|
|
StdOutExerciseCheck, |
28
|
|
|
SelfCheck |
29
|
|
|
{ |
30
|
|
|
use TemporaryDirectoryTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Filesystem |
34
|
|
|
*/ |
35
|
|
|
private $filesystem; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Generator |
39
|
|
|
*/ |
40
|
|
|
private $faker; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Parser |
44
|
|
|
*/ |
45
|
|
|
private $parser; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Filesystem $filesystem |
49
|
|
|
* @param Generator $faker |
50
|
|
|
* @param Parser $parser |
51
|
|
|
*/ |
52
|
|
|
public function __construct(Filesystem $filesystem, Generator $faker, Parser $parser) |
53
|
|
|
{ |
54
|
|
|
$this->filesystem = $filesystem; |
55
|
|
|
$this->faker = $faker; |
56
|
|
|
$this->parser = $parser; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getName() |
63
|
|
|
{ |
64
|
|
|
return 'Concerned about Separation?'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getDescription() |
71
|
|
|
{ |
72
|
|
|
return 'Separate code and utilise files and classes'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function getArgs() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$folder = $this->getTemporaryPath(); |
81
|
|
|
|
82
|
|
|
$files = [ |
83
|
|
|
"learnyouphp.dat", |
84
|
|
|
"learnyouphp.txt", |
85
|
|
|
"learnyouphp.sql", |
86
|
|
|
"api.html", |
87
|
|
|
"README.md", |
88
|
|
|
"CHANGELOG.md", |
89
|
|
|
"LICENCE.md", |
90
|
|
|
"md", |
91
|
|
|
"data.json", |
92
|
|
|
"data.dat", |
93
|
|
|
"words.dat", |
94
|
|
|
"w00t.dat", |
95
|
|
|
"w00t.txt", |
96
|
|
|
"wrrrrongdat", |
97
|
|
|
"dat", |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$this->filesystem->mkdir($folder); |
101
|
|
|
array_walk($files, function ($file) use ($folder) { |
102
|
|
|
$this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), ''); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
$ext = ''; |
106
|
|
|
while ($ext === '') { |
107
|
|
|
$index = array_rand($files); |
108
|
|
|
$ext = pathinfo($files[$index], PATHINFO_EXTENSION); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return [$folder, $ext]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return SolutionInterface |
116
|
|
|
*/ |
117
|
|
|
public function getSolution() |
118
|
|
|
{ |
119
|
|
|
return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/concerned-about-separation/solution'); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return null |
124
|
|
|
*/ |
125
|
|
|
public function tearDown() |
126
|
|
|
{ |
127
|
|
|
$this->filesystem->remove($this->getTemporaryPath()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $fileName |
132
|
|
|
* @return ResultInterface |
133
|
|
|
*/ |
134
|
|
|
public function check($fileName) |
135
|
|
|
{ |
136
|
|
|
$statements = $this->parser->parse(file_get_contents($fileName)); |
137
|
|
|
|
138
|
|
|
$include = null; |
139
|
|
|
foreach ($statements as $statement) { |
|
|
|
|
140
|
|
|
if ($statement instanceof Include_) { |
141
|
|
|
$include = $statement; |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null === $include) { |
147
|
|
|
return Failure::fromNameAndReason($this->getName(), 'No require statement found'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return new Success($this->getName()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.