Conditions | 15 |
Total Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Changes | 16 | ||
Bugs | 0 | Features | 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 |
||
52 | function run(\CharlotteDunois\Livia\Commands\Context $context, \ArrayObject $args, bool $fromPattern) { |
||
53 | $messages = array(); |
||
54 | $prev = null; |
||
55 | |||
56 | $code = $args['script']; |
||
57 | if(\mb_substr($code, -1) !== ';') { |
||
58 | $code .= ';'; |
||
59 | } |
||
60 | |||
61 | if(\mb_strpos($code, 'return') === false && \mb_strpos($code, 'echo') === false) { |
||
62 | $code = \explode(';', $code); |
||
63 | $code[(\count($code) - 2)] = \PHP_EOL.'return '.\trim($code[(\count($code) - 2)]); |
||
64 | $code = \implode(';', $code); |
||
65 | } |
||
66 | |||
67 | return (new \React\Promise\Promise(function (callable $resolve, callable $reject) use ($context, $args, $code, &$messages, &$prev) { |
||
68 | $time = null; |
||
69 | |||
70 | $timer = function (bool $callback = false) { |
||
71 | static $hrtime; |
||
72 | return $this->timer($hrtime, $callback); |
||
73 | }; |
||
74 | |||
75 | $doCallback = function ($result) use ($code, $context, &$messages, &$time, &$timer) { |
||
76 | $endtime = $timer(true); |
||
77 | |||
78 | $previous = \set_error_handler(array($this, 'errorCallback')); |
||
79 | $result = $this->invokeDump($result); |
||
80 | \set_error_handler($previous); |
||
81 | |||
82 | $len = \mb_strlen($result); |
||
83 | $maxlen = 1850 - \mb_strlen($code); |
||
84 | |||
85 | if($len > $maxlen) { |
||
86 | $result = \mb_substr($result, 0, $maxlen).\PHP_EOL.'...'; |
||
87 | } |
||
88 | |||
89 | $sizeformat = \count($this->timeformats) - 1; |
||
90 | $format = 0; |
||
91 | |||
92 | $exectime = $endtime - $time; |
||
93 | while(\ceil($exectime) >= 1000.0 && $format < $sizeformat) { |
||
94 | $exectime /= 1000; |
||
95 | $format++; |
||
96 | } |
||
97 | $exectime = \ceil($exectime); |
||
98 | |||
99 | $messages[] = $context->say($context->message->author.\CharlotteDunois\Yasmin\Models\Message::$replySeparator.'Executed callback after '.$exectime.$this->timeformats[$format].'.'.\PHP_EOL.\PHP_EOL.'```php'.\PHP_EOL.$result.\PHP_EOL.'```'.($len > $maxlen ? \PHP_EOL.'Original length: '.$len : '')); |
||
100 | }; |
||
101 | |||
102 | $prev = \set_error_handler(array($this, 'errorCallback')); |
||
103 | |||
104 | $endtime = null; |
||
105 | $time = $timer(); |
||
106 | |||
107 | $evalcode = 'namespace CharlotteDunois\\Livia\\Commands\\EvalNamespace\\'.\preg_replace('/[^a-z]/i', '', \bin2hex(\random_bytes(10)).\sha1(\time())).';'. |
||
108 | \PHP_EOL.$code; |
||
109 | |||
110 | $result = (function () use ($evalcode, $context, &$doCallback) { |
||
111 | $client = $this->client; |
||
112 | return eval($evalcode); |
||
113 | })(); |
||
114 | |||
115 | if(!($result instanceof \React\Promise\PromiseInterface)) { |
||
116 | $endtime = $timer(); |
||
117 | $result = \React\Promise\resolve($result); |
||
118 | } |
||
119 | |||
120 | return $result->then(function ($result) use ($code, $context, &$messages, &$prev, &$endtime, $time, &$timer) { |
||
121 | if($endtime === null) { |
||
122 | $endtime = $timer(); |
||
123 | } |
||
124 | |||
125 | $this->lastResult = $result; |
||
126 | $result = $this->invokeDump($result); |
||
127 | |||
128 | \set_error_handler($prev); |
||
129 | |||
130 | $len = \mb_strlen($result); |
||
131 | $maxlen = 1850 - \mb_strlen($code); |
||
132 | |||
133 | if($len > $maxlen) { |
||
134 | $result = \mb_substr($result, 0, $maxlen).\PHP_EOL.'...'; |
||
135 | } |
||
136 | |||
137 | $sizeformat = \count($this->timeformats) - 1; |
||
138 | $format = 0; |
||
139 | |||
140 | $exectime = $endtime - $time; |
||
141 | while(\ceil($exectime) >= 1000.0 && $format < $sizeformat) { |
||
142 | $exectime /= 1000; |
||
143 | $format++; |
||
144 | } |
||
145 | $exectime = \ceil($exectime); |
||
146 | |||
147 | $messages[] = $context->say($context->message->author.\CharlotteDunois\Yasmin\Models\Message::$replySeparator.'Executed in '.$exectime.$this->timeformats[$format].'.'.\PHP_EOL.\PHP_EOL.'```php'.\PHP_EOL.$result.\PHP_EOL.'```'.($len > $maxlen ? \PHP_EOL.'Original length: '.$len : '')); |
||
148 | return $messages; |
||
149 | })->done($resolve, $reject); |
||
150 | }))->otherwise(function ($e) use ($code, $context, &$messages, &$prev) { |
||
151 | \set_error_handler($prev); |
||
152 | |||
153 | $e = (string) $e; |
||
154 | $len = \mb_strlen($e); |
||
155 | $maxlen = 1900 - \mb_strlen($code); |
||
156 | |||
157 | if($len > $maxlen) { |
||
158 | $e = \mb_substr($e, 0, $maxlen).\PHP_EOL.'...'; |
||
159 | } |
||
160 | |||
161 | $messages[] = $context->say($context->message->author.\PHP_EOL.'```php'.\PHP_EOL.$code.\PHP_EOL.'```'.\PHP_EOL.'Error: ```'.\PHP_EOL.$e.\PHP_EOL.'```'); |
||
162 | return $messages; |
||
163 | }); |
||
229 |