| Conditions | 14 | 
| Paths | 99 | 
| Total Lines | 113 | 
| Code Lines | 51 | 
| 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  | 
            ||
| 70 | protected function processTopic(int $recursion = 0): string  | 
            ||
| 71 |     { | 
            ||
| 72 |         $topic = synapse()->memory->shortTerm()->get('topic') ?? 'random'; | 
            ||
| 73 |         $begin = synapse()->brain->topic("__begin__"); | 
            ||
| 74 | |||
| 75 | $triggers = synapse()->brain->topic($topic)->triggers();  | 
            ||
| 76 | |||
| 77 |         if ($recursion === synapse()->memory->global()->get('depth')) { | 
            ||
| 78 |             synapse()->rivescript->warn("Top many recursive calls to :func", ["func" => __CLASS__ . "::" . __FUNCTION__]); | 
            ||
| 79 | return "ERR: Deep Recursion Detected";  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * 1. Check if topic is valid  | 
            ||
| 84 | * 2. Process the triggers  | 
            ||
| 85 | * 3. if valid trigger  | 
            ||
| 86 | * - Check if response is redirect  | 
            ||
| 87 | * -  | 
            ||
| 88 | */  | 
            ||
| 89 |         if ($begin) { | 
            ||
| 90 |             synapse()->rivescript->say("Begin label found. Starting processing."); | 
            ||
| 91 | |||
| 92 | //            $request = $begin->triggers()->get("request"); | 
            ||
| 93 | $request = null;  | 
            ||
| 94 | |||
| 95 | // FIXME ugly code award make this global.  | 
            ||
| 96 |             foreach ($begin->triggers() as $trigger) { | 
            ||
| 97 |                 if ($trigger->getText() === "request") { | 
            ||
| 98 | $request = $trigger;  | 
            ||
| 99 | break;  | 
            ||
| 100 | }  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 |             if ($request) { | 
            ||
| 104 | $response = $request->getQueue()->process();  | 
            ||
| 105 | $output = $response->getValue();  | 
            ||
| 106 | |||
| 107 |                 if ($output !== '') { | 
            ||
| 108 | $this->output .= $output;  | 
            ||
| 109 | }  | 
            ||
| 110 | |||
| 111 |                 if ($begin->isOk() === false) { | 
            ||
| 112 | return $this->output;  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 |         foreach ($triggers as $index => $trigger) { | 
            ||
| 118 | $valid = $this->isValidTrigger($trigger);  | 
            ||
| 119 | |||
| 120 |             if ($valid === true) { | 
            ||
| 121 |                 synapse()->rivescript->debug("Found trigger \":trigger\".", [ | 
            ||
| 122 | 'trigger' => $trigger->getText(),  | 
            ||
| 123 | ]);  | 
            ||
| 124 | |||
| 125 |                 synapse()->memory->shortTerm()->put('trigger', $trigger); | 
            ||
| 126 | $response = $this->getValidResponseForTriggerAtIndex($index);  | 
            ||
| 127 | |||
| 128 |                 if ($response) { | 
            ||
| 129 |                     if ($response->isTopicChanged() === true) { | 
            ||
| 130 | synapse()->rivescript->debug(  | 
            ||
| 131 | "Topic changed from \":from\" to \":to\"",  | 
            ||
| 132 | [  | 
            ||
| 133 | "from" => $topic,  | 
            ||
| 134 |                                 "to" => synapse()->memory->shortTerm()->get('topic') ?? 'random' | 
            ||
| 135 | ]  | 
            ||
| 136 | );  | 
            ||
| 137 | |||
| 138 | synapse()->rivescript->debug(  | 
            ||
| 139 | "Parsed trigger :trigger",  | 
            ||
| 140 | [  | 
            ||
| 141 | "trigger" => $response->getValue(),  | 
            ||
| 142 | ]  | 
            ||
| 143 | );  | 
            ||
| 144 | |||
| 145 |                         //     synapse()->memory->shortTerm()->put('trigger', $response->getValue()); | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * The topic has changed to something else during $this->getValidResponse($trigger).  | 
            ||
| 149 | * Call $this->>processTopic again to process the new topic.  | 
            ||
| 150 | *  | 
            ||
| 151 | * @note in this case the input can stay the same.  | 
            ||
| 152 | */  | 
            ||
| 153 | // return $this->processTopic(++$recursion);  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 |                     if ($response->isRedirect() === true) { | 
            ||
| 157 |                         synapse()->rivescript->debug("Found redirect to \":redirect\" current topic is \":topic\".", [ | 
            ||
| 158 | "redirect" => $response->getRedirect(),  | 
            ||
| 159 | "topic" => $response->getTopic()  | 
            ||
| 160 | ]);  | 
            ||
| 161 | |||
| 162 | synapse()->input = new Input($response->getRedirect(), synapse()->rivescript->getClientId());  | 
            ||
| 163 |                         synapse()->memory->shortTerm()->put('trigger', $response->getRedirect()); | 
            ||
| 164 | |||
| 165 | return $this->processTopic(++$recursion);  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | $output = $response->getValue();  | 
            ||
| 169 | |||
| 170 |                     if ($output) { | 
            ||
| 171 | $this->output .= $output;  | 
            ||
| 172 | break;  | 
            ||
| 173 | }  | 
            ||
| 174 |                 } else { | 
            ||
| 175 |                     synapse()->rivescript->warn("Could not find a valid response for trigger \":trigger\"", [ | 
            ||
| 176 | "trigger" => $trigger->getText()  | 
            ||
| 177 | ]);  | 
            ||
| 178 | }  | 
            ||
| 179 | }  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | return trim($this->output);  | 
            ||
| 183 | }  | 
            ||
| 229 |