Movie_plugin::answer()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 19
nc 6
nop 1
1
<?php
2
3
namespace JarvisPHP\Plugins\Movie_plugin;
4
5
use JarvisPHP\Core\JarvisSession;
6
use JarvisPHP\Core\JarvisPHP;
7
use JarvisPHP\Core\JarvisLanguage;
8
use JarvisPHP\Core\JarvisTTS;
9
10
/**
11
 * A Movie plugin for reading a movie overview from TheMovieDb
12
 * @author Stefano Bianchini
13
 * @website http://www.stefanobianchini.net
14
 */
15
class Movie_plugin implements \JarvisPHP\Core\JarvisPluginInterface{
16
    /**
17
     * Priority of plugin
18
     * @var int  
19
     */
20
    var $priority = 2;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $priority.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
21
    
22
    /**
23
     * the behaviour of plugin
24
     * @param string $command
25
     */
26
    function answer($command) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
        $answer = '';
0 ignored issues
show
Unused Code introduced by
$answer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
        JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"');
29
30
        //Understand the movie
31
        preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command, $matches);
32
33
        $movie_title = end($matches);
34
35
        $_THEMOVIEDB_API_KEY = '';
36
37
        //Load API key from json config
38
        if(file_exists('Plugins/Movie_plugin/api-key.json')) {
39
            //Create your own api key and put it in api-key.json
40
            $json_config = json_decode(file_get_contents('Plugins/Movie_plugin/api-key.json'));
41
            $_THEMOVIEDB_API_KEY = $json_config->themoviedb_key;
42
        }
43
44
        $token  = new \Tmdb\ApiToken($_THEMOVIEDB_API_KEY);
45
        $client = new \Tmdb\Client($token, ['secure' => false]);
46
47
        $result = $client->getSearchApi()->searchMovies($movie_title, ['language'=>_LANGUAGE, 'page'=>1]);
0 ignored issues
show
Security Bug introduced by
It seems like $movie_title defined by end($matches) on line 33 can also be of type false; however, Tmdb\Api\Search::searchMovies() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
48
49
        if(count($result['results'])>0) {
50
            $answer = ($result['results'][0]['overview']) ? ($result['results'][0]['overview']) : JarvisLanguage::translate('no_results',get_called_class());
51
        } else {
52
            //No results
53
            $answer = JarvisLanguage::translate('no_results',get_called_class());
54
        }       
55
56
        JarvisTTS::speak($answer);
57
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
58
        $response->send();
59
    }
60
    
61
    /**
62
     * Get plugin's priority
63
     * @return int
64
     */
65
    function getPriority() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
        return $this->priority;
67
    }
68
    
69
    /**
70
     * Is it the right plugin for the command?
71
     * @param string $command
72
     * @return boolean
73
     */
74
    function isLikely($command) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
        return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return preg_match(\Jarvi...ed_class()), $command); (integer) is incompatible with the return type declared by the interface JarvisPHP\Core\JarvisPluginInterface::isLikely of type boolean.

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...
76
    }
77
    
78
    /**
79
     * Does the plugin need a session?
80
     * @return boolean
81
     */
82
    function hasSession() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
        return false;
84
    }
85
}
86