Info_plugin::isLikely()   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 1
1
<?php
2
3
namespace JarvisPHP\Plugins\Info_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
 * Info plugin
12
 * @author Stefano Bianchini
13
 * @website http://www.stefanobianchini.net
14
 */
15
class Info_plugin implements \JarvisPHP\Core\JarvisPluginInterface{
16
    /**
17
     * Priority of plugin
18
     * @var int  
19
     */
20
    var $priority = 1;
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...
Coding Style introduced by
answer uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
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
        if(preg_match(JarvisLanguage::translate('preg_match_tell_more',get_called_class()), $command)) {
29
            //Testing session
30
            JarvisPHP::getLogger()->debug('User says: '.$command);
31
            $answer = 'Ok, i am on '. php_uname();
32
            JarvisSession::terminate();
33
        }
34
        else {
35
            JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"');
36
            $answer = sprintf(JarvisLanguage::translate('my_name_is',get_called_class()),_SYSTEM_NAME, $_SERVER['SERVER_NAME'],$_SERVER['SERVER_ADDR']);
37
            
38
        }
39
        JarvisTTS::speak($answer);
40
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
41
        $response->send();
42
    }
43
    /**
44
     * Get plugin's priority
45
     * @return int
46
     */
47
    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...
48
        return $this->priority;
49
    }
50
    
51
    /**
52
     * Is it the right plugin for the command?
53
     * @param string $command
54
     * @return boolean
55
     */
56
    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...
57
        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...
58
    }
59
    
60
    /**
61
     * Does the plugin need a session?
62
     * @return boolean
63
     */
64
    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...
65
        return true;
66
    }
67
}
68