Positroner::loadPositrons()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 12
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
1
<?php
2
namespace Posibrain;
3
4
use Seld\JsonLint\JsonParser;
5
use Seld\JsonLint\ParsingException;
6
use Monolog\Logger;
7
include_once (__DIR__ . '/../tools.php');
8
9
/**
10
 *
11
 * @author Fylhan (http://fylhan.la-bnbox.fr)
12
 * @license LGPL-2.1+
13
 */
14
class Positroner implements IPositroner
15
{
16
17
    private static $logger = NULL;
18
19
    public $config;
20
21
    public $params;
22
23
    public $positrons;
24
25
    public $selectedPositrons;
26
27
    public function __construct($config = array(), $params = array())
28
    {
29
        // Logger
30 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...
31
            self::$logger = new Logger(__CLASS__);
32
            if (! empty($params) && isset($params['loggerHandler'])) {
33
                self::$logger->pushHandler($params['loggerHandler']);
34
            }
35
        }
36
        
37
        // Init seed for random values
38
        mt_srand((double) microtime() * 1000000);
39
        $this->params = $params;
40
        $this->config = $config;
41
    }
42
43
    public function listPositrons($ids = null)
44
    {
45
        $files = glob(dirname(__FILE__) . '/Positron/*/*Positron.php');
46
        if (empty($files))
47
            $files = array();
48
        
49
        $positrons = array();
50
        foreach ($files as $file) {
51
            $path = preg_replace('!^.*(Posibrain/Positron/.*/.*Positron).*$!ui', '$1', $file);
52
            $name = preg_replace('!^.*Posibrain/Positron/(.*)/.*Positron.*$!ui', '$1', $file);
53
            if (empty($ids) || $ids === $name  || (is_array($ids) && in_array($name, $ids)))  
54
            {
55
                $positrons[$name] = $path;
56
            }
57
        }
58
        return $positrons;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $positrons; (array) is incompatible with the return type declared by the interface Posibrain\IPositroner::listPositrons of type Posibrain\List.

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...
59
    }
60
61
    public function loadPositrons($ids, $config, $params = array())
62
    {
63
        $positrons = $this->listPositrons($ids);
64
        foreach ($positrons as $positronName => $positronPath) {
65
            require_once (dirname(__FILE__) . '/../' . $positronPath . '.php');
66
            $className = '\\' . str_replace('/', '\\', $positronPath);
67
            $positron = new $className($config, $params);
68
            $this->positrons[$positronName] = $positron;
69
        }
70
        $this->selectedPositrons = array();
71
        return $this->positrons;
72
    }
73
74
    public function getPostitrons()
75
    {
76
        if (empty($this->positrons))
77
            $this->loadPositrons($this->config, $this->params);
0 ignored issues
show
Documentation introduced by
$this->config is of type array, but the function expects a object<Posibrain\List>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->params is of type array, but the function expects a object<Posibrain\Config>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        return $this->positrons;
79
    }
80
81
    public function findPositron($id)
82
    {
83
        if (! isset($this->positrons[$id])) {
84
            return null;
85
        }
86
        return $this->positrons[$id];
87
    }
88
89
    public function updatePositron($id, $state)
90
    {}
91
92
    public function isBotTriggered(TchatMessage $request, $currentValue = true)
93
    {
94
        if (empty($this->positrons)) {
95
            return $currentValue;
96
        }
97
        // Positrons trigerred?
98
        $this->selectedPositrons = array();
99
        foreach ($this->positrons as $positron) {
100
            if ($positron->isPositronTriggered($request)) {
101
                $this->selectedPositrons[] = $positron;
102
            }
103
        }
104
        // Bot trigerred?
105
        foreach ($this->selectedPositrons as $positron) {
106
            $currentValue = $positron->isBotTriggered($request, $currentValue);
107
        }
108
        return $currentValue;
109
    }
110
111
    public function analyseRequest(TchatMessage $request, AnalysedRequest $currentAnalysedRequest = null)
112
    {
113
        if (empty($this->selectedPositrons)) {
114
            return new AnalysedRequest($request->getMessage(), $request->getName(), $request->getDate(), $request);
115
        }
116
        foreach ($this->selectedPositrons as $positron) {
117
            $currentAnalysedRequest = $positron->analyseRequest($request, $currentAnalysedRequest);
118
        }
119
        if (null == $currentAnalysedRequest) {
120
            $currentAnalysedRequest = new AnalysedRequest($request->getMessage(), $request->getName(), $request->getDate(), $request);
121
        }
122
        return $currentAnalysedRequest;
123
    }
124
125
    public function loadMemory(AnalysedRequest $request, $currentMemory = null)
126
    {
127
        if (empty($this->selectedPositrons)) {
128
            return $currentMemory;
129
        }
130
        foreach ($this->selectedPositrons as $positron) {
131
            $currentMemory = $positron->loadMemory($request, $currentMemory);
132
        }
133
        return $currentMemory;
134
    }
135
136 View Code Duplication
    public function generateSymbolicAnswer(AnalysedRequest $request, $memory = null, TchatMessage $currentAnswer = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
137
    {
138
        if (empty($this->selectedPositrons)) {
139
            return new TchatMessage('Goxjjdp !?');
140
        }
141
        foreach ($this->selectedPositrons as $positron) {
142
            $currentAnswser = $positron->generateSymbolicAnswer($request, $memory, $currentAnswer);
143
        }
144
        if (null == $currentAnswser) {
0 ignored issues
show
Bug introduced by
The variable $currentAnswser does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
145
            $currentAnswser = new TchatMessage('HJJoc ?');
146
        }
147
        return $currentAnswser;
148
    }
149
150 View Code Duplication
    public function provideMeaning(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
151
    {
152
        if (empty($this->selectedPositrons)) {
153
            return new TchatMessage('Ndfdslp !?');
154
        }
155
        foreach ($this->selectedPositrons as $positron) {
156
            $currentAnswser = $positron->provideMeaning($request, $memory, $answer, $currentAnswer);
157
        }
158
        if (null == $currentAnswser) {
0 ignored issues
show
Bug introduced by
The variable $currentAnswser does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
159
            $currentAnswser = new TchatMessage('Ndfdslp ?');
160
        }
161
        return $currentAnswser;
162
    }
163
164
    public function beautifyAnswer(AnalysedRequest $request, $memory, TchatMessage $answer, TchatMessage $currentAnswer = null)
165
    {
166
        if (empty($this->selectedPositrons)) {
167
            return $currentAnswer;
168
        }
169
        foreach ($this->selectedPositrons as $positron) {
170
            $currentAnswser = $positron->beautifyAnswer($request, $memory, $answer, $currentAnswer);
171
        }
172
        return $currentAnswser;
0 ignored issues
show
Bug introduced by
The variable $currentAnswser does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
173
    }
174
}