Skip to content

Commit 33864ee

Browse files
authored
Merge pull request #991 from mike42/thicolares-add-strict-types
Add strict types (w/ modifications)
2 parents 32d6c83 + 5f27884 commit 33864ee

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

src/Mike42/Escpos/CodePage.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/**
43
* This file is part of escpos-php: PHP receipt printer library for use with
54
* ESC/POS-compatible thermal and impact printers.
@@ -166,7 +165,7 @@ protected static function generateEncodingArray(string $encodingName) : array
166165
// Try to identify the UTF-8 character at this position in the code page
167166
$encodingChar = chr($char);
168167
$utf8 = $converter ->convert($encodingChar, false);
169-
if ($utf8 === $missingChar) {
168+
if ($utf8 === $missingChar || $utf8 === false) {
170169
// Cannot be mapped to unicode
171170
continue;
172171
}

src/Mike42/Escpos/ImagickEscposImage.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/**
43
* This file is part of escpos-php: PHP receipt printer library for use with
54
* ESC/POS-compatible thermal and impact printers.
@@ -117,7 +116,7 @@ private function getColumnFormatFromImage(Imagick $im, int $lineHeight)
117116
return [$this -> getRasterBlobFromImage($im)];
118117
} elseif ($imgWidth > $lineHeight) {
119118
// Calculations
120-
$slicesLeft = ceil($imgWidth / $lineHeight / 2);
119+
$slicesLeft = (int)ceil($imgWidth / $lineHeight / 2); // TODO avoid use of floats with intdiv()
121120
$widthLeft = $slicesLeft * $lineHeight;
122121
$widthRight = $imgWidth - $widthLeft;
123122
// Slice up (left)
@@ -149,11 +148,11 @@ private function getImageFromFile($filename)
149148
{
150149
$im = new Imagick();
151150
try {
152-
$im->setResourceLimit(6, 1); // Prevent libgomp1 segfaults, grumble grumble.
151+
$im -> setResourceLimit(6, 1); // Prevent libgomp1 segfaults, grumble grumble.
153152
$im -> readimage($filename);
154153
} catch (\ImagickException $e) {
155154
/* Re-throw as normal exception */
156-
throw new Exception($e);
155+
throw new Exception($e -> getMessage());
157156
}
158157
return $im;
159158
}
@@ -247,7 +246,7 @@ public static function loadPdf($pdfFile, int $pageWidth = 550)
247246
} catch (\ImagickException $e) {
248247
/* Wrap in normal exception, so that classes which call this do not
249248
* themselves require imagick as a dependency. */
250-
throw new Exception($e);
249+
throw new Exception($e -> getMessage());
251250
}
252251
}
253252

src/Mike42/Escpos/PrintConnectors/NetworkPrintConnector.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/**
43
* This file is part of escpos-php: PHP receipt printer library for use with
54
* ESC/POS-compatible thermal and impact printers.
@@ -24,17 +23,19 @@ class NetworkPrintConnector extends FilePrintConnector
2423
* Construct a new NetworkPrintConnector
2524
*
2625
* @param string $ip IP address or hostname to use.
27-
* @param string $port The port number to connect on.
28-
* @param string $timeout The connection timeout, in seconds.
26+
* @param int $port The port number to connect on.
27+
* @param int $timeout The connection timeout, in seconds.
2928
* @throws Exception Where the socket cannot be opened.
3029
*/
31-
public function __construct(string $ip, int $port = 9100, bool $timeout = false)
30+
public function __construct(string $ip, int $port = 9100, int $timeout = -1)
3231
{
33-
// Default to 60 if default_socket_timeout isn't defined in the ini
34-
$defaultSocketTimeout = ini_get("default_socket_timeout") ?: 60;
35-
$timeout = $timeout ?: $defaultSocketTimeout;
36-
37-
$this -> fp = @fsockopen($ip, $port, $errno, $errstr, $timeout);
32+
// Note: Once the minimum PHP version is PHP 7.0 or higher, we can type $timeout as '?int' to make it optional
33+
// instead of using -1.
34+
if ($timeout == -1) {
35+
$this -> fp = @fsockopen($ip, $port, $errno, $errstr);
36+
} else {
37+
$this -> fp = @fsockopen($ip, $port, $errno, $errstr, (float)$timeout);
38+
}
3839
if ($this -> fp === false) {
3940
throw new Exception("Cannot initialise NetworkPrintConnector: " . $errstr);
4041
}

src/Mike42/Escpos/Printer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/**
43
* This file is part of escpos-php: PHP receipt printer library for use with
54
* ESC/POS-compatible thermal and impact printers.
@@ -691,7 +690,10 @@ public function pulse(int $pin = 0, int $on_ms = 120, int $off_ms = 240)
691690
self::validateInteger($pin, 0, 1, __FUNCTION__);
692691
self::validateInteger($on_ms, 1, 511, __FUNCTION__);
693692
self::validateInteger($off_ms, 1, 511, __FUNCTION__);
694-
$this -> connector -> write(self::ESC . "p" . chr($pin + 48) . chr($on_ms / 2) . chr($off_ms / 2));
693+
$pin_value = $pin + 48; // Character '0' or '1'.
694+
$on_value = intdiv($on_ms, 2);
695+
$off_value = intdiv($off_ms, 2);
696+
$this -> connector -> write(self::ESC . "p" . chr($pin_value) . chr($on_value) . chr($off_value));
695697
}
696698

697699
/**

test/integration/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function runExample($fn, $syntaxCheck = false)
194194
protected function requireGraphicsLibrary()
195195
{
196196
if (!EscposImage::isGdLoaded() && !EscposImage::isImagickLoaded()) {
197-
$this -> markTestSkipped("This test requires a graphics library.");
197+
$this -> markTestSkipped("gd or imagick plugin is required for this test");
198198
}
199199
}
200200
}

test/unit/GdEscposImageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testGdWhite()
6969
private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $columnFormat = null)
7070
{
7171
if (!EscposImage::isGdLoaded()) {
72-
$this -> markTestSkipped("imagick plugin is required for this test");
72+
$this -> markTestSkipped("gd plugin is required for this test");
7373
}
7474
$onDisk = ($fn === null ? null : (dirname(__FILE__) . "/resources/$fn"));
7575
// With optimisations

0 commit comments

Comments
 (0)