Completed
Push — master ( 48a5d3...028360 )
by Aydin
03:14
created

DependencyHeaven::getType()   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\Check\ComposerCheck;
7
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
8
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
9
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
10
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11
use PhpSchool\PhpWorkshop\ExerciseCheck\CgiOutputExerciseCheck;
12
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
13
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
14
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
15
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
16
use Zend\Diactoros\Request;
17
18
/**
19
 * Class DependencyHeaven
20
 * @package PhpSchool\LearnYouPhp\Exercise
21
 * @author Michael Woodward <[email protected]>
22
 */
23
class DependencyHeaven extends AbstractExercise implements
24
    ExerciseInterface,
25
    CgiExercise,
26
    ComposerExerciseCheck
27
{
28
    /**
29
     * @var Generator
30
     */
31
    private $faker;
32
33
    /**
34
     * @param Generator $faker
35
     */
36
    public function __construct(Generator $faker)
37
    {
38
        $this->faker = $faker;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getName()
45
    {
46
        return 'Dependency Heaven';
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getDescription()
53
    {
54
        return 'An introduction to Composer dependency management';
55
    }
56
57
    /**
58
     * @return SolutionInterface
59
     */
60
    public function getSolution()
61
    {
62
        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...
63
    }
64
65
    /**
66
     * @return RequestInterface[]
67
     */
68
    public function getRequests()
69
    {
70
        $requests = [];
71
72
        for ($i = 0; $i < rand(2, 5); $i++) {
73
            $requests[] = $this->newApiRequest('/reverse');
74
            $requests[] = $this->newApiRequest('/swapcase');
75
            $requests[] = $this->newApiRequest('/titleize');
76
        }
77
78
        return $requests;
79
    }
80
81
    /**
82
     * @param string $endpoint
83
     * @return RequestInterface
84
     */
85
    private function newApiRequest($endpoint)
86
    {
87
        $request = (new Request($endpoint))
88
            ->withMethod('POST')
89
            ->withHeader('Content-Type', 'application/x-www-form-urlencoded');
90
91
        $request->getBody()->write(
92
            http_build_query(['data' => $this->faker->sentence(rand(3, 6))])
93
        );
94
95
        return $request;
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function getRequiredPackages()
102
    {
103
        return [
104
            'klein/klein',
105
            'danielstjules/stringy'
106
        ];
107
    }
108
109
    /**
110
     * @return ExerciseType
111
     */
112
    public function getType()
113
    {
114
        return ExerciseType::CGI();
115
    }
116
117
    /**
118
     * @param ExerciseDispatcher $dispatcher
119
     */
120
    public function configure(ExerciseDispatcher $dispatcher)
121
    {
122
        $dispatcher->requireCheck(ComposerCheck::class, $dispatcher::CHECK_BEFORE);
123
    }
124
}
125