Completed
Push — master ( b0dd07...48a5d3 )
by Aydin
02:14
created

DependencyHeaven::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\ExerciseCheck\CgiOutputExerciseCheck;
9
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
10
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
11
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
12
use Zend\Diactoros\Request;
13
14
/**
15
 * Class DependencyHeaven
16
 * @package PhpSchool\LearnYouPhp\Exercise
17
 * @author Michael Woodward <[email protected]>
18
 */
19
class DependencyHeaven extends AbstractExercise implements
20
    ExerciseInterface,
21
    CgiOutputExerciseCheck,
22
    ComposerExerciseCheck
23
{
24
    /**
25
     * @var Generator
26
     */
27
    private $faker;
28
29
    /**
30
     * @param Generator $faker
31
     */
32
    public function __construct(Generator $faker)
33
    {
34
        $this->faker = $faker;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getName()
41
    {
42
        return 'Dependency Heaven';
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getDescription()
49
    {
50
        return 'An introduction to Composer dependency management';
51
    }
52
53
    /**
54
     * @return SolutionInterface
55
     */
56
    public function getSolution()
57
    {
58
        return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/dependency-heaven/solution');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \PhpSchool\PhpWor...ency-heaven/solution'); (PhpSchool\PhpWorkshop\Solution\DirectorySolution) is incompatible with the return type of the parent method PhpSchool\PhpWorkshop\Ex...ctExercise::getSolution of type PhpSchool\PhpWorkshop\Solution\SingleFileSolution.

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...
59
    }
60
61
    /**
62
     * @return RequestInterface[]
63
     */
64
    public function getRequests()
65
    {
66
        $requests = [];
67
68
        for ($i = 0; $i < rand(2, 5); $i++) {
69
            $requests[] = $this->newApiRequest('/reverse');
70
            $requests[] = $this->newApiRequest('/swapcase');
71
            $requests[] = $this->newApiRequest('/titleize');
72
        }
73
74
        return $requests;
75
    }
76
77
    /**
78
     * @param string $endpoint
79
     * @return RequestInterface
80
     */
81
    private function newApiRequest($endpoint)
82
    {
83
        $request = (new Request($endpoint))
84
            ->withMethod('POST')
85
            ->withHeader('Content-Type', 'application/x-www-form-urlencoded');
86
87
        $request->getBody()->write(
88
            http_build_query(['data' => $this->faker->sentence(rand(3, 6))])
89
        );
90
91
        return $request;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getRequiredPackages()
98
    {
99
        return [
100
            'klein/klein',
101
            'danielstjules/stringy'
102
        ];
103
    }
104
}
105