Pusher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 23.81%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 11
loc 76
ccs 5
cts 21
cp 0.2381
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A close() 0 8 2
A doPush() 0 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Pusher.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:WebSocketsZMQ!
9
 * @subpackage     Pusher
10
 * @since          1.0.0
11
 *
12
 * @date           01.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsZMQ\Pusher;
18
19
use ZMQ;
20
use ZMQContext;
21
use ZMQSocket;
22
use ZMQSocketException;
23
use RuntimeException;
24
25
use Psr\Log;
26
27
use IPub;
28
use IPub\WebSocketsZMQ;
29
30
use IPub\WebSockets\Router;
31
32
use IPub\WebSocketsWAMP\PushMessages;
33
use IPub\WebSocketsWAMP\Serializers;
34
35
/**
36
 * ZeroMQ message pusher
37 1
 *
38
 * @package        iPublikuj:WebSocketsZMQ!
39
 * @subpackage     Pushers
40
 *
41
 * @author         Adam Kadlec <[email protected]>
42
 */
43
final class Pusher extends PushMessages\Pusher
44
{
45
	/**
46
	 * @var WebSocketsZMQ\Configuration
47
	 */
48
	private $configuration;
49
50
	/**
51
	 * @var Log\LoggerInterface
52
	 */
53
	private $logger;
54
55
	/**
56
	 * @var ZMQSocket
57
	 */
58
	private $socket;
59
60
	/**
61
	 * @param WebSocketsZMQ\Configuration $configuration
62
	 * @param Router\LinkGenerator $linkGenerator
63
	 * @param Serializers\PushMessageSerializer $serializer
64
	 * @param Log\LoggerInterface|NULL $logger
65
	 */
66 1 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
67
		WebSocketsZMQ\Configuration $configuration,
68 1
		Router\LinkGenerator $linkGenerator,
69 1
		Serializers\PushMessageSerializer $serializer,
70 1
		Log\LoggerInterface $logger = NULL
71
	) {
72
		parent::__construct('zmq', $serializer, $linkGenerator);
73
74
		$this->configuration = $configuration;
75
		$this->logger = $logger === NULL ? new Log\NullLogger : $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger === NULL ? new \...\NullLogger() : $logger can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
	}
77
78
	/**
79
	 * {@inheritdoc}
80
	 *
81
	 * @throws ZMQSocketException
82
	 */
83
	public function close() : void
84
	{
85
		if ($this->isConnected() === FALSE) {
86
			return;
87
		}
88
89
		$this->socket->disconnect($this->configuration->getHost() . ':' . $this->configuration->getPort());
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 *
95
	 * @throws ZMQSocketException
96
	 */
97
	protected function doPush(string $data, array $context = []) : void
98
	{
99
		if ($this->isConnected() === FALSE) {
100
			if (!extension_loaded('zmq')) {
101
				throw new RuntimeException(sprintf(
102
					'%s pusher require %s php extension',
103
					get_class($this),
104
					$this->getName()
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
105
				));
106
			}
107
108
			$context = new ZMQContext(1, $this->configuration->isPersistent());
109 1
110
			$this->socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH);
111
			$this->socket->connect($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort());
112
113
			$this->setConnected();
114
		}
115
116
		$this->socket->send($data);
117
	}
118
}
119