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 | /** |
||
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); |
||
0 ignored issues
–
show
It seems like
$paragraphs defined by $this->faker->paragraphs(rand(5, 50), true) on line 71 can also be of type array ; however, Symfony\Component\Filesy...\Filesystem::dumpFile() does only seem to accept string , maybe add an additional type check?
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: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.
Loading history...
|
|||
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, $dispatcher::CHECK_AFTER); |
||
115 | } |
||
116 | } |
||
117 |
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.