1 | <?php |
||
19 | class PublishToExchangeListener extends AbstractListener |
||
20 | { |
||
21 | /** |
||
22 | * @var AMQPStreamConnection |
||
23 | */ |
||
24 | protected $amqp; |
||
25 | |||
26 | /** |
||
27 | * @var LoggerInterface |
||
28 | */ |
||
29 | private $logger; |
||
30 | |||
31 | /** |
||
32 | * @var AMQPChannel |
||
33 | */ |
||
34 | protected $channel; |
||
35 | |||
36 | /** |
||
37 | * @var string the exchange name for the published messages |
||
38 | */ |
||
39 | public $exchange; |
||
40 | |||
41 | /** |
||
42 | * @var string the exchange type. Defaults to 'direct' |
||
43 | */ |
||
44 | public $exchangeType = 'direct'; |
||
45 | |||
46 | public function __construct(AMQPStreamConnection $amqp, LoggerInterface $logger) |
||
51 | |||
52 | /** |
||
53 | * Handle an event. |
||
54 | * @param EventInterface $event |
||
55 | * @return void |
||
56 | */ |
||
57 | public function handle(EventInterface $event): void |
||
70 | |||
71 | protected function getChannel(): AMQPChannel |
||
80 | |||
81 | private function createMessage($event): AMQPMessage |
||
92 | |||
93 | /** |
||
94 | * Builds routing key for $event. Default logic: |
||
95 | * |
||
96 | * For events named with `Was` keyword (e.g. `ObjectWasChanged`) routing key will be `object.was.changed`. |
||
97 | * For other events [[InvalidConfigException]] will be thrown. |
||
98 | * |
||
99 | * You can override this method to implement own routing key generation logic. |
||
100 | * |
||
101 | * @param EventInterface $event |
||
102 | * @return string |
||
103 | * @throws InvalidConfigException when `Was` keyword |
||
104 | */ |
||
105 | public function buildRoutingKey(EventInterface $event) |
||
119 | } |
||
120 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.