Conditions | 7 |
Paths | 1 |
Total Lines | 90 |
Code Lines | 23 |
Lines | 90 |
Ratio | 100 % |
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 |
||
31 | View Code Duplication | public function __construct($container, $host, $port, $rconPassword) |
|
|
|||
32 | { |
||
33 | /*$callback = function(Packet $packet, Socket $socket) { |
||
34 | if (is_null($packet) || $packet->isEmpty()) return false; |
||
35 | |||
36 | $remaining = $packet->getLong(false); |
||
37 | $packet->setPos(4); |
||
38 | $id = $packet->getLong(false); |
||
39 | |||
40 | if ($remaining > 0) { |
||
41 | $splittedPackets = new PacketCollection(); |
||
42 | $respId = null; |
||
43 | |||
44 | do { |
||
45 | if (!$respId) { |
||
46 | $respId = $id; |
||
47 | } |
||
48 | elseif ($respId != $id) { |
||
49 | $packet = $socket->recv(false); |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | $splittedPackets->add($packet->rewind()); |
||
54 | $packet = $socket->recv(false, $remaining); |
||
55 | $remaining -= $packet->getLength(); |
||
56 | } while ($remaining > 0); |
||
57 | |||
58 | return $splittedPackets->reassemble( |
||
59 | function(Packet $bigPacket, Packet $packet) { |
||
60 | if ($bigPacket->isEmpty()) { |
||
61 | $bigPacket->addContent($packet->getContent()); |
||
62 | $bigPacket->setPos($packet->getLength()-2); |
||
63 | } |
||
64 | else { |
||
65 | $bigPacket->addContent($packet); |
||
66 | } |
||
67 | |||
68 | return $bigPacket; |
||
69 | } |
||
70 | )->rewind(); |
||
71 | } |
||
72 | else { |
||
73 | return $packet; |
||
74 | } |
||
75 | };*/ |
||
76 | $callbacks = array(); |
||
77 | // Permet de déterminé s'il s'agit d'une réponse multi-paquet |
||
78 | $callbacks[Socket::MULTI_DETECTOR] = function(Packet $packet) { |
||
79 | if (is_null($packet) || $packet->isEmpty()) return false; |
||
80 | |||
81 | // Récupération de la longueur de la réponse |
||
82 | $len = $packet->getLong(false); |
||
83 | // Il faut rajouter 4 puisque la taille récupéré correspondant |
||
84 | // A la taille du paquet sans l'entier contenant la taille |
||
85 | $remaining = $len - $packet->getLength() + 4; |
||
86 | |||
87 | if ($remaining > 0) { |
||
88 | return true; |
||
89 | } |
||
90 | else { |
||
91 | return false; |
||
92 | } |
||
93 | }; |
||
94 | // Permet de récupérer les différents paquets qui composent une réponse |
||
95 | $callbacks[Socket::MULTI_RECEIVER] = function(Packet $packet, Socket $socket) { |
||
96 | if (is_null($packet) || $packet->isEmpty()) return false; |
||
97 | |||
98 | // Récupération de la longueur de la réponse |
||
99 | $len = $packet->getLong(false); |
||
100 | // Il faut rajouter 4 puisque la taille récupéré correspondant |
||
101 | // A la taille du paquet sans l'entier contenant la taille |
||
102 | $remaining = $len - $packet->getLength() + 4; |
||
103 | |||
104 | while ($remaining > 0) { |
||
105 | // Récupération du prochain paquet et calcul de la taille restante à récupérer |
||
106 | $newPacket = $socket->recv(false, $remaining); |
||
107 | $remaining = $remaining - $newPacket->getLength(); |
||
108 | |||
109 | // Ajout du paquet au paquet originel |
||
110 | $packet->setPos($packet->getLength() - 2); |
||
111 | $packet->addContent($newPacket->getContent()); |
||
112 | } |
||
113 | |||
114 | return $packet; |
||
115 | }; |
||
116 | |||
117 | $this->rconPassword = $rconPassword; |
||
118 | $this->socket = $container->get('socket')->getTCPSocket($host, $port, $callbacks); |
||
119 | $this->packetFactory = $container->get('packet.factory.rcon.source'); |
||
120 | } |
||
121 | |||
297 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.