HaddockPositron::isPositronTriggered()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Posibrain\Positron\Haddock;
3
4
use Monolog\Logger;
5
use Posibrain\Positron\Positron;
6
use Posibrain\TchatBotConfig;
7
use Posibrain\AnalysedRequest;
8
use Posibrain\TchatMessage;
9
10
/**
11
 *
12
 * @author Fylhan (http://fylhan.la-bnbox.fr)
13
 * @license LGPL-2.1+
14
 */
15
class HaddockPositron extends Positron
16
{
17
18
    private static $logger = NULL;
19
20
    private $config;
21
22
    private $insults;
23
24
    private $nbOfInsults;
25
26
    private $ponctuations;
27
28
    private $nbOfPonctuations;
29
30
    private $internalPonctuations;
31
32
    private $nbOfInternalPonctuations;
33
34
    public function __construct($config, $params = array())
35
    {
36
        // Logger
37 View Code Duplication
        if (NULL == self::$logger) {
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...
38
            self::$logger = new Logger(__CLASS__);
39
            if (! empty($params) && isset($params['loggerHandler'])) {
40
                self::$logger->pushHandler($params['loggerHandler']);
41
            }
42
        }
43
        
44
        // Brain Manager
45
        $this->config = $config;
46
        $this->insults = array(
47
            'Bachi-bouzouk',
48
            'Tonnerre de Brest',
49
            'Mille tonnerres de Brest',
50
            'Misérable ectoplasme',
51
            'Papou des Carpathes',
52
            'Que le grand cric me croque et me fasse avaler ma barbe',
53
            'Ectoplasme à roulettes',
54
            'Espèce de zouave interplanétaire',
55
            'Sale vilaine bête de tonnerre de Brest',
56
            'Espèce de porc-épic mal embouché',
57
            'Patagon de zoulou',
58
            'Loup-garou à la graisse de renoncule',
59
            'Bougres d’extrait de crétins des Alpes',
60
            'Macchabée d\'eau de vaisselle',
61
            'Astronaute d\'eau douce',
62
            'Moules à gaufres',
63
            'Ornithorynque',
64
            'Macrocéphale',
65
            'Iconoclaste',
66
            'Mille millions de mille sabords'
67
        );
68
        $this->nbOfInsults = count($this->insults);
69
        $this->ponctuations = array(
70
            '!'
71
        );
72
        $this->nbOfPonctuations = count($this->ponctuations);
73
        $this->internalPonctuations = array(
74
            '?',
75
            '&',
76
            '#',
77
            'ټ',
78
            '☠',
79
            '@'
80
        );
81
        $this->nbOfInternalPonctuations = count($this->internalPonctuations);
82
    }
83
84
    public function isPositronTriggered(TchatMessage $request)
85
    {
86
        return mt_rand(0, 1);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return mt_rand(0, 1); (integer) is incompatible with the return type of the parent method Posibrain\Positron\Positron::isPositronTriggered 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...
87
    }
88
89
    public function beautifyAnswer(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null)
90
    {
91
        if (null == $currentAnswer) {
92
            $currentAnswer = $answer;
93
        }
94
        
95
        $ponctuationSize = mt_rand(1, 3);
96
        $ponctuation = '';
97
        for ($i = 0; $i < $ponctuationSize; $i ++) {
98
            if ($i == ($ponctuationSize - 1) && $ponctuationSize >= 3) {
99
                $internalPonctuationSize = mt_rand(1, 5);
100
                for ($j = 0; $j < $internalPonctuationSize; $j ++) {
101
                    $ponctuation .= $this->internalPonctuations[mt_rand(0, $this->nbOfInternalPonctuations - 1)];
102
                }
103
            }
104
            $ponctuation .= $this->ponctuations[mt_rand(0, $this->nbOfPonctuations - 1)];
105
        }
106
        $insult = $this->insults[mt_rand(0, $this->nbOfInsults - 1)] . ' ' . $ponctuation;
107
        // Start with insulte
108
        if (mt_rand(0, 1)) {
109
            $message = $insult . ' ' . ucfirst($currentAnswer->getMessage());
110
        }
111
        // End with insult
112
        else {
113
            // Add final punctuation if not yet there
114
            if (! preg_match('![\.\!\?;]$!', $currentAnswer->getMessage())) {
115
                $currentAnswer->setMessage($currentAnswer->getMessage() . ' !');
116
            }
117
            $message = ucfirst($currentAnswer->getMessage()) . ' ' . $insult;
118
        }
119
        $currentAnswer->setMessage($message);
120
        return $currentAnswer;
121
    }
122
123
    public function getConfig()
124
    {
125
        return $this->config;
126
    }
127
128
    public function setConfig($config)
129
    {
130
        $this->config = $config;
131
    }
132
}
133