InformationOn_plugin::getPriority()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace JarvisPHP\Plugins\InformationOn_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 simple InformationOn plugin that uses Wikipedia Opensearch API
12
 * @author Stefano Bianchini
13
 * @website http://www.stefanobianchini.net
14
 * Based on Wiki_plugin of UgoRaffaele https://github.com/UgoRaffaele/
15
 */
16
class InformationOn_plugin implements \JarvisPHP\Core\JarvisPluginInterface{
17
    /**
18
     * Priority of plugin
19
     * @var int  
20
     */
21
    var $priority = 10;
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...
22
    
23
    /**
24
     * the behaviour of plugin
25
     * @param string $command
26
     */
27
    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...
28
        //Extract search term
29
        preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command, $matches);
30
        $search_term = $matches[2];
31
		$wiki_query_url = _WIKI_URL . "?action=opensearch&search=" . urlencode($search_term) . "&format=xml&limit=5";
32
		$xml = simplexml_load_string(file_get_contents($wiki_query_url));
33
        $item_array = $xml->Section->Item;
34
        $answer = JarvisLanguage::translate('nothing_found',get_called_class());
35
        //Have i got results?
36
        if(count($item_array)>0) {
37
            foreach ($item_array as $item) {
38
                if (strlen($item->Description) > 0) {
39
                    $answer = sprintf(JarvisLanguage::translate('search_result_is',get_called_class()), $item->Description);
40
                    break;
41
                }
42
            }
43
        }
44
        //Making response
45
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisTTS::speak($answer), JarvisPHP::getRealClassName(get_called_class()), true);
0 ignored issues
show
Unused Code introduced by
The call to JarvisResponse::__construct() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
        $response->send();
47
    }
48
    
49
    /**
50
     * Get plugin's priority
51
     * @return int
52
     */
53
    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...
54
        return $this->priority;
55
    }
56
    
57
    /**
58
     * Is it the right plugin for the command?
59
     * @param string $command
60
     * @return boolean
61
     */
62
    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...
63
        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...
64
    }
65
    
66
    /**
67
     * Does the plugin need a session?
68
     * @return boolean
69
     */
70
    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...
71
        return false;
72
    }
73
}
74