| Conditions | 8 |
| Paths | 2 |
| Total Lines | 74 |
| 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 |
||
| 75 | public function predis() |
||
| 76 | {
|
||
| 77 | $pubSubLoop = function () {
|
||
| 78 | $client = Broadcast::getDriver()->getConnection(true); |
||
| 79 | |||
| 80 | // Initialize a new pubsub consumer. |
||
| 81 | $pubsub = $client->pubSubLoop(); |
||
| 82 | |||
| 83 | $channels = []; |
||
| 84 | foreach (Broadcast::channels() as $key => $channel) {
|
||
| 85 | $channels[$key] = $channel . '.io'; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Subscribe to your channels |
||
| 89 | $pubsub->subscribe(ArrayHelper::merge(['control_channel'], $channels)); |
||
| 90 | |||
| 91 | // Start processing the pubsup messages. Open a terminal and use redis-cli |
||
| 92 | // to push messages to the channels. Examples: |
||
| 93 | // ./redis-cli PUBLISH notifications "this is a test" |
||
| 94 | // ./redis-cli PUBLISH control_channel quit_loop |
||
| 95 | /** @var object $message */ |
||
| 96 | foreach ($pubsub as $message) {
|
||
| 97 | switch ($message->kind) {
|
||
| 98 | case 'subscribe': |
||
| 99 | $this->output("Subscribed to {$message->channel}\n");
|
||
| 100 | break; |
||
| 101 | case 'message': |
||
| 102 | if ('control_channel' == $message->channel) {
|
||
| 103 | if ('quit_loop' == $message->payload) {
|
||
| 104 | $this->output("Aborting pubsub loop...\n", Console::FG_RED);
|
||
| 105 | $pubsub->unsubscribe(); |
||
| 106 | } else {
|
||
| 107 | $this->output("Received an unrecognized command: {$message->payload}\n", Console::FG_RED);
|
||
| 108 | } |
||
| 109 | } else {
|
||
| 110 | $payload = Json::decode($message->payload); |
||
| 111 | $data = $payload['data'] ?? []; |
||
| 112 | $id = $payload['id'] ?? ''; |
||
| 113 | |||
| 114 | // $pid = pcntl_fork(); |
||
| 115 | // if ($pid == -1) {
|
||
| 116 | // exit('Error while forking process.');
|
||
| 117 | // } elseif ($pid) {
|
||
| 118 | // //parent. Wait for the child and continues |
||
| 119 | // pcntl_wait($status); |
||
| 120 | // $exitStatus = pcntl_wexitstatus($status); |
||
| 121 | // if ($exitStatus !== 0) {
|
||
| 122 | // //put job back to queue or other stuff |
||
| 123 | // } |
||
| 124 | // }else {
|
||
| 125 | Broadcast::on($payload['name'], $data, $id); |
||
| 126 | // Yii::$app->end(); |
||
| 127 | // } |
||
| 128 | // Received the following message from {$message->channel}:") {$message->payload}";
|
||
| 129 | } |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // Always unset the pubsub consumer instance when you are done! The |
||
| 135 | // class destructor will take care of cleanups and prevent protocol |
||
| 136 | // desynchronizations between the client and the server. |
||
| 137 | unset($pubsub); |
||
| 138 | }; |
||
| 139 | |||
| 140 | // Auto recconnect on redis timeout |
||
| 141 | try {
|
||
| 142 | $pubSubLoop(); |
||
| 143 | } catch (\Predis\Connection\ConnectionException $e) {
|
||
| 144 | $pubSubLoop(); |
||
| 145 | } |
||
| 146 | |||
| 147 | return true; |
||
| 148 | } |
||
| 149 | } |
||
| 150 |
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: