Conditions | 14 |
Paths | 1201 |
Total Lines | 50 |
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 |
||
43 | public function __construct( |
||
44 | string $topic, |
||
45 | string $channel, |
||
46 | Endpoint $endpoint, |
||
47 | Promised $subscribed, |
||
48 | int $producing = null, |
||
49 | Consuming $consuming = null |
||
50 | ) { |
||
51 | $host = $endpoint->address()->host(); |
||
52 | $port = $endpoint->address()->port(); |
||
53 | |||
54 | if ($port <= 0) { |
||
55 | $dsn = new DSN($host); |
||
56 | $host = $dsn->host(); |
||
57 | $port = $dsn->port(); |
||
58 | switch ($dsn->scheme()) { |
||
59 | case 'lookupd': |
||
60 | $lookupd = new Lookupd($host, $port); |
||
61 | break; |
||
62 | case 'nsqd': |
||
63 | $nsqd = $endpoint; |
||
64 | break; |
||
65 | default: |
||
66 | throw new ClusterEndpointInitException('Not supported scheme'); |
||
67 | } |
||
68 | } else { |
||
69 | $lookupd = new Lookupd($host, $port); |
||
70 | } |
||
71 | |||
72 | $producing === null || $this->producer = new Producer($producing); |
||
73 | $consuming === null || $this->consumer = new Consumer($consuming); |
||
74 | |||
75 | if (isset($lookupd)) { |
||
76 | $this->producer && $this->producer->setLookupd($lookupd); |
||
77 | $this->consumer && $this->consumer->setLookupd($lookupd); |
||
78 | } |
||
79 | |||
80 | if (isset($nsqd)) { |
||
81 | $this->producer && $this->producer->addEndpoint($nsqd); |
||
82 | $this->consumer && $this->consumer->addEndpoint($nsqd); |
||
83 | } |
||
84 | |||
85 | if ($this->producer) { |
||
86 | $this->producer->setTopic($topic)->startup(); |
||
87 | } |
||
88 | |||
89 | if ($this->consumer) { |
||
90 | $this->consumer->setTopic($topic)->setChannel($channel); |
||
91 | $subscribed->then(function () { |
||
92 | $this->consumer->startup(); |
||
93 | }); |
||
124 |