MyFirstIo::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
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
Bug introduced by
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];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($path); (string[]) is incompatible with the return type declared by the interface PhpSchool\PhpWorkshop\Ex...se\CliExercise::getArgs of type string[][].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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);
115
    }
116
}
117