Skip to content

Commit 2960eb4

Browse files
committed
1 parent 1233d12 commit 2960eb4

File tree

85 files changed

+451
-448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+451
-448
lines changed

composer.lock

Lines changed: 157 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Composer/Address.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer;
56

6-
77
interface Address
88
{
9-
const TYPE_BIT = 'bit';
10-
const TYPE_BYTE = 'byte';
11-
const TYPE_INT16 = 'int16';
12-
const TYPE_UINT16 = 'uint16';
13-
const TYPE_INT32 = 'int32';
14-
const TYPE_UINT32 = 'uint32';
15-
const TYPE_INT64 = 'int64';
16-
const TYPE_UINT64 = 'uint64';
17-
const TYPE_FLOAT = 'float';
18-
const TYPE_DOUBLE = 'double';
19-
const TYPE_STRING = 'string';
9+
public const TYPE_BIT = 'bit';
10+
public const TYPE_BYTE = 'byte';
11+
public const TYPE_INT16 = 'int16';
12+
public const TYPE_UINT16 = 'uint16';
13+
public const TYPE_INT32 = 'int32';
14+
public const TYPE_UINT32 = 'uint32';
15+
public const TYPE_INT64 = 'int64';
16+
public const TYPE_UINT64 = 'uint64';
17+
public const TYPE_FLOAT = 'float';
18+
public const TYPE_DOUBLE = 'double';
19+
public const TYPE_STRING = 'string';
2020

21-
const TYPES = [
21+
public const TYPES = [
2222
Address::TYPE_BIT,
2323
Address::TYPE_BYTE,
2424
Address::TYPE_INT16,

src/Composer/AddressSplitter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer;
@@ -13,10 +14,10 @@ abstract class AddressSplitter
1314
*/
1415
private array $currentUnaddressableRanges = [];
1516

16-
const UNIT_ID_PREFIX = '||unitId=';
17+
public const UNIT_ID_PREFIX = '||unitId=';
1718

18-
const MAX_REGISTERS_PER_MODBUS_REQUEST = 124;
19-
const MAX_COILS_PER_MODBUS_REQUEST = 2048; // response has 1 byte field for count - so 256 * 8 is max
19+
public const MAX_REGISTERS_PER_MODBUS_REQUEST = 124;
20+
public const MAX_COILS_PER_MODBUS_REQUEST = 2048; // response has 1 byte field for count - so 256 * 8 is max
2021

2122
protected function getMaxAddressesPerModbusRequest(): int
2223
{
@@ -115,7 +116,7 @@ public function splitWithUnaddressableRanges(array $addresses, array $unaddressa
115116
// as those addresses overlap
116117
if ($maxAvailableRegister === null || $nextAvailableRegister > $maxAvailableRegister) {
117118
$maxAvailableRegister = $nextAvailableRegister;
118-
} else if ($nextAvailableRegister < $maxAvailableRegister) {
119+
} elseif ($nextAvailableRegister < $maxAvailableRegister) {
119120
$nextAvailableRegister = $maxAvailableRegister;
120121
}
121122
$previousQuantity = $quantity;
@@ -139,7 +140,7 @@ public function splitWithUnaddressableRanges(array $addresses, array $unaddressa
139140
return $result;
140141
}
141142

142-
protected function shouldSplit(Address $currentAddress, int $currentQuantity, Address $previousAddress = null, int $previousQuantity = null): bool
143+
protected function shouldSplit(Address $currentAddress, int $currentQuantity, ?Address $previousAddress = null, ?int $previousQuantity = null): bool
143144
{
144145
if ($currentQuantity >= $this->getMaxAddressesPerModbusRequest()) {
145146
return true;

src/Composer/Range.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function fromIntArray(array $range): Range
2929
$count = count($range);
3030
if ($count == 1) {
3131
return new Range($range[0], $range[0]);
32-
} else if ($count == 2) {
32+
} elseif ($count == 2) {
3333
return new Range($range[0], $range[1]);
3434
}
3535
throw new InvalidArgumentException('Range can only be created from array with 1 or 2 elements');

src/Composer/Read/Coil/ReadCoilAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer\Read\Coil;
56

6-
77
use ModbusTcpClient\Composer\Address;
88
use ModbusTcpClient\Packet\ModbusResponse;
99

@@ -21,7 +21,7 @@ class ReadCoilAddress implements Address
2121
/** @var callable */
2222
private $errorCallback;
2323

24-
public function __construct(int $address, string $name = null, callable $callback = null, callable $errorCallback = null)
24+
public function __construct(int $address, ?string $name = null, ?callable $callback = null, ?callable $errorCallback = null)
2525
{
2626
$this->address = $address;
2727
$this->name = $name ?: (string)($address);

src/Composer/Read/Coil/ReadCoilAddressSplitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer\Read\Coil;
56

6-
77
use ModbusTcpClient\Composer\AddressSplitter;
88

99
class ReadCoilAddressSplitter extends AddressSplitter

src/Composer/Read/Coil/ReadCoilRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer\Read\Coil;
56

6-
77
use ModbusTcpClient\Composer\Request;
88
use ModbusTcpClient\Exception\ModbusException;
99
use ModbusTcpClient\Packet\ErrorResponse;

src/Composer/Read/ReadCoilsBuilder.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer\Read;
@@ -34,7 +35,7 @@ class ReadCoilsBuilder
3435
/** @var int */
3536
private int $unitId;
3637

37-
public function __construct(string $requestClass, string $uri = null, int $unitId = 0)
38+
public function __construct(string $requestClass, ?string $uri = null, int $unitId = 0)
3839
{
3940
$this->addressSplitter = new ReadCoilAddressSplitter($requestClass);
4041

@@ -44,12 +45,12 @@ public function __construct(string $requestClass, string $uri = null, int $unitI
4445
$this->unitId = $unitId;
4546
}
4647

47-
public static function newReadCoils(string $uri = null, int $unitId = 0): ReadCoilsBuilder
48+
public static function newReadCoils(?string $uri = null, int $unitId = 0): ReadCoilsBuilder
4849
{
4950
return new ReadCoilsBuilder(ReadCoilsRequest::class, $uri, $unitId);
5051
}
5152

52-
public static function newReadInputDiscretes(string $uri = null, int $unitId = 0): ReadCoilsBuilder
53+
public static function newReadInputDiscretes(?string $uri = null, int $unitId = 0): ReadCoilsBuilder
5354
{
5455
return new ReadCoilsBuilder(ReadInputDiscretesRequest::class, $uri, $unitId);
5556
}
@@ -157,11 +158,10 @@ public function fromArray(array $coil): ReadCoilsBuilder
157158

158159
public function coil(
159160
int $address,
160-
string $name = null,
161-
callable $callback = null,
162-
callable $errorCallback = null
163-
): ReadCoilsBuilder
164-
{
161+
?string $name = null,
162+
?callable $callback = null,
163+
?callable $errorCallback = null
164+
): ReadCoilsBuilder {
165165
return $this->addAddress(new ReadCoilAddress($address, $name, $callback, $errorCallback));
166166
}
167167

@@ -178,8 +178,3 @@ public function isNotEmpty(): bool
178178
return !empty($this->addresses);
179179
}
180180
}
181-
182-
183-
184-
185-

src/Composer/Read/ReadRegistersBuilder.php

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace ModbusTcpClient\Composer\Read;
@@ -39,7 +40,7 @@ class ReadRegistersBuilder
3940
/** @var int */
4041
private int $unitId;
4142

42-
public function __construct(string $requestClass, string $uri = null, int $unitId = 0)
43+
public function __construct(string $requestClass, ?string $uri = null, int $unitId = 0)
4344
{
4445
$this->addressSplitter = new ReadRegisterAddressSplitter($requestClass);
4546

@@ -49,12 +50,12 @@ public function __construct(string $requestClass, string $uri = null, int $unitI
4950
$this->unitId = $unitId;
5051
}
5152

52-
public static function newReadHoldingRegisters(string $uri = null, int $unitId = 0): ReadRegistersBuilder
53+
public static function newReadHoldingRegisters(?string $uri = null, int $unitId = 0): ReadRegistersBuilder
5354
{
5455
return new ReadRegistersBuilder(ReadHoldingRegistersRequest::class, $uri, $unitId);
5556
}
5657

57-
public static function newReadInputRegisters(string $uri = null, int $unitId = 0): ReadRegistersBuilder
58+
public static function newReadInputRegisters(?string $uri = null, int $unitId = 0): ReadRegistersBuilder
5859
{
5960
return new ReadRegistersBuilder(ReadInputRegistersRequest::class, $uri, $unitId);
6061
}
@@ -206,27 +207,26 @@ public function fromArray(array $register): ReadRegistersBuilder
206207
return $this;
207208
}
208209

209-
public function bit(int $address, int $nthBit, string $name = null, callable $callback = null, callable $errorCallback = null): ReadRegistersBuilder
210+
public function bit(int $address, int $nthBit, ?string $name = null, ?callable $callback = null, ?callable $errorCallback = null): ReadRegistersBuilder
210211
{
211212
if ($nthBit < 0 || $nthBit > 15) {
212213
throw new InvalidArgumentException("Invalid bit number in for register given! nthBit: '{$nthBit}', address: {$address}");
213214
}
214215
return $this->addAddress(new BitReadRegisterAddress($address, $nthBit, $name, $callback, $errorCallback));
215216
}
216217

217-
public function byte(int $address, bool $firstByte = true, string $name = null, callable $callback = null, callable $errorCallback = null): ReadRegistersBuilder
218+
public function byte(int $address, bool $firstByte = true, ?string $name = null, ?callable $callback = null, ?callable $errorCallback = null): ReadRegistersBuilder
218219
{
219220
return $this->addAddress(new ByteReadRegisterAddress($address, $firstByte, $name, $callback, $errorCallback));
220221
}
221222

222223
public function int16(
223224
int $address,
224-
string $name = null,
225-
callable $callback = null,
226-
callable $errorCallback = null,
227-
int $endian = null
228-
): ReadRegistersBuilder
229-
{
225+
?string $name = null,
226+
?callable $callback = null,
227+
?callable $errorCallback = null,
228+
?int $endian = null
229+
): ReadRegistersBuilder {
230230
$r = new ReadRegisterAddress(
231231
$address,
232232
Address::TYPE_INT16,
@@ -240,12 +240,11 @@ public function int16(
240240

241241
public function uint16(
242242
int $address,
243-
string $name = null,
244-
callable $callback = null,
245-
callable $errorCallback = null,
246-
int $endian = null
247-
): ReadRegistersBuilder
248-
{
243+
?string $name = null,
244+
?callable $callback = null,
245+
?callable $errorCallback = null,
246+
?int $endian = null
247+
): ReadRegistersBuilder {
249248
$r = new ReadRegisterAddress(
250249
$address,
251250
Address::TYPE_UINT16,
@@ -259,12 +258,11 @@ public function uint16(
259258

260259
public function int32(
261260
int $address,
262-
string $name = null,
263-
callable $callback = null,
264-
callable $errorCallback = null,
265-
int $endian = null
266-
): ReadRegistersBuilder
267-
{
261+
?string $name = null,
262+
?callable $callback = null,
263+
?callable $errorCallback = null,
264+
?int $endian = null
265+
): ReadRegistersBuilder {
268266
$r = new ReadRegisterAddress(
269267
$address,
270268
Address::TYPE_INT32,
@@ -278,12 +276,11 @@ public function int32(
278276

279277
public function uint32(
280278
int $address,
281-
string $name = null,
282-
callable $callback = null,
283-
callable $errorCallback = null,
284-
int $endian = null
285-
): ReadRegistersBuilder
286-
{
279+
?string $name = null,
280+
?callable $callback = null,
281+
?callable $errorCallback = null,
282+
?int $endian = null
283+
): ReadRegistersBuilder {
287284
$r = new ReadRegisterAddress(
288285
$address,
289286
Address::TYPE_UINT32,
@@ -297,12 +294,11 @@ public function uint32(
297294

298295
public function uint64(
299296
int $address,
300-
string $name = null,
301-
callable $callback = null,
302-
callable $errorCallback = null,
303-
int $endian = null
304-
): ReadRegistersBuilder
305-
{
297+
?string $name = null,
298+
?callable $callback = null,
299+
?callable $errorCallback = null,
300+
?int $endian = null
301+
): ReadRegistersBuilder {
306302
$r = new ReadRegisterAddress(
307303
$address,
308304
Address::TYPE_UINT64,
@@ -316,12 +312,11 @@ public function uint64(
316312

317313
public function int64(
318314
int $address,
319-
string $name = null,
320-
callable $callback = null,
321-
callable $errorCallback = null,
322-
int $endian = null
323-
): ReadRegistersBuilder
324-
{
315+
?string $name = null,
316+
?callable $callback = null,
317+
?callable $errorCallback = null,
318+
?int $endian = null
319+
): ReadRegistersBuilder {
325320
$r = new ReadRegisterAddress(
326321
$address,
327322
Address::TYPE_INT64,
@@ -335,12 +330,11 @@ public function int64(
335330

336331
public function float(
337332
int $address,
338-
string $name = null,
339-
callable $callback = null,
340-
callable $errorCallback = null,
341-
int $endian = null
342-
): ReadRegistersBuilder
343-
{
333+
?string $name = null,
334+
?callable $callback = null,
335+
?callable $errorCallback = null,
336+
?int $endian = null
337+
): ReadRegistersBuilder {
344338
$r = new ReadRegisterAddress(
345339
$address,
346340
Address::TYPE_FLOAT,
@@ -354,12 +348,11 @@ public function float(
354348

355349
public function double(
356350
int $address,
357-
string $name = null,
358-
callable $callback = null,
359-
callable $errorCallback = null,
360-
int $endian = null
361-
): ReadRegistersBuilder
362-
{
351+
?string $name = null,
352+
?callable $callback = null,
353+
?callable $errorCallback = null,
354+
?int $endian = null
355+
): ReadRegistersBuilder {
363356
$r = new ReadRegisterAddress(
364357
$address,
365358
Address::TYPE_DOUBLE,
@@ -374,12 +367,11 @@ public function double(
374367
public function string(
375368
int $address,
376369
int $byteLength,
377-
string $name = null,
378-
callable $callback = null,
379-
callable $errorCallback = null,
380-
int $endian = null
381-
): ReadRegistersBuilder
382-
{
370+
?string $name = null,
371+
?callable $callback = null,
372+
?callable $errorCallback = null,
373+
?int $endian = null
374+
): ReadRegistersBuilder {
383375
if ($byteLength < 1 || $byteLength > 228) {
384376
throw new InvalidArgumentException("Out of range string length for given! length: '{$byteLength}', address: {$address}");
385377
}
@@ -399,8 +391,3 @@ public function isNotEmpty(): bool
399391
return !empty($this->addresses);
400392
}
401393
}
402-
403-
404-
405-
406-

0 commit comments

Comments
 (0)