Completed
Push — master ( 0df352...724305 )
by John
04:42
created

AbstractCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 45.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 37
ccs 5
cts 11
cp 0.4545
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
getCommandText() 0 1 ?
A getPrompt() 0 4 1
A parseResponse() 0 14 2
1
<?php
2
3
/**
4
 * This file is part of graze/dynamark3-client.
5
 *
6
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://github.com/graze/dynamark3-client/blob/master/LICENSE
12
 * @link https://github.com/graze/dynamark3-client
13
 */
14
15
namespace Graze\Dynamark3Client\Command;
16
17
use Graze\Dynamark3Client\Command\CommandInterface;
18
use Graze\TelnetClient\TelnetResponseInterface;
19
use Graze\Dynamark3Client\Dynamark3Response;
20
use Graze\Dynamark3Client\Dynamark3Constants;
21
22
abstract class AbstractCommand implements CommandInterface
23
{
24
    /**
25
     * @return string
26
     */
27
    abstract public function getCommandText();
28
29
    /**
30
     * Default to the TelnetClient's prompt
31
     *
32
     * @return string
33
     */
34 2
    public function getPrompt()
35
    {
36 2
        return null;
37
    }
38
39
    /**
40
     * @param TelnetResponseInterface $response
41
     *
42
     * @return Graze\Dynamark3Client\Dynamark3Response
43
     */
44 3
    public function parseResponse(TelnetResponseInterface $response)
45
    {
46 3
        $errorCode = null;
47 3
        $prompt = reset($response->getPromptMatches());
0 ignored issues
show
Bug introduced by
$response->getPromptMatches() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
48
        if ($response->isError()) {
49
            // error prompt - ERROR nnn
50
            $errorCode = substr($prompt, 6);
51
        }
52
53
        return new Dynamark3Response(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Graze\Dynama...e($prompt, $errorCode); (Graze\Dynamark3Client\Dynamark3Response) is incompatible with the return type declared by the interface Graze\Dynamark3Client\Co...nterface::parseResponse of type Graze\Dynamark3Client\Co...lient\Dynamark3Response.

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...
54
            $prompt,
55
            $errorCode
56
        );
57
    }
58
}
59