Weather_plugin   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 13.19 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 13
c 3
b 0
f 1
lcom 0
cbo 7
dl 12
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C answer() 12 54 10
A getPriority() 0 3 1
A isLikely() 0 3 1
A hasSession() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JarvisPHP\Plugins\Weather_plugin;
4
5
use JarvisPHP\Core\JarvisSession;
6
use JarvisPHP\Core\JarvisPHP;
7
use JarvisPHP\Core\JarvisLanguage;
8
use JarvisPHP\Core\JarvisTTS;
9
//OpenWeatherMap
10
use Cmfcmf\OpenWeatherMap;
11
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
12
13
/**
14
 * A Weather plugin for today / tomorrow forecast
15
 * @author Stefano Bianchini
16
 * @website http://www.stefanobianchini.net
17
 */
18
class Weather_plugin implements \JarvisPHP\Core\JarvisPluginInterface{
19
    /**
20
     * Priority of plugin
21
     * @var int  
22
     */
23
    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...
24
    
25
    /**
26
     * the behaviour of plugin
27
     * @param string $command
28
     */
29
    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...
30
        $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...
31
        JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"');
32
        
33
        $owm = new OpenWeatherMap();
34
35
        $tomorrow = new \DateTime();
36
        $tomorrow->modify('+1 day');
37
38
        $_OPENWEATHERMAP_API_KEY = '';
39
40
        //Load API key from json config
41
        if(file_exists('Plugins/Weather_plugin/api-key.json')) {
42
            //Create your own api key and put it in api-key.json
43
            $json_config = json_decode(file_get_contents('Plugins/Weather_plugin/api-key.json'));
44
            $_OPENWEATHERMAP_API_KEY = $json_config->openweathermap_key;
45
        }
46
47
        try {
48
            $forecast = $owm->getWeatherForecast('Rimini', 'metric', _LANGUAGE, $_OPENWEATHERMAP_API_KEY,2);
49
        } catch(OWMException $e) {
50
            JarvisPHP::getLogger()->error('OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
51
            $answer = JarvisLanguage::translate('weather_error',get_called_class());            
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...
52
        } catch(\Exception $e) {
53
            JarvisPHP::getLogger()->error('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
54
            $answer = JarvisLanguage::translate('weather_error',get_called_class());
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...
55
        }
56
57
        if(preg_match(JarvisLanguage::translate('preg_match_today',get_called_class()), $command)) {
58
            $answer = JarvisLanguage::translate('forecast_for_today',get_called_class());
59 View Code Duplication
            foreach ($forecast as $weather) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                if($weather->time->day->format('d.m.Y')==date('d.m.Y')) {
61
                    $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": ";
62
                    $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('&deg;C','',$weather->temperature)));
63
                }
64
            }
65
        } else if(preg_match(JarvisLanguage::translate('preg_match_tomorrow',get_called_class()), $command)) {
66
            $answer = JarvisLanguage::translate('forecast_for_tomorrow',get_called_class());
67 View Code Duplication
            foreach ($forecast as $weather) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                if($weather->time->day->format('d.m.Y')==$tomorrow->format('d.m.Y')) {
69
                    $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": ";
70
                    $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('&deg;C','',$weather->temperature)));
71
                }
72
            }
73
        } else {
74
            $answer = JarvisLanguage::translate('not_understand_weather_day',get_called_class());
75
        }
76
77
        
78
79
        JarvisTTS::speak($answer);
80
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
81
        $response->send();
82
    }
83
    
84
    /**
85
     * Get plugin's priority
86
     * @return int
87
     */
88
    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...
89
        return $this->priority;
90
    }
91
    
92
    /**
93
     * Is it the right plugin for the command?
94
     * @param string $command
95
     * @return boolean
96
     */
97
    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...
98
        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...
99
    }
100
    
101
    /**
102
     * Does the plugin need a session?
103
     * @return boolean
104
     */
105
    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...
106
        return false;
107
    }
108
}
109