Completed
Push — master ( 2ff2f4...64e7ee )
by Aydin
02:08
created

MyFirstIo::getSolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $paragraphs defined by $this->faker->paragraphs(rand(5, 50), true) on line 67 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...
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