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