ActualOutsideTemperature_plugin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 1
cbo 4
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A answer() 0 18 1
A getPriority() 0 3 1
A isLikely() 0 3 1
A hasSession() 0 3 1
1
<?php
2
3
namespace JarvisPHP\Plugins\ActualOutsideTemperature_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
 * ActualOutsideTemperature_plugin
12
 *
13
 * @author Stefano Bianchini
14
 */
15
class ActualOutsideTemperature_plugin implements \JarvisPHP\Core\JarvisPluginInterface {
16
    /**
17
     * Priority of plugin
18
     * @var int  
19
     */
20
    var $priority = 4;
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
    var $place = "Rimini, Italy";
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $place.

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...
23
    
24
    /**
25
     * the behaviour of plugin
26
     * @param string $command
27
     */
28
    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...
29
        
30
        $BASE_URL = "http://query.yahooapis.com/v1/public/yql";
31
        $yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$this->place.'") and u="c"';
32
        $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
33
        
34
        // Make call with cURL
35
        $session = curl_init($yql_query_url);
36
        curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
37
        $json = curl_exec($session);
38
        // Convert JSON to PHP object
39
        $phpObj =  json_decode($json);
40
        $answer = sprintf(JarvisLanguage::translate('actual_temperature_outside_is',get_called_class()),$phpObj->query->results->channel->item->condition->temp, $phpObj->query->results->channel->atmosphere->humidity);
41
        JarvisTTS::speak($answer);
42
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
43
        $response->send();
44
        
45
    }
46
    
47
    /**
48
     * Get plugin's priority
49
     * @return int
50
     */
51
    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...
52
        return $this->priority;
53
    }
54
    
55
    /**
56
     * Is it the right plugin for the command?
57
     * @param string $command
58
     * @return boolean
59
     */
60
    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...
61
        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...
62
    }
63
    
64
    /**
65
     * Does the plugin need a session?
66
     * @return boolean
67
     */
68
    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...
69
        return false;
70
    }
71
}
72