Conditions | 8 |
Paths | 10 |
Total Lines | 81 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
93 | protected function processTopic(int $recursion = 0): string |
||
94 | { |
||
95 | $topic = synapse()->memory->shortTerm()->get('topic') ?? 'random'; |
||
96 | |||
97 | // synapse()->rivescript->say("TOPIC {$topic}..."); |
||
98 | $triggers = synapse()->brain->topic($topic)->triggers(); |
||
99 | |||
100 | if ($recursion == 200) { |
||
101 | synapse()->rivescript->warn("Top many recursive calls to :func", ["func" => __CLASS__ . "::" . __FUNCTION__]); |
||
102 | exit; |
||
103 | } |
||
104 | |||
105 | foreach ($triggers as $trigger => $data) { |
||
106 | $valid = $this->isValidTrigger($trigger); |
||
107 | |||
108 | if ($valid === true) { |
||
109 | synapse()->rivescript->debug("Found trigger \":trigger\".", [ |
||
110 | 'trigger' => $trigger, |
||
111 | ]); |
||
112 | |||
113 | synapse()->memory->shortTerm()->put('trigger', $trigger); |
||
114 | $response = $this->getValidResponse($trigger); |
||
115 | |||
116 | if ($response) { |
||
117 | |||
118 | |||
119 | if ($response->isTopicChanged() === true) { |
||
120 | synapse()->rivescript->debug( |
||
121 | "Topic changed from \":from\" to \":to\"", |
||
122 | [ |
||
123 | "from" => $topic, |
||
124 | "to" => synapse()->memory->shortTerm()->get('topic') ?? 'random' |
||
125 | ] |
||
126 | ); |
||
127 | |||
128 | synapse()->rivescript->debug( |
||
129 | "Parsed trigger :trigger", |
||
130 | [ |
||
131 | "trigger" => $response->getValue(), |
||
132 | ] |
||
133 | ); |
||
134 | |||
135 | // synapse()->memory->shortTerm()->put('trigger', $response->getValue()); |
||
136 | |||
137 | /** |
||
138 | * The topic has changed to something else during $this->getValidResponse($trigger). |
||
139 | * Call $this->>processTopic again to process the new topic. |
||
140 | * |
||
141 | * @note in this case the input can stay the same. |
||
142 | */ |
||
143 | // return $this->processTopic(++$recursion); |
||
144 | } |
||
145 | |||
146 | if ($response->isRedirect() === true) { |
||
147 | synapse()->rivescript->debug("Found redirect to \":redirect\" current topic is \":topic\".", [ |
||
148 | "redirect" => $response->getRedirect(), |
||
149 | "topic" => $response->getTopic() |
||
150 | ]); |
||
151 | |||
152 | synapse()->input = new Input($response->getRedirect(), synapse()->rivescript->getClientId()); |
||
153 | synapse()->memory->shortTerm()->put('trigger', $response->getRedirect()); |
||
154 | |||
155 | return $this->processTopic(++$recursion); |
||
156 | } |
||
157 | |||
158 | |||
159 | $output = $response->getValue(); |
||
160 | |||
161 | if ($output) { |
||
162 | $this->output = $output; |
||
163 | break; |
||
164 | } |
||
165 | } else { |
||
166 | synapse()->rivescript->warn("Could not find a valid response for trigger \":trigger\"", [ |
||
167 | "trigger" => $trigger |
||
168 | ]); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return trim($this->output); |
||
174 | } |
||
217 |