Completed
Push — develop ( 41c2b1...ccfaa9 )
by Vadim
02:18
created

Google::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace AudioManager\Adapter;
4
5
use AudioManager\Adapter\Options\Google as Options;
6
7
/**
8
 * Google TTS adapter
9
 * @package AudioManager\Adapter
10
 * @method Options getOptions()
11
 */
12
class Google extends AbstractAdapter implements AdapterInterface
13
{
14
    /**
15
     * @param string $query
16
     * @return mixed
17
     */
18
    public function read($query)
19
    {
20
        $handle = $this->getHandle();
21
        $handle->setUrl($this->createUrl($query));
22
        $handle->setOption(CURLOPT_RETURNTRANSFER, 1);
23
        $response = $handle->execute();
24
        $this->setHeaders($handle->getInfo());
25
        return $response;
26
    }
27
28
    /**
29
     * Create url from options
30
     * @param string $query
31
     * @return string
32
     */
33
    protected function createUrl($query)
34
    {
35
        $options = $this->getOptions();
36
        $path = sprintf(
37
            'http://translate.google.com/translate_tts?ie=%s&tl=%s&q=%s',
38
            $options->hasEncoding() ? $options->getEncoding() : 'UTF-8',
39
            $options->getLanguage(),
40
            urlencode($query)
41
        );
42
        return $path;
43
    }
44
45
    /**
46
     * Get options object
47
     * @return Options
48
     */
49
    protected function initOptions()
50
    {
51
        return new Options();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \AudioManager\Adapter\Options\Google(); (AudioManager\Adapter\Options\Google) is incompatible with the return type declared by the abstract method AudioManager\Adapter\AbstractAdapter::initOptions of type AudioManager\Adapter\Options\OptionsInterface.

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...
52
    }
53
}
54