These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
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 | View Code Duplication | class ExceptionalCoding extends AbstractExercise implements |
|
18 | ExerciseInterface, |
||
19 | CliExercise, |
||
20 | FunctionRequirementsExerciseCheck |
||
21 | { |
||
22 | use TemporaryDirectoryTrait; |
||
23 | |||
24 | /** |
||
25 | * @var Filesystem |
||
26 | */ |
||
27 | private $filesystem; |
||
28 | |||
29 | /** |
||
30 | * @var Generator |
||
31 | */ |
||
32 | private $faker; |
||
33 | |||
34 | /** |
||
35 | * @param Filesystem $filesystem |
||
36 | * @param Generator $faker |
||
37 | */ |
||
38 | public function __construct(Filesystem $filesystem, Generator $faker) |
||
39 | { |
||
40 | $this->filesystem = $filesystem; |
||
41 | $this->faker = $faker; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getName() |
||
48 | { |
||
49 | return "Exceptional Coding"; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getDescription() |
||
56 | { |
||
57 | return "Introduction to Exceptions"; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getArgs() |
||
64 | { |
||
65 | $this->filesystem->mkdir($this->getTemporaryPath()); |
||
66 | |||
67 | $fileCount = rand(2, 10); |
||
68 | $realFiles = rand(1, $fileCount - 1); |
||
69 | |||
70 | $files = []; |
||
71 | foreach (range(1, $fileCount) as $index) { |
||
72 | $file = sprintf('%s/%s.txt', $this->getTemporaryPath(), $this->faker->uuid); |
||
73 | if ($index <= $realFiles) { |
||
74 | $this->filesystem->touch($file); |
||
75 | } |
||
76 | $files[] = $file; |
||
77 | } |
||
78 | |||
79 | return $files; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return void |
||
84 | */ |
||
85 | public function tearDown() |
||
86 | { |
||
87 | $this->filesystem->remove($this->getTemporaryPath()); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string[] |
||
92 | */ |
||
93 | public function getRequiredFunctions() |
||
94 | { |
||
95 | return []; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return string[] |
||
100 | */ |
||
101 | public function getBannedFunctions() |
||
102 | { |
||
103 | return ['array_filter', 'file_exists']; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return ExerciseType |
||
108 | */ |
||
109 | public function getType() |
||
110 | { |
||
111 | return ExerciseType::CLI(); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param ExerciseDispatcher $dispatcher |
||
116 | */ |
||
117 | public function configure(ExerciseDispatcher $dispatcher) |
||
118 | { |
||
119 | $dispatcher->requireCheck(FunctionRequirementsCheck::class, $dispatcher::CHECK_AFTER); |
||
120 | } |
||
121 | } |
||
122 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.