Passed
Push — master ( 3b900d...5746d6 )
by Harry Osmar
31s
created

Response   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A asJSON() 0 7 1
A asHTML() 0 7 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/13/18
6
 * Time: 1:15 PM
7
 */
8
9
namespace PhpBootstrap\Services;
10
11
use PhpBootstrap\Contracts\Response as ResponseInterface;
12
use Zend\Diactoros\MessageTrait;
13
use Zend\Diactoros\Response\InjectContentTypeTrait;
14
15
/**
16
 * Class Response
17
 * @package PhpBootstrap\Services
18
 * Just extend the \PhpRestfulApiResponse\Response from https://github.com/harryosmar/php-restful-api-response
19
 */
20
class Response extends \PhpRestfulApiResponse\Response implements ResponseInterface
21
{
22
    use MessageTrait, InjectContentTypeTrait;
23
24
    /**
25
     * Response constructor.
26
     * @param string $body
27
     * @param int $status
28
     * @param int $errorCode
29
     * @param array $headers
30
     */
31
    public function __construct($body = 'php://memory', int $status = 200, $errorCode = null, array $headers = [])
32
    {
33
        $this->setStatusCode($status);
0 ignored issues
show
Bug introduced by
The method setStatusCode() cannot be called from this context as it is declared private in class PhpRestfulApiResponse\Response.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
34
        $this->setErrorCode($errorCode);
35
        $this->stream = $this->getStream($body, 'wb+');
0 ignored issues
show
Bug introduced by
The property stream cannot be accessed from this context as it is declared private in class Zend\Diactoros\MessageTrait.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
36
        $this->setHeaders($this->headers);
37
    }
38
39
    /**
40
     * @return Response
41
     */
42
    public function asJSON() : ResponseInterface
43
    {
44
        $headers = $this->injectContentType('application/json', $this->headers);
45
        $this->setHeaders($headers);
46
47
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PhpBootstrap\Services\Response) is incompatible with the return type declared by the interface PhpBootstrap\Contracts\Response::asJSON of type self.

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...
48
    }
49
50
    /**
51
     * @return Response
52
     */
53
    public function asHTML() : ResponseInterface
54
    {
55
        $headers = $this->injectContentType('text/html', $this->headers);
56
        $this->setHeaders($headers);
57
58
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PhpBootstrap\Services\Response) is incompatible with the return type declared by the interface PhpBootstrap\Contracts\Response::asHTML of type self.

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
}