Conditions | 8 |
Paths | 2 |
Total Lines | 57 |
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 |
||
83 | public function predis(SymfonyStyle $io) |
||
84 | { |
||
85 | $pubSubLoop = function () use ($io) { |
||
86 | /** @var \Predis\Client $client */ |
||
87 | $client = $this->redisDriver->getClient(true); |
||
88 | |||
89 | // Initialize a new pubsub consumer. |
||
90 | $pubSub = $client->pubSubLoop(); |
||
91 | |||
92 | $channels = []; |
||
93 | foreach ($this->broadcast->channels() as $key => $channel) { |
||
94 | $channels[$key] = $channel . '.io'; |
||
95 | } |
||
96 | |||
97 | // Subscribe to your channels |
||
98 | $pubSub->subscribe(array_merge(['control_channel'], $channels)); |
||
99 | |||
100 | // Start processing the pubsup messages. Open a terminal and use redis-cli |
||
101 | // to push messages to the channels. Examples: |
||
102 | // ./redis-cli PUBLISH notifications "this is a test" |
||
103 | // ./redis-cli PUBLISH control_channel quit_loop |
||
104 | foreach ($pubSub as $message) { |
||
105 | switch ($message->kind) { |
||
106 | case 'subscribe': |
||
107 | $io->success("Subscribed to {$message->channel}"); |
||
108 | break; |
||
109 | case 'message': |
||
110 | if ('control_channel' == $message->channel) { |
||
111 | if ('quit_loop' == $message->payload) { |
||
112 | $io->success("Aborting pubsub loop...\n"); |
||
113 | $pubSub->unsubscribe(); |
||
114 | } else { |
||
115 | $io->success("Received an unrecognized command: {$message->payload}\n"); |
||
116 | } |
||
117 | } else { |
||
118 | $payload = json_decode($message->payload, true); |
||
119 | $data = $payload['data'] ?? []; |
||
120 | |||
121 | $this->broadcast->on($payload['name'], $data); |
||
122 | } |
||
123 | break; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | // Always unset the pubsub consumer instance when you are done! The |
||
128 | // class destructor will take care of cleanups and prevent protocol |
||
129 | // desynchronizations between the client and the server. |
||
130 | unset($pubSub); |
||
131 | }; |
||
132 | |||
133 | // Auto recconnect on redis timeout |
||
134 | try { |
||
135 | $pubSubLoop(); |
||
136 | } catch (ConnectionException $e) { |
||
137 | $pubSubLoop(); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: