-
-
diff --git a/example/rawbt-receipt.php b/example/rawbt-receipt.php
deleted file mode 100644
index 45a4e8fc..00000000
--- a/example/rawbt-receipt.php
+++ /dev/null
@@ -1,145 +0,0 @@
-getSupportsGraphics()) {
- $printer->graphics($logo);
- }
- if ($profile->getSupportsBitImageRaster() && !$profile->getSupportsGraphics()) {
- $printer->bitImage($logo);
- }
-
- /* Name of shop */
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
- $printer->text("ExampleMart Ltd.\n");
- $printer->selectPrintMode();
- $printer->text("Shop No. 42.\n");
- $printer->feed();
-
-
- /* Title of receipt */
- $printer->setEmphasis(true);
- $printer->text("SALES INVOICE\n");
- $printer->setEmphasis(false);
-
- /* Items */
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->setEmphasis(true);
- $printer->text(new item('', '$'));
- $printer->setEmphasis(false);
- foreach ($items as $item) {
- $printer->text($item->getAsString(32)); // for 58mm Font A
- }
- $printer->setEmphasis(true);
- $printer->text($subtotal->getAsString(32));
- $printer->setEmphasis(false);
- $printer->feed();
-
- /* Tax and total */
- $printer->text($tax->getAsString(32));
- $printer->selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
- $printer->text($total->getAsString(32));
- $printer->selectPrintMode();
-
- /* Footer */
- $printer->feed(2);
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->text("Thank you for shopping\n");
- $printer->text("at ExampleMart\n");
- $printer->text("For trading hours,\n");
- $printer->text("please visit example.com\n");
- $printer->feed(2);
- $printer->text($date . "\n");
-
- /* Barcode Default look */
-
- $printer->barcode("ABC", Printer::BARCODE_CODE39);
- $printer->feed();
- $printer->feed();
-
-
-// Demo that alignment QRcode is the same as text
- $printer2 = new Printer($connector); // dirty printer profile hack !!
- $printer2->setJustification(Printer::JUSTIFY_CENTER);
- $printer2->qrCode("https://rawbt.ru/mike42", Printer::QR_ECLEVEL_M, 8);
- $printer2->text("rawbt.ru/mike42\n");
- $printer2->setJustification();
- $printer2->feed();
-
-
- /* Cut the receipt and open the cash drawer */
- $printer->cut();
- $printer->pulse();
-
-} catch (Exception $e) {
- echo $e->getMessage();
-} finally {
- $printer->close();
-}
-
-/* A wrapper to do organise item names & prices into columns */
-
-class item
-{
- private $name;
- private $price;
- private $dollarSign;
-
- public function __construct($name = '', $price = '', $dollarSign = false)
- {
- $this->name = $name;
- $this->price = $price;
- $this->dollarSign = $dollarSign;
- }
-
- public function getAsString($width = 48)
- {
- $rightCols = 10;
- $leftCols = $width - $rightCols;
- if ($this->dollarSign) {
- $leftCols = $leftCols / 2 - $rightCols / 2;
- }
- $left = str_pad($this->name, $leftCols);
-
- $sign = ($this->dollarSign ? '$ ' : '');
- $right = str_pad($sign . $this->price, $rightCols, ' ', STR_PAD_LEFT);
- return "$left$right\n";
- }
-
- public function __toString()
- {
- return $this->getAsString();
- }
-
-}
diff --git a/example/receipt-with-logo.php b/example/receipt-with-logo.php
index 8a99edc9..27ec84ee 100644
--- a/example/receipt-with-logo.php
+++ b/example/receipt-with-logo.php
@@ -4,6 +4,35 @@
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
+/* A wrapper to organize item names & prices into columns */
+class item
+{
+ private $name;
+ private $price;
+ private $dollarSign;
+
+ public function __construct($name = '', $price = '', $dollarSign = false)
+ {
+ $this -> name = $name;
+ $this -> price = $price;
+ $this -> dollarSign = $dollarSign;
+ }
+
+ public function __toString()
+ {
+ $rightCols = 10;
+ $leftCols = 38;
+ if ($this -> dollarSign) {
+ $leftCols = $leftCols / 2 - $rightCols / 2;
+ }
+ $left = str_pad($this -> name, $leftCols) ;
+
+ $sign = ($this -> dollarSign ? '$ ' : '');
+ $right = str_pad($sign . $this -> price, $rightCols, ' ', STR_PAD_LEFT);
+ return "$left$right\n";
+ }
+}
+
/* Fill in your own connector here */
$connector = new FilePrintConnector("php://stdout");
@@ -73,32 +102,3 @@
$printer -> pulse();
$printer -> close();
-
-/* A wrapper to do organise item names & prices into columns */
-class item
-{
- private $name;
- private $price;
- private $dollarSign;
-
- public function __construct($name = '', $price = '', $dollarSign = false)
- {
- $this -> name = $name;
- $this -> price = $price;
- $this -> dollarSign = $dollarSign;
- }
-
- public function __toString()
- {
- $rightCols = 10;
- $leftCols = 38;
- if ($this -> dollarSign) {
- $leftCols = $leftCols / 2 - $rightCols / 2;
- }
- $left = str_pad($this -> name, $leftCols) ;
-
- $sign = ($this -> dollarSign ? '$ ' : '');
- $right = str_pad($sign . $this -> price, $rightCols, ' ', STR_PAD_LEFT);
- return "$left$right\n";
- }
-}
diff --git a/example/specific/235-get-data.php b/example/specific/235-get-data.php
index 5ca20cfb..b1d4b566 100644
--- a/example/specific/235-get-data.php
+++ b/example/specific/235-get-data.php
@@ -6,11 +6,11 @@
*/
require __DIR__ . '/../../vendor/autoload.php';
use Mike42\Escpos\Printer;
-use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
+use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
use Mike42\Escpos\CapabilityProfile;
// Make sure you load a Star print connector or you may get gibberish.
-$connector = new DummyPrintConnector();
+$connector = new MemoryPrintConnector();
$profile = CapabilityProfile::load("TSP600");
$printer = new Printer($connector);
$printer -> text("Hello world!\n");
diff --git a/phpunit.xml b/phpunit.xml
index 158b7e07..27951daa 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -1,10 +1,5 @@
-
+
+test/unit
@@ -13,9 +8,9 @@ verbose="true"
test/integration
-
-
+
+ src
-
-
+
+
diff --git a/src/Mike42/Escpos/CapabilityProfile.php b/src/Mike42/Escpos/CapabilityProfile.php
index ac55c384..cf592934 100644
--- a/src/Mike42/Escpos/CapabilityProfile.php
+++ b/src/Mike42/Escpos/CapabilityProfile.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -28,84 +28,84 @@ class CapabilityProfile
* @var string $codePageCacheKey
* Hash of the code page data structure, to identify it for caching.
*/
- protected $codePageCacheKey;
+ protected string $codePageCacheKey;
/**
*
* @var array $codePages
* Associtive array of CodePage objects, indicating which encodings the printer supports.
*/
- protected $codePages;
+ protected array $codePages;
/**
*
* @var array $colors
* Not used.
*/
- protected $colors;
+ protected array $colors;
/**
*
* @var array $features
* Feature values.
*/
- protected $features;
+ protected array $features;
/**
*
* @var array $fonts
* Not used
*/
- protected $fonts;
+ protected array $fonts;
/**
*
* @var array $media
* Not used
*/
- protected $media;
+ protected array $media;
/**
*
* @var string $name
* Name of the profile, including model number.
*/
- protected $name;
+ protected string $name;
/**
*
* @var string $notes
* Notes on the profile, null if not set.
*/
- protected $notes;
+ protected string $notes;
/**
*
* @var string $profileId
* ID of the profile.
*/
- protected $profileId;
+ protected string $profileId;
/**
* @var string $vendor
* Name of manufacturer.
*/
- protected $vendor;
+ protected string $vendor;
/**
*
- * @var array $encodings
+ * @var array|null $encodings
* Data structure containing encodings loaded from disk, null if not loaded yet.
*/
- protected static $encodings = null;
+ protected static ?array $encodings = null;
/**
*
- * @var array $profiles
+ * @var array|null $profiles
* Data structure containing profiles loaded from disk, null if not loaded yet.
*/
- protected static $profiles = null;
+ protected static ?array $profiles = null;
/**
* Construct new CapabilityProfile.
@@ -158,11 +158,11 @@ public function getCodePages() : array
*
* @param string $featureName
* Name of the feature to retrieve.
- * @throws \InvalidArgumentException Where the feature does not exist.
+ * @throws InvalidArgumentException Where the feature does not exist.
* The exception will contain suggestions for the closest-named features.
- * @return mixed feature value.
+ * @return bool feature value.
*/
- public function getFeature($featureName)
+ public function getFeature($featureName): bool
{
if (isset($this->features[$featureName])) {
return $this->features[$featureName];
diff --git a/src/Mike42/Escpos/CodePage.php b/src/Mike42/Escpos/CodePage.php
index 54b3cb6e..b497fa1f 100644
--- a/src/Mike42/Escpos/CodePage.php
+++ b/src/Mike42/Escpos/CodePage.php
@@ -1,15 +1,18 @@
-,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
* for details.
*/
+declare(strict_types=1);
+
namespace Mike42\Escpos;
use \InvalidArgumentException;
@@ -146,7 +149,7 @@ public function isEncodable()
}
/**
- * Given an ICU encoding name, generate a 128-entry array, with the unicode code points
+ * Given an ICU encoding name, generate a 128-entry array, with the Unicode code points
* for the character at positions 128-255 in this code page.
*
* @param string $encodingName Name of the encoding
@@ -156,8 +159,13 @@ protected static function generateEncodingArray(string $encodingName) : array
{
// Set up converter for encoding
$missingChar = chr(self::MISSING_CHAR_CODE);
- // Throws a lot of warnings for ambiguous code pages, but fallbacks seem fine.
- $converter = @new \UConverter("UTF-8", $encodingName);
+ try {
+ $converter = new \UConverter("UTF-8", $encodingName);
+ } catch (\IntlException $e) {
+ // In PHP 8.5+ an unknown encoding will throw. We are enumerating every character we know how to print,
+ // and it is common/expected that we will encounter things unsupported by the host OS.
+ return array_fill(0, 128, self::MISSING_CHAR_CODE);
+ }
$converter -> setSubstChars($missingChar);
// Loop through 128 code points
$intArray = array_fill(0, 128, self::MISSING_CHAR_CODE);
@@ -166,12 +174,12 @@ protected static function generateEncodingArray(string $encodingName) : array
$encodingChar = chr($char);
$utf8 = $converter ->convert($encodingChar, false);
if ($utf8 === $missingChar || $utf8 === false) {
- // Cannot be mapped to unicode
+ // Cannot be mapped to Unicode
continue;
}
$reverse = $converter ->convert($utf8, true);
if ($reverse !== $encodingChar) {
- // Avoid conversions which don't reverse well (eg. multi-byte code pages)
+ // Avoid conversions which don't reverse well (eg. multibyte code pages)
continue;
}
// Replace space with the correct character if we found it
diff --git a/src/Mike42/Escpos/Devices/AuresCustomerDisplay.php b/src/Mike42/Escpos/Devices/AuresCustomerDisplay.php
index 3b1ff7a6..3e23d49b 100644
--- a/src/Mike42/Escpos/Devices/AuresCustomerDisplay.php
+++ b/src/Mike42/Escpos/Devices/AuresCustomerDisplay.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -48,7 +48,7 @@ class AuresCustomerDisplay extends Printer
*
* @see \Mike42\Escpos\Printer::initialize()
*/
- public function initialize()
+ public function initialize(): void
{
// Select ESC/POS mode first
$this->selectEscposMode();
@@ -64,7 +64,7 @@ public function initialize()
*
* This device supports other modes, which are not used.
*/
- protected function selectEscposMode()
+ protected function selectEscposMode(): void
{
$this->connector->write("\x02\x05\x43\x31\x03");
}
@@ -77,7 +77,7 @@ protected function selectEscposMode()
* AuresCustomerDisplay::TEXT_VERTICAL_SCROLL or
* AuresCustomerDisplay::TEXT_HORIZONTAL_SCROLL
*/
- public function selectTextScrollMode(int $mode = AuresCustomerDisplay::TEXT_VERTICAL_SCROLL)
+ public function selectTextScrollMode(int $mode = AuresCustomerDisplay::TEXT_VERTICAL_SCROLL): void
{
self::validateInteger($mode, 1, 3, __FUNCTION__);
$this->connector->write("\x1F" . chr($mode));
@@ -86,7 +86,7 @@ public function selectTextScrollMode(int $mode = AuresCustomerDisplay::TEXT_VERT
/**
* Clear the display.
*/
- public function clear()
+ public function clear(): void
{
$this->connector->write("\x0c");
}
@@ -94,7 +94,7 @@ public function clear()
/**
* Instruct the display to show the firmware version.
*/
- public function showFirmwareVersion()
+ public function showFirmwareVersion(): void
{
$this->connector->write("\x02\x05\x56\x01\x03");
}
@@ -102,7 +102,7 @@ public function showFirmwareVersion()
/**
* Instruct the display to begin a self-test/demo sequence.
*/
- public function selfTest()
+ public function selfTest(): void
{
$this->connector->write("\x02\x05\x44\x08\x03");
}
@@ -114,7 +114,7 @@ public function selfTest()
* logo, but that the vendor supplies software
* which has this function.
*/
- public function showLogo()
+ public function showLogo(): void
{
$this->connector->write("\x02\xFC\x55\xAA\x55\xAA");
}
@@ -125,7 +125,7 @@ public function showLogo()
*
* @see \Mike42\Escpos\Printer::text()
*/
- public function text(string $str)
+ public function text(string $str): void
{
// Need to intercept line-feeds, since "\n" is insufficient on this device.
foreach (explode("\n", $str) as $id => $line) {
@@ -142,7 +142,7 @@ public function text(string $str)
*
* @see \Mike42\Escpos\Printer::feed()
*/
- public function feed(int $lines = 1)
+ public function feed(int $lines = 1): void
{
self::validateInteger($lines, 1, 255, __FUNCTION__);
for ($i = 0; $i < $lines; $i ++) {
diff --git a/src/Mike42/Escpos/EscposImage.php b/src/Mike42/Escpos/EscposImage.php
index 44322e8f..3af0c187 100644
--- a/src/Mike42/Escpos/EscposImage.php
+++ b/src/Mike42/Escpos/EscposImage.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -198,7 +198,7 @@ public function toColumnFormat($doubleDensity = false)
*
* @param string|null $filename Filename to load from.
*/
- protected function loadImageData(string $filename = null)
+ protected function loadImageData(?string $filename = null)
{
// Load image in to string of 1's and 0's, also set width & height
$this -> setImgWidth(0);
diff --git a/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyph.php b/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyph.php
index f136a759..adaad45d 100644
--- a/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyph.php
+++ b/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyph.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
diff --git a/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyphFactory.php b/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyphFactory.php
index 3514190c..9c9addfc 100644
--- a/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyphFactory.php
+++ b/src/Mike42/Escpos/Experimental/Unifont/ColumnFormatGlyphFactory.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -17,5 +17,5 @@
interface ColumnFormatGlyphFactory
{
- public function getGlyph($codePoint);
+ public function getGlyph($codePoint): ColumnFormatGlyph;
}
diff --git a/src/Mike42/Escpos/Experimental/Unifont/FontMap.php b/src/Mike42/Escpos/Experimental/Unifont/FontMap.php
index 9d8a6962..e73ae651 100644
--- a/src/Mike42/Escpos/Experimental/Unifont/FontMap.php
+++ b/src/Mike42/Escpos/Experimental/Unifont/FontMap.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -20,7 +20,7 @@
class FontMap
{
- protected $printer;
+ protected Printer $printer;
const MIN = 0x20;
const MAX = 0x7E;
@@ -28,13 +28,15 @@ class FontMap
const FONT_B_WIDTH = 9;
// Map memory locations to code points
- protected $memory;
+ protected array $memory;
- // Map unicode code points to bytes
- protected $chars;
+ // Map Unicode code points to bytes
+ protected array $chars;
// next available slot
- protected $next = 0;
+ protected int $next = 0;
+
+ private ColumnFormatGlyphFactory $glyphFactory;
public function __construct(ColumnFormatGlyphFactory $glyphFactory, Printer $printer)
{
@@ -43,12 +45,12 @@ public function __construct(ColumnFormatGlyphFactory $glyphFactory, Printer $pri
$this -> reset();
}
- public function cacheChars(array $codePoints)
+ public function cacheChars(array $codePoints): void
{
// TODO flush existing cache to fill with these chars.
}
- public function writeChar(int $codePoint)
+ public function writeChar(int $codePoint): void
{
if (!$this -> addChar($codePoint, true)) {
throw new InvalidArgumentException("Code point $codePoint not available");
@@ -57,18 +59,18 @@ public function writeChar(int $codePoint)
$this -> printer -> getPrintConnector() -> write($data);
}
- public function reset()
+ public function reset(): void
{
$this -> chars = [];
- $this -> memory = array_fill(0, (\Mike42\Escpos\Experimental\Unifont\FontMap::MAX - FontMap::MIN) + 1, -1);
+ $this -> memory = array_fill(0, (FontMap::MAX - FontMap::MIN) + 1, -1);
}
- public function occupied($id)
+ public function occupied($id): bool
{
return $this -> memory[$id] !== -1;
}
- public function evict($id)
+ public function evict($id): bool
{
if (!$this -> occupied($id)) {
return true;
@@ -78,7 +80,7 @@ public function evict($id)
return true;
}
- public function addChar(int $codePoint, $evict = true)
+ public function addChar(int $codePoint, $evict = true): bool
{
if (isset($this -> chars[$codePoint])) {
// Char already available
@@ -116,7 +118,7 @@ public function addChar(int $codePoint, $evict = true)
return true;
}
- public function submitCharsToPrinterFont(array $chars)
+ public function submitCharsToPrinterFont(array $chars): void
{
ksort($chars);
// TODO We can sort into batches of contiguous characters here.
diff --git a/src/Mike42/Escpos/Experimental/Unifont/UnifontGlyphFactory.php b/src/Mike42/Escpos/Experimental/Unifont/UnifontGlyphFactory.php
index ec24e294..c58df537 100644
--- a/src/Mike42/Escpos/Experimental/Unifont/UnifontGlyphFactory.php
+++ b/src/Mike42/Escpos/Experimental/Unifont/UnifontGlyphFactory.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -17,15 +17,15 @@
class UnifontGlyphFactory implements ColumnFormatGlyphFactory
{
- protected $unifontFile;
+ protected array $unifontFile;
- public static function colFormat16(array $in)
+ public static function colFormat16(array $in): string
{
// Map 16 x 16 bit unifont (32 bytes) to 16 x 24 ESC/POS column format image (48 bytes).
return UnifontGlyphFactory::colFormat8($in, 2, 1) . UnifontGlyphFactory::colFormat8($in, 2, 2);
}
- public static function colFormat8(array $in, $chars = 1, $idx = 1)
+ public static function colFormat8(array $in, $chars = 1, $idx = 1): string
{
// Map 8 x 16 bit unifont (32 bytes) to 8 x 24 ESC/POS column format image (24 bytes).
return implode([
@@ -205,7 +205,7 @@ public function __construct(array $unifontFile)
$this -> unifontFile = $unifontFile;
}
- public function getGlyph($codePoint)
+ public function getGlyph($codePoint): ColumnFormatGlyph
{
// Binary search for correct line.
$min = 0;
diff --git a/src/Mike42/Escpos/Experimental/Unifont/UnifontPrintBuffer.php b/src/Mike42/Escpos/Experimental/Unifont/UnifontPrintBuffer.php
index dad95490..6d060365 100644
--- a/src/Mike42/Escpos/Experimental/Unifont/UnifontPrintBuffer.php
+++ b/src/Mike42/Escpos/Experimental/Unifont/UnifontPrintBuffer.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -40,7 +40,7 @@ public function __construct(string $unifontFilename)
$this -> started = false;
}
- public function writeChar(int $codePoint)
+ public function writeChar(int $codePoint): void
{
if ($codePoint == 10) {
$this -> write("\n");
@@ -52,7 +52,7 @@ public function writeChar(int $codePoint)
}
}
- public function writeText(string $text)
+ public function writeText(string $text): void
{
if (!$this -> started) {
$mode = Printer::MODE_FONT_B | Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH;
@@ -73,21 +73,21 @@ public function writeText(string $text)
}
}
- public function flush()
+ public function flush(): void
{
}
- public function setPrinter(Printer $printer = null)
+ public function setPrinter(?Printer $printer = null): void
{
$this -> printer = $printer;
$this -> fontMap = new FontMap($this -> unifont, $this -> printer);
}
- public function writeTextRaw(string $text)
+ public function writeTextRaw(string $text): void
{
}
- public function getPrinter()
+ public function getPrinter(): ?Printer
{
return $this -> printer;
}
@@ -97,7 +97,7 @@ public function getPrinter()
*
* @param string $data
*/
- private function write($data)
+ private function write($data): void
{
$this -> printer -> getPrintConnector() -> write($data);
}
diff --git a/src/Mike42/Escpos/GdEscposImage.php b/src/Mike42/Escpos/GdEscposImage.php
index 57eed180..ddd39cd9 100644
--- a/src/Mike42/Escpos/GdEscposImage.php
+++ b/src/Mike42/Escpos/GdEscposImage.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -29,7 +29,7 @@ class GdEscposImage extends EscposImage
* @throws Exception if the image format is not supported,
* or the file cannot be opened.
*/
- protected function loadImageData(string $filename = null)
+ protected function loadImageData(?string $filename = null)
{
if ($filename === null) {
/* Set to blank image */
diff --git a/src/Mike42/Escpos/ImagickEscposImage.php b/src/Mike42/Escpos/ImagickEscposImage.php
index aa7c7750..83baa640 100644
--- a/src/Mike42/Escpos/ImagickEscposImage.php
+++ b/src/Mike42/Escpos/ImagickEscposImage.php
@@ -1,15 +1,18 @@
-,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
* for details.
*/
+declare(strict_types=1);
+
namespace Mike42\Escpos;
use Exception;
@@ -88,7 +91,7 @@ protected function getColumnFormatFromFile($filename = null, $highDensityVertica
* @throws Exception if the image format is not supported,
* or the file cannot be opened.
*/
- protected function loadImageData(string $filename = null)
+ protected function loadImageData(?string $filename = null)
{
if ($filename === null) {
/* Set to blank image */
diff --git a/src/Mike42/Escpos/NativeEscposImage.php b/src/Mike42/Escpos/NativeEscposImage.php
index 960d4989..753b3c4a 100644
--- a/src/Mike42/Escpos/NativeEscposImage.php
+++ b/src/Mike42/Escpos/NativeEscposImage.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -22,7 +22,7 @@
*/
class NativeEscposImage extends EscposImage
{
- protected function loadImageData(string $filename = null)
+ protected function loadImageData(?string $filename = null)
{
$image = Image::fromFile($filename) -> toRgb() -> toBlackAndWhite();
$imgHeight = $image -> getHeight();
diff --git a/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php b/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php
index 60382d74..846b749f 100644
--- a/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php
+++ b/src/Mike42/Escpos/PrintBuffers/EscposPrintBuffer.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -62,7 +62,7 @@ public function __construct()
$this -> printer = null;
}
- public function flush()
+ public function flush(): void
{
if ($this -> printer == null) {
throw new LogicException("Not attached to a printer.");
@@ -70,12 +70,12 @@ public function flush()
// TODO Not yet implemented for this buffer: This indicates that the printer needs the current line to be ended.
}
- public function getPrinter()
+ public function getPrinter(): ?Printer
{
return $this -> printer;
}
- public function setPrinter(Printer $printer = null)
+ public function setPrinter(?Printer $printer = null): void
{
$this -> printer = $printer;
if ($printer != null) {
@@ -83,7 +83,7 @@ public function setPrinter(Printer $printer = null)
}
}
- public function writeText(string $text)
+ public function writeText(string $text): void
{
if ($this->printer == null) {
throw new LogicException("Not attached to a printer.");
@@ -119,7 +119,7 @@ public function writeText(string $text)
}
}
- public function writeTextRaw(string $text)
+ public function writeTextRaw(string $text): void
{
if ($this -> printer == null) {
throw new LogicException("Not attached to a printer.");
@@ -152,7 +152,7 @@ public function writeTextRaw(string $text)
* @return boolean|integer Code page number, or FALSE if the text is not
* printable on any supported encoding.
*/
- private function identifyText(int $codePoint)
+ private function identifyText(int $codePoint): bool|int
{
if (!isset($this -> available[$codePoint])) {
/* Character not available anywhere */
@@ -165,7 +165,7 @@ private function identifyText(int $codePoint)
* Based on the printer's connector, compute (or load a cached copy of) maps
* of UTF character to unicode characters for later use.
*/
- private function loadAvailableCharacters()
+ private function loadAvailableCharacters(): void
{
$profile = $this -> printer -> getPrinterCapabilityProfile();
$supportedCodePages = $profile -> getCodePages();
@@ -238,7 +238,7 @@ private function loadAvailableCharacters()
* @param array $codePoints Text to print, as list of unicode code points
* @param integer $encodingNo Encoding number to use- assumed to exist.
*/
- private function writeTextUsingEncoding(array $codePoints, int $encodingNo)
+ private function writeTextUsingEncoding(array $codePoints, int $encodingNo): void
{
$encodeMap = $this -> encode[$encodingNo];
$len = count($codePoints);
@@ -271,7 +271,7 @@ private function writeTextUsingEncoding(array $codePoints, int $encodingNo)
*
* @param string $data
*/
- private function write(string $data)
+ private function write(string $data): void
{
$this -> printer -> getPrintConnector() -> write($data);
}
@@ -283,7 +283,7 @@ private function write(string $data)
* @param boolean $extended True to allow 128-256 values also (excluded by default)
* @return boolean True if the character is printable, false if it is not.
*/
- private static function asciiCheck(string $char, bool $extended = false)
+ private static function asciiCheck(string $char, bool $extended = false): bool
{
if (strlen($char) != 1) {
// Multi-byte string
diff --git a/src/Mike42/Escpos/PrintBuffers/ImagePrintBuffer.php b/src/Mike42/Escpos/PrintBuffers/ImagePrintBuffer.php
index 4203e1ae..b0827f9f 100644
--- a/src/Mike42/Escpos/PrintBuffers/ImagePrintBuffer.php
+++ b/src/Mike42/Escpos/PrintBuffers/ImagePrintBuffer.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -28,17 +28,18 @@
*/
class ImagePrintBuffer implements PrintBuffer
{
- private $printer;
+ private ?Printer $printer;
/**
* @var string|null font to use
*/
- private $font;
+ private ?string $font;
- private $fontSize;
+ private int $fontSize;
public function __construct()
{
+ $this -> printer = null;
if (!EscposImage::isImagickLoaded()) {
throw new Exception("ImagePrintBuffer requires the imagick extension");
}
@@ -46,24 +47,24 @@ public function __construct()
$this -> fontSize = 24;
}
- public function flush()
+ public function flush(): void
{
if ($this -> printer == null) {
throw new LogicException("Not attached to a printer.");
}
}
- public function getPrinter()
+ public function getPrinter(): ?Printer
{
return $this -> printer;
}
- public function setPrinter(Printer $printer = null)
+ public function setPrinter(?Printer $printer = null): void
{
$this -> printer = $printer;
}
- public function writeText(string $text)
+ public function writeText(string $text): void
{
if ($this -> printer == null) {
throw new LogicException("Not attached to a printer.");
@@ -90,9 +91,8 @@ public function writeText(string $text)
$draw -> setTextAntialias(true);
$metrics = $image -> queryFontMetrics($draw, $text);
$draw -> annotation(0, $metrics['ascender'], $text);
-
/* Create image & draw annotation on it */
- $image -> newImage($metrics['textWidth'], $metrics['textHeight'], $background);
+ $image -> newImage((int)$metrics['textWidth'], (int)$metrics['textHeight'], $background);
$image -> setImageFormat('png');
$image -> drawImage($draw);
// debugging if you want to view the images yourself
@@ -105,7 +105,7 @@ public function writeText(string $text)
$this -> printer -> bitImage($escposImage, $size);
}
- public function writeTextRaw(string $text)
+ public function writeTextRaw(string $text): void
{
if ($this -> printer == null) {
throw new LogicException("Not attached to a printer.");
@@ -123,7 +123,7 @@ public function writeTextRaw(string $text)
* @param string $font
* Font name or a filename
*/
- public function setFont(string $font)
+ public function setFont(string $font): void
{
$this->font = $font;
}
@@ -131,7 +131,7 @@ public function setFont(string $font)
/**
* Numeric font size for rendering text to image
*/
- public function setFontSize(int $fontSize)
+ public function setFontSize(int $fontSize): void
{
$this->fontSize = $fontSize;
}
diff --git a/src/Mike42/Escpos/PrintBuffers/PrintBuffer.php b/src/Mike42/Escpos/PrintBuffers/PrintBuffer.php
index 384c18ff..4da947b6 100644
--- a/src/Mike42/Escpos/PrintBuffers/PrintBuffer.php
+++ b/src/Mike42/Escpos/PrintBuffers/PrintBuffer.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -36,31 +36,31 @@ interface PrintBuffer
* Cause the buffer to send any partial input and wait on a newline.
* If the printer is already on a new line, this does nothing.
*/
- public function flush();
+ public function flush(): void;
/**
* Used by Escpos to check if a printer is set.
*/
- public function getPrinter();
+ public function getPrinter(): ?Printer;
/**
* Used by Escpos to hook up one-to-one link between buffers and printers.
*
* @param Printer|null $printer New printer
*/
- public function setPrinter(Printer $printer = null);
+ public function setPrinter(?Printer $printer = null): void;
/**
* Accept UTF-8 text for printing.
*
* @param string $text Text to print
*/
- public function writeText(string $text);
+ public function writeText(string $text): void;
/**
* Accept 8-bit text in the current encoding and add it to the buffer.
*
* @param string $text Text to print, already the target encoding.
*/
- public function writeTextRaw(string $text);
+ public function writeTextRaw(string $text): void;
}
diff --git a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php
index 3ef25c8f..9c1710d6 100644
--- a/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/CupsPrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -26,17 +26,17 @@ class CupsPrintConnector implements PrintConnector
{
/**
- * @var array $buffer
+ * @var array|null $buffer
* Buffer of accumilated data.
*/
- private $buffer;
+ private array|null $buffer;
/**
*
* @var string $printerName
* The name of the target printer.
*/
- private $printerName;
+ private string $printerName;
/**
* Construct new CUPS print connector.
@@ -45,8 +45,9 @@ class CupsPrintConnector implements PrintConnector
* The CUPS printer name to print to. This must be loaded using a raw driver.
* @throws BadMethodCallException
*/
- public function __construct($dest)
+ public function __construct(string $dest)
{
+ $this->buffer = null; // just ensurer this is initialized - __destruct() will call if an exception is thrown below.
$valid = $this->getLocalPrinters();
if (count($valid) == 0) {
throw new BadMethodCallException("You do not have any printers installed on " .
@@ -57,7 +58,7 @@ public function __construct($dest)
throw new BadMethodCallException("'$dest' is not a printer on this system. " .
"Printers are: [" . implode(", ", $valid) . "]");
}
- $this->buffer = array ();
+ $this->buffer = array();
$this->printerName = $dest;
}
@@ -74,7 +75,7 @@ public function __destruct()
/**
* Send job to printer.
*/
- public function finalize()
+ public function finalize(): void
{
$data = implode($this->buffer);
$this->buffer = null;
@@ -106,7 +107,7 @@ public function finalize()
* @param string $cmd
* Command to run
*/
- protected function getCmdOutput($cmd)
+ protected function getCmdOutput($cmd): false|string
{
$descriptors = array (
1 => array (
@@ -139,10 +140,10 @@ protected function getCmdOutput($cmd)
/**
* Read data from the printer.
*
- * @param string $len Length of data to read.
- * @return string Data read from the printer, or false where reading is not possible.
+ * @param int $len Length of data to read.
+ * @return string|boolean Data read from the printer, or false where reading is not possible.
*/
- public function read($len)
+ public function read(int $len): bool|string
{
return false;
}
@@ -150,7 +151,7 @@ public function read($len)
/**
* @param string $data
*/
- public function write($data)
+ public function write(string $data): void
{
$this->buffer [] = $data;
}
diff --git a/src/Mike42/Escpos/PrintConnectors/DummyPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/DummyPrintConnector.php
index 9b27605d..dd0d958a 100644
--- a/src/Mike42/Escpos/PrintConnectors/DummyPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/DummyPrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -17,65 +17,12 @@
/**
* Print connector that writes to nowhere, but allows the user to retrieve the
- * buffered data. Used for testing.
+ * buffered data.
+ *
+ * @deprecated Renamed to {@see MemoryPrintConnector}. This subclass is retained
+ * for backwards compatibility and will be removed in a future major version.
+ * Use MemoryPrintConnector in new code.
*/
-final class DummyPrintConnector implements PrintConnector
+class DummyPrintConnector extends MemoryPrintConnector
{
- /**
- * @var array $buffer
- * Buffer of accumilated data.
- */
- private $buffer;
-
- /**
- * @var string data which the printer will provide on next read
- */
- private $readData;
-
- /**
- * Create new print connector
- */
- public function __construct()
- {
- $this -> buffer = [];
- }
-
- public function clear()
- {
- $this -> buffer = [];
- }
-
- public function __destruct()
- {
- if ($this -> buffer !== null) {
- trigger_error("Print connector was not finalized. Did you forget to close the printer?", E_USER_NOTICE);
- }
- }
-
- public function finalize()
- {
- $this -> buffer = null;
- }
-
- /**
- * @return string Get the accumulated data that has been sent to this buffer.
- */
- public function getData()
- {
- return implode($this -> buffer);
- }
-
- /**
- * {@inheritDoc}
- * @see PrintConnector::read()
- */
- public function read($len)
- {
- return $len >= strlen($this -> readData) ? $this -> readData : substr($this -> readData, 0, $len);
- }
-
- public function write($data)
- {
- $this -> buffer[] = $data;
- }
}
diff --git a/src/Mike42/Escpos/PrintConnectors/FilePrintConnector.php b/src/Mike42/Escpos/PrintConnectors/FilePrintConnector.php
index 0ab30637..70d90299 100644
--- a/src/Mike42/Escpos/PrintConnectors/FilePrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/FilePrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -23,7 +23,7 @@
class FilePrintConnector implements PrintConnector
{
/**
- * @var resource $fp
+ * @var false|resource $fp
* The file pointer to send data to.
*/
protected $fp;
@@ -33,7 +33,7 @@ class FilePrintConnector implements PrintConnector
*
* @param string $filename
*/
- public function __construct($filename)
+ public function __construct(string $filename)
{
$this -> fp = fopen($filename, "wb+");
if ($this -> fp === false) {
@@ -51,7 +51,7 @@ public function __destruct()
/**
* Close file pointer
*/
- public function finalize()
+ public function finalize(): void
{
if ($this -> fp !== false) {
fclose($this -> fp);
@@ -62,7 +62,7 @@ public function finalize()
/* (non-PHPdoc)
* @see PrintConnector::read()
*/
- public function read($len)
+ public function read(int $len): bool|string
{
if ($this -> fp === false) {
throw new Exception("PrintConnector has been closed, cannot read input.");
@@ -75,7 +75,7 @@ public function read($len)
*
* @param string $data
*/
- public function write($data)
+ public function write(string $data): void
{
if ($this -> fp === false) {
throw new Exception("PrintConnector has been closed, cannot send output.");
diff --git a/src/Mike42/Escpos/PrintConnectors/MemoryPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/MemoryPrintConnector.php
new file mode 100644
index 00000000..dffec7a7
--- /dev/null
+++ b/src/Mike42/Escpos/PrintConnectors/MemoryPrintConnector.php
@@ -0,0 +1,82 @@
+,
+ * incorporating modifications by others. See CONTRIBUTORS.md for a full list.
+ *
+ * This software is distributed under the terms of the MIT license. See LICENSE.md
+ * for details.
+ */
+
+declare(strict_types=1);
+
+namespace Mike42\Escpos\PrintConnectors;
+
+/**
+ * Print connector that writes to nowhere, but allows the user to retrieve the
+ * buffered data. Useful for capturing raw ESC/POS output in memory - for
+ * inspection, for sending over your own transport, or for testing.
+ */
+class MemoryPrintConnector implements PrintConnector
+{
+ /**
+ * @var array $buffer
+ * Buffer of accumilated data.
+ */
+ private $buffer;
+
+ /**
+ * @var string data which the printer will provide on next read
+ */
+ private $readData;
+
+ /**
+ * Create new print connector
+ */
+ public function __construct()
+ {
+ $this -> buffer = [];
+ }
+
+ public function clear()
+ {
+ $this -> buffer = [];
+ }
+
+ public function __destruct()
+ {
+ if ($this -> buffer !== null) {
+ trigger_error("Print connector was not finalized. Did you forget to close the printer?", E_USER_NOTICE);
+ }
+ }
+
+ public function finalize(): void
+ {
+ $this -> buffer = null;
+ }
+
+ /**
+ * @return string Get the accumulated data that has been sent to this buffer.
+ */
+ public function getData()
+ {
+ return implode($this -> buffer);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see PrintConnector::read()
+ */
+ public function read(int $len): bool|string
+ {
+ return $len >= strlen($this -> readData) ? $this -> readData : substr($this -> readData, 0, $len);
+ }
+
+ public function write(string $data): void
+ {
+ $this -> buffer[] = $data;
+ }
+}
diff --git a/src/Mike42/Escpos/PrintConnectors/MultiplePrintConnector.php b/src/Mike42/Escpos/PrintConnectors/MultiplePrintConnector.php
index 7f954195..6a4cc866 100644
--- a/src/Mike42/Escpos/PrintConnectors/MultiplePrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/MultiplePrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -20,27 +20,27 @@
*/
class MultiplePrintConnector implements PrintConnector
{
- private $connectors;
+ private array $connectors;
public function __construct(PrintConnector ...$connectors)
{
$this -> connectors = $connectors;
}
- public function finalize()
+ public function finalize(): void
{
foreach ($this -> connectors as $connector) {
$connector -> finalize();
}
}
- public function read($len)
+ public function read(int $len): bool|string
{
// Cannot write
return false;
}
- public function write($data)
+ public function write(string $data): void
{
foreach ($this -> connectors as $connector) {
$connector -> write($data);
diff --git a/src/Mike42/Escpos/PrintConnectors/NetworkPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/NetworkPrintConnector.php
index 0ca78115..f2cee1b2 100644
--- a/src/Mike42/Escpos/PrintConnectors/NetworkPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/NetworkPrintConnector.php
@@ -1,15 +1,18 @@
-,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
* for details.
*/
+declare(strict_types=1);
+
namespace Mike42\Escpos\PrintConnectors;
use Exception;
diff --git a/src/Mike42/Escpos/PrintConnectors/PrintConnector.php b/src/Mike42/Escpos/PrintConnectors/PrintConnector.php
index e95b54db..8018676f 100644
--- a/src/Mike42/Escpos/PrintConnectors/PrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/PrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -31,20 +31,20 @@ public function __destruct();
* Finish using this print connector (close file, socket, send
* accumulated output, etc).
*/
- public function finalize();
+ public function finalize(): void;
/**
* Read data from the printer.
*
- * @param string $len Length of data to read.
- * @return string Data read from the printer, or false where reading is not possible.
+ * @param int $len Length of data to read.
+ * @return string|boolean Data read from the printer, or false where reading is not possible.
*/
- public function read($len);
+ public function read(int $len): bool|string;
/**
* Write data to the print connector.
*
* @param string $data The data to write
*/
- public function write($data);
+ public function write(string $data): void;
}
diff --git a/src/Mike42/Escpos/PrintConnectors/RawbtPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/RawbtPrintConnector.php
index 92fd5a13..3d4e679d 100644
--- a/src/Mike42/Escpos/PrintConnectors/RawbtPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/RawbtPrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -23,14 +23,9 @@ final class RawbtPrintConnector implements PrintConnector
{
/**
* @var array $buffer
- * Buffer of accumilated data.
+ * Buffer of accumulated data.
*/
- private $buffer;
-
- /**
- * @var string data which the printer will provide on next read
- */
- private $readData;
+ private array|null $buffer;
/**
* Create new print connector
@@ -41,7 +36,7 @@ public function __construct()
$this->buffer = [];
}
- public function clear()
+ public function clear(): void
{
$this->buffer = [];
}
@@ -53,7 +48,7 @@ public function __destruct()
}
}
- public function finalize()
+ public function finalize(): void
{
ob_end_clean();
echo "intent:base64," . base64_encode($this->getData()) . "#Intent;scheme=rawbt;package=ru.a402d.rawbtprinter;end;";
@@ -63,7 +58,7 @@ public function finalize()
/**
* @return string Get the accumulated data that has been sent to this buffer.
*/
- public function getData()
+ public function getData(): string
{
return implode($this->buffer);
}
@@ -72,12 +67,12 @@ public function getData()
* {@inheritDoc}
* @see PrintConnector::read()
*/
- public function read($len)
+ public function read(int $len): bool|string
{
- return $len >= strlen($this->readData) ? $this->readData : substr($this->readData, 0, $len);
+ return false;
}
- public function write($data)
+ public function write(string $data): void
{
$this->buffer[] = $data;
}
diff --git a/src/Mike42/Escpos/PrintConnectors/UriPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/UriPrintConnector.php
index 204a304d..7488de87 100644
--- a/src/Mike42/Escpos/PrintConnectors/UriPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/UriPrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -19,7 +19,7 @@ class UriPrintConnector
{
const URI_ASSEMBLER_PATTERN = "~^(.+):/{2}(.+?)(?::(\d{1,4}))?$~";
- public static function get($uri)
+ public static function get($uri): PrintConnector
{
// Parse URI
$is_uri = preg_match(self::URI_ASSEMBLER_PATTERN, $uri, $uri_parts);
diff --git a/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php b/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
index a1763b5a..d3803d5a 100644
--- a/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
+++ b/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
@@ -4,7 +4,7 @@
* This file is part of escpos-php: PHP receipt printer library for use with
* ESC/POS-compatible thermal and impact printers.
*
- * Copyright (c) 2014-20 Michael Billington < michael.billington@gmail.com >,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
@@ -168,7 +168,7 @@ public function __destruct()
}
}
- public function finalize()
+ public function finalize(): void
{
$data = implode($this -> buffer);
$this -> buffer = null;
@@ -187,7 +187,7 @@ public function finalize()
* @param string $data Print data
* @throws Exception
*/
- protected function finalizeLinux($data)
+ protected function finalizeLinux($data): void
{
/* Non-Windows samba printing */
$device = "//" . $this -> hostname . "/" . $this -> printerName;
@@ -251,7 +251,7 @@ protected function finalizeMac($data)
*
* @param string $data
*/
- protected function finalizeWin($data)
+ protected function finalizeWin($data): void
{
/* Windows-friendly printing of all sorts */
if (!$this -> isLocal) {
@@ -306,9 +306,9 @@ protected function finalizeWin($data)
}
/**
- * @return string Current platform. Separated out for testing purposes.
+ * @return int Current platform. Separated out for testing purposes.
*/
- protected function getCurrentPlatform()
+ protected function getCurrentPlatform(): int
{
if (PHP_OS == "WINNT") {
return self::PLATFORM_WIN;
@@ -322,7 +322,7 @@ protected function getCurrentPlatform()
/* (non-PHPdoc)
* @see PrintConnector::read()
*/
- public function read($len)
+ public function read(int $len): bool|string
{
/* Two-way communication is not supported */
return false;
@@ -334,10 +334,10 @@ public function read($len)
* @param string $command the command to run.
* @param string $outputStr variable to fill with standard output.
* @param string $errorStr variable to fill with standard error.
- * @param string $inputStr text to pass to the command's standard input (optional).
+ * @param string|null $inputStr text to pass to the command's standard input (optional).
* @return number
*/
- protected function runCommand($command, &$outputStr, &$errorStr, $inputStr = null)
+ protected function runCommand(string $command, &$outputStr, &$errorStr, string|null $inputStr = null): int
{
$descriptors = [
0 => ["pipe", "r"],
@@ -390,7 +390,7 @@ protected function runWrite($data, $filename)
return file_put_contents($filename, $data) !== false;
}
- public function write($data)
+ public function write(string $data): void
{
$this -> buffer[] = $data;
}
diff --git a/src/Mike42/Escpos/Printer.php b/src/Mike42/Escpos/Printer.php
index 05c8a6f0..2fc8babe 100644
--- a/src/Mike42/Escpos/Printer.php
+++ b/src/Mike42/Escpos/Printer.php
@@ -1,15 +1,18 @@
-,
+ * Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
*
* This software is distributed under the terms of the MIT license. See LICENSE.md
* for details.
*/
+declare(strict_types=1);
+
namespace Mike42\Escpos;
use Exception;
@@ -330,25 +333,25 @@ class Printer
* @var PrintBuffer|null $buffer
* The printer's output buffer.
*/
- protected $buffer;
+ protected ?PrintBuffer $buffer;
/**
* @var PrintConnector $connector
* Connector showing how to print to this printer
*/
- protected $connector;
+ protected PrintConnector $connector;
/**
* @var CapabilityProfile $profile
* Profile showing supported features for this printer
*/
- protected $profile;
+ protected CapabilityProfile $profile;
/**
* @var int $characterTable
* Current character code table
*/
- protected $characterTable;
+ protected int $characterTable;
/**
* Construct a new print object
@@ -357,7 +360,7 @@ class Printer
* @param CapabilityProfile|null $profile Supported features of this printer. If not set, the "default" CapabilityProfile will be used, which is suitable for Epson printers.
* @throws InvalidArgumentException
*/
- public function __construct(PrintConnector $connector, CapabilityProfile $profile = null)
+ public function __construct(PrintConnector $connector, ?CapabilityProfile $profile = null)
{
/* Set connector */
$this -> connector = $connector;
@@ -387,7 +390,7 @@ public function __construct(PrintConnector $connector, CapabilityProfile $profil
* available barcode types vary between printers.
* @throws InvalidArgumentException Where the length or characters used in $content is invalid for the requested barcode format.
*/
- public function barcode(string $content, int $type = Printer::BARCODE_CODE39)
+ public function barcode(string $content, int $type = Printer::BARCODE_CODE39): void
{
/* Validate input */
self::validateInteger($type, 65, 73, __FUNCTION__, "Barcode type");
@@ -453,7 +456,7 @@ public function barcode(string $content, int $type = Printer::BARCODE_CODE39)
* (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and
* `Printer::IMG_DOUBLE_WIDTH` flags.
*/
- public function bitImage(EscposImage $img, int $size = Printer::IMG_DEFAULT)
+ public function bitImage(EscposImage $img, int $size = Printer::IMG_DEFAULT): void
{
self::validateInteger($size, 0, 3, __FUNCTION__);
$rasterData = $img -> toRasterFormat();
@@ -473,7 +476,7 @@ public function bitImage(EscposImage $img, int $size = Printer::IMG_DEFAULT)
* (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and
* `Printer::IMG_DOUBLE_WIDTH` flags.
*/
- public function bitImageColumnFormat(EscposImage $img, int $size = Printer::IMG_DEFAULT)
+ public function bitImageColumnFormat(EscposImage $img, int $size = Printer::IMG_DEFAULT): void
{
$highDensityVertical = ! (($size & self::IMG_DOUBLE_HEIGHT) == Printer::IMG_DOUBLE_HEIGHT);
$highDensityHorizontal = ! (($size & self::IMG_DOUBLE_WIDTH) == Printer::IMG_DOUBLE_WIDTH);
@@ -497,7 +500,7 @@ public function bitImageColumnFormat(EscposImage $img, int $size = Printer::IMG_
* Close the underlying buffer. With some connectors, the
* job will not actually be sent to the printer until this is called.
*/
- public function close()
+ public function close(): void
{
$this -> connector -> finalize();
}
@@ -508,7 +511,7 @@ public function close()
* @param int $mode Cut mode, either Printer::CUT_FULL or Printer::CUT_PARTIAL. If not specified, `Printer::CUT_FULL` will be used.
* @param int $lines Number of lines to feed
*/
- public function cut(int $mode = Printer::CUT_FULL, int $lines = 3)
+ public function cut(int $mode = Printer::CUT_FULL, int $lines = 3): void
{
// TODO validation on cut() inputs
$this -> connector -> write(self::GS . "V" . chr($mode) . chr($lines));
@@ -519,7 +522,7 @@ public function cut(int $mode = Printer::CUT_FULL, int $lines = 3)
*
* @param int $lines Number of lines to feed
*/
- public function feed(int $lines = 1)
+ public function feed(int $lines = 1): void
{
self::validateInteger($lines, 1, 255, __FUNCTION__);
if ($lines <= 1) {
@@ -533,7 +536,7 @@ public function feed(int $lines = 1)
* Some printers require a form feed to release the paper. On most printers, this
* command is only useful in page mode, which is not implemented in this driver.
*/
- public function feedForm()
+ public function feedForm(): void
{
$this -> connector -> write(self::FF);
}
@@ -541,7 +544,7 @@ public function feedForm()
/**
* Some slip printers require `ESC q` sequence to release the paper.
*/
- public function release()
+ public function release(): void
{
$this -> connector -> write(self::ESC . chr(113));
}
@@ -551,7 +554,7 @@ public function release()
*
* @param int $lines number of lines to feed. If not specified, 1 line will be fed.
*/
- public function feedReverse(int $lines = 1)
+ public function feedReverse(int $lines = 1): void
{
self::validateInteger($lines, 1, 255, __FUNCTION__);
$this -> connector -> write(self::ESC . "e" . chr($lines));
@@ -560,7 +563,7 @@ public function feedReverse(int $lines = 1)
/**
* @return int
*/
- public function getCharacterTable()
+ public function getCharacterTable(): int
{
return $this -> characterTable;
}
@@ -568,7 +571,7 @@ public function getCharacterTable()
/**
* @return PrintBuffer
*/
- public function getPrintBuffer()
+ public function getPrintBuffer(): ?PrintBuffer
{
return $this -> buffer;
}
@@ -576,7 +579,7 @@ public function getPrintBuffer()
/**
* @return PrintConnector
*/
- public function getPrintConnector()
+ public function getPrintConnector(): PrintConnector
{
return $this -> connector;
}
@@ -584,7 +587,7 @@ public function getPrintConnector()
/**
* @return CapabilityProfile
*/
- public function getPrinterCapabilityProfile()
+ public function getPrinterCapabilityProfile(): CapabilityProfile
{
return $this -> profile;
}
@@ -608,7 +611,7 @@ public function getPrinterCapabilityProfile()
* (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and
* `Printer::IMG_DOUBLE_WIDTH` flags.
*/
- public function graphics(EscposImage $img, int $size = Printer::IMG_DEFAULT)
+ public function graphics(EscposImage $img, int $size = Printer::IMG_DEFAULT): void
{
self::validateInteger($size, 0, 3, __FUNCTION__);
$rasterData = $img -> toRasterFormat();
@@ -625,7 +628,7 @@ public function graphics(EscposImage $img, int $size = Printer::IMG_DEFAULT)
/**
* Initialize printer. This resets formatting back to the defaults.
*/
- public function initialize()
+ public function initialize(): void
{
$this -> connector -> write(self::ESC . "@");
$this -> characterTable = 0;
@@ -647,7 +650,7 @@ public function initialize()
* start/end bars, or truncated code Printer::PDF417_TRUNCATED with start bars only.
* @throws Exception If this profile indicates that PDF417 code is not supported
*/
- public function pdf417Code(string $content, int $width = 3, int $heightMultiplier = 3, int $dataColumnCount = 0, float $ec = 0.10, int $options = Printer::PDF417_STANDARD)
+ public function pdf417Code(string $content, int $width = 3, int $heightMultiplier = 3, int $dataColumnCount = 0, float $ec = 0.10, int $options = Printer::PDF417_STANDARD): void
{
self::validateInteger($width, 2, 8, __FUNCTION__, 'width');
self::validateInteger($heightMultiplier, 2, 8, __FUNCTION__, 'heightMultiplier');
@@ -685,7 +688,7 @@ public function pdf417Code(string $content, int $width = 3, int $heightMultiplie
* @param int $on_ms pulse ON time, in milliseconds.
* @param int $off_ms pulse OFF time, in milliseconds.
*/
- public function pulse(int $pin = 0, int $on_ms = 120, int $off_ms = 240)
+ public function pulse(int $pin = 0, int $on_ms = 120, int $off_ms = 240): void
{
self::validateInteger($pin, 0, 1, __FUNCTION__);
self::validateInteger($on_ms, 1, 511, __FUNCTION__);
@@ -704,7 +707,7 @@ public function pulse(int $pin = 0, int $on_ms = 120, int $off_ms = 240)
* @param int $size Pixel size to use. Must be 1-16 (default 3)
* @param int $model QR code model to use. Must be one of Printer::QR_MODEL_1, Printer::QR_MODEL_2 (default) or Printer::QR_MICRO (not supported by all printers).
*/
- public function qrCode(string $content, int $ec = Printer::QR_ECLEVEL_L, int$size = 3, int $model = Printer::QR_MODEL_2)
+ public function qrCode(string $content, int $ec = Printer::QR_ECLEVEL_L, int$size = 3, int $model = Printer::QR_MODEL_2): void
{
self::validateInteger($ec, 0, 3, __FUNCTION__);
self::validateInteger($size, 1, 16, __FUNCTION__);
@@ -734,7 +737,7 @@ public function qrCode(string $content, int $ec = Printer::QR_ECLEVEL_L, int$siz
*
* @param int $table The table to select. Available code tables are model-specific.
*/
- public function selectCharacterTable(int $table = 0)
+ public function selectCharacterTable(int $table = 0): void
{
self::validateInteger($table, 0, 255, __FUNCTION__);
$supported = $this -> profile -> getCodePages();
@@ -763,7 +766,7 @@ public function selectCharacterTable(int $table = 0)
*
* @param int $mode The mode to use. Default is Printer::MODE_FONT_A, with no special formatting. This has a similar effect to running initialize().
*/
- public function selectPrintMode(int $mode = Printer::MODE_FONT_A)
+ public function selectPrintMode(int $mode = Printer::MODE_FONT_A): void
{
$allModes = Printer::MODE_FONT_B | self::MODE_EMPHASIZED | self::MODE_DOUBLE_HEIGHT | self::MODE_DOUBLE_WIDTH | self::MODE_UNDERLINE;
if (!is_integer($mode) || $mode < 0 || ($mode & $allModes) != $mode) {
@@ -778,7 +781,7 @@ public function selectPrintMode(int $mode = Printer::MODE_FONT_A)
*
* @param bool $on True to enable user-defined character set, false to use built-in characters sets.
*/
- public function selectUserDefinedCharacterSet($on = true)
+ public function selectUserDefinedCharacterSet($on = true): void
{
$this -> connector -> write(self::ESC . "%". ($on ? chr(1) : chr(0)));
}
@@ -788,7 +791,7 @@ public function selectUserDefinedCharacterSet($on = true)
*
* @param int $height Height in dots. If not specified, 8 will be used.
*/
- public function setBarcodeHeight(int $height = 8)
+ public function setBarcodeHeight(int $height = 8): void
{
self::validateInteger($height, 1, 255, __FUNCTION__);
$this -> connector -> write(self::GS . "h" . chr($height));
@@ -800,7 +803,7 @@ public function setBarcodeHeight(int $height = 8)
* @param int $width Bar width in dots. If not specified, 3 will be used.
* Values above 6 appear to have no effect.
*/
- public function setBarcodeWidth(int $width = 3)
+ public function setBarcodeWidth(int $width = 3): void
{
self::validateInteger($width, 1, 255, __FUNCTION__);
$this -> connector -> write(self::GS . "w" . chr($width));
@@ -813,7 +816,7 @@ public function setBarcodeWidth(int $width = 3)
* or any combination of Printer::BARCODE_TEXT_ABOVE and Printer::BARCODE_TEXT_BELOW
* flags to display the text.
*/
- public function setBarcodeTextPosition(int $position = Printer::BARCODE_TEXT_NONE)
+ public function setBarcodeTextPosition(int $position = Printer::BARCODE_TEXT_NONE): void
{
self::validateInteger($position, 0, 3, __FUNCTION__, "Barcode text position");
$this -> connector -> write(self::GS . "H" . chr($position));
@@ -824,7 +827,7 @@ public function setBarcodeTextPosition(int $position = Printer::BARCODE_TEXT_NON
*
* @param boolean $on true for double strike, false for no double strike
*/
- public function setDoubleStrike(bool $on = true)
+ public function setDoubleStrike(bool $on = true): void
{
self::validateBoolean($on, __FUNCTION__);
$this -> connector -> write(self::ESC . "G". ($on ? chr(1) : chr(0)));
@@ -835,7 +838,7 @@ public function setDoubleStrike(bool $on = true)
*
* @param int $color Color to use. Must be either Printer::COLOR_1 (default), or Printer::COLOR_2.
*/
- public function setColor(int $color = Printer::COLOR_1)
+ public function setColor(int $color = Printer::COLOR_1): void
{
self::validateInteger($color, 0, 1, __FUNCTION__, "Color");
$this -> connector -> write(self::ESC . "r" . chr($color));
@@ -846,7 +849,7 @@ public function setColor(int $color = Printer::COLOR_1)
*
* @param boolean $on true for emphasis, false for no emphasis
*/
- public function setEmphasis(bool $on = true)
+ public function setEmphasis(bool $on = true): void
{
self::validateBoolean($on, __FUNCTION__);
$this -> connector -> write(self::ESC . "E". ($on ? chr(1) : chr(0)));
@@ -857,7 +860,7 @@ public function setEmphasis(bool $on = true)
*
* @param int $font The font to use. Must be either Printer::FONT_A, Printer::FONT_B, or Printer::FONT_C.
*/
- public function setFont(int $font = Printer::FONT_A)
+ public function setFont(int $font = Printer::FONT_A): void
{
self::validateInteger($font, 0, 2, __FUNCTION__);
$this -> connector -> write(self::ESC . "M" . chr($font));
@@ -868,7 +871,7 @@ public function setFont(int $font = Printer::FONT_A)
*
* @param int $justification One of Printer::JUSTIFY_LEFT, Printer::JUSTIFY_CENTER, or Printer::JUSTIFY_RIGHT.
*/
- public function setJustification(int $justification = Printer::JUSTIFY_LEFT)
+ public function setJustification(int $justification = Printer::JUSTIFY_LEFT): void
{
self::validateInteger($justification, 0, 2, __FUNCTION__);
$this -> connector -> write(self::ESC . "a" . chr($justification));
@@ -882,7 +885,7 @@ public function setJustification(int $justification = Printer::JUSTIFY_LEFT)
* @param int|null $height The height of each line, in dots. If not set, the printer
* will reset to its default line spacing.
*/
- public function setLineSpacing(int $height = null)
+ public function setLineSpacing(?int $height = null): void
{
if ($height === null) {
// Reset to default
@@ -898,7 +901,7 @@ public function setLineSpacing(int $height = null)
*
* @param int $margin The left margin to set on to the print area, in dots.
*/
- public function setPrintLeftMargin(int $margin = 0)
+ public function setPrintLeftMargin(int $margin = 0): void
{
self::validateInteger($margin, 0, 65535, __FUNCTION__);
$this -> connector -> write(Printer::GS . 'L' . self::intLowHigh($margin, 2));
@@ -910,7 +913,7 @@ public function setPrintLeftMargin(int $margin = 0)
*
* @param int $width The width of the page print area, in dots.
*/
- public function setPrintWidth(int $width = 512)
+ public function setPrintWidth(int $width = 512): void
{
self::validateInteger($width, 1, 65535, __FUNCTION__);
$this -> connector -> write(Printer::GS . 'W' . self::intLowHigh($width, 2));
@@ -922,7 +925,7 @@ public function setPrintWidth(int $width = 512)
* @param PrintBuffer $buffer The buffer to use.
* @throws InvalidArgumentException Where the buffer is already attached to a different printer.
*/
- public function setPrintBuffer(PrintBuffer $buffer)
+ public function setPrintBuffer(PrintBuffer $buffer): void
{
if ($buffer === $this -> buffer) {
return;
@@ -942,7 +945,7 @@ public function setPrintBuffer(PrintBuffer $buffer)
*
* @param boolean $on True to enable, false to disable.
*/
- public function setReverseColors(bool $on = true)
+ public function setReverseColors(bool $on = true): void
{
self::validateBoolean($on, __FUNCTION__);
$this -> connector -> write(self::GS . "B" . ($on ? chr(1) : chr(0)));
@@ -954,7 +957,7 @@ public function setReverseColors(bool $on = true)
* @param int $widthMultiplier Multiple of the regular height to use (range 1 - 8)
* @param int $heightMultiplier Multiple of the regular height to use (range 1 - 8)
*/
- public function setTextSize(int $widthMultiplier, int $heightMultiplier)
+ public function setTextSize(int $widthMultiplier, int $heightMultiplier): void
{
self::validateInteger($widthMultiplier, 1, 8, __FUNCTION__);
self::validateInteger($heightMultiplier, 1, 8, __FUNCTION__);
@@ -967,7 +970,7 @@ public function setTextSize(int $widthMultiplier, int $heightMultiplier)
*
* @param int $underline Either true/false, or one of Printer::UNDERLINE_NONE, Printer::UNDERLINE_SINGLE or Printer::UNDERLINE_DOUBLE. Defaults to Printer::UNDERLINE_SINGLE.
*/
- public function setUnderline(int $underline = Printer::UNDERLINE_SINGLE)
+ public function setUnderline(int $underline = Printer::UNDERLINE_SINGLE): void
{
/* Set the underline */
self::validateInteger($underline, 0, 2, __FUNCTION__);
@@ -979,7 +982,7 @@ public function setUnderline(int $underline = Printer::UNDERLINE_SINGLE)
*
* @param boolean $on True to enable, false to disable.
*/
- public function setUpsideDown(bool $on = true)
+ public function setUpsideDown(bool $on = true): void
{
$this -> connector -> write(self::ESC . "{" . ($on ? chr(1) : chr(0)));
}
@@ -992,9 +995,9 @@ public function setUpsideDown(bool $on = true)
*
* @param string $str Text to print, as UTF-8
*/
- public function text(string $str)
+ public function text(string $str): void
{
- $this -> buffer -> writeText((string)$str);
+ $this -> buffer -> writeText($str);
}
/**
@@ -1005,7 +1008,7 @@ public function text(string $str)
*
* @param string $str Text to print, as UTF-8
*/
- public function textChinese(string $str = "")
+ public function textChinese(string $str = ""): void
{
$this -> connector -> write(self::FS . "&");
$str = \UConverter::transcode($str, "GBK", "UTF-8");
@@ -1021,9 +1024,9 @@ public function textChinese(string $str = "")
*
* @param string $str Text to print
*/
- public function textRaw(string $str = "")
+ public function textRaw(string $str = ""): void
{
- $this -> buffer -> writeTextRaw((string)$str);
+ $this -> buffer -> writeTextRaw($str);
}
/**
@@ -1035,7 +1038,7 @@ public function textRaw(string $str = "")
* @param string $m Modifier/variant for function. Often '0' where used.
* @throws InvalidArgumentException Where the input lengths are bad.
*/
- protected function wrapperSend2dCodeData(string $fn, string $cn, string$data = '', string $m = '')
+ protected function wrapperSend2dCodeData(string $fn, string $cn, string$data = '', string $m = ''): void
{
if (strlen($m) > 1 || strlen($cn) != 1 || strlen($fn) != 1) {
throw new InvalidArgumentException("wrapperSend2dCodeData: cn and fn must be one character each.");
@@ -1052,7 +1055,7 @@ protected function wrapperSend2dCodeData(string $fn, string $cn, string$data = '
* @param string $data Data to send.
* @throws InvalidArgumentException Where the input lengths are bad.
*/
- protected function wrapperSendGraphicsData(string $m, string $fn, string$data = '')
+ protected function wrapperSendGraphicsData(string $m, string $fn, string$data = ''): void
{
if (strlen($m) != 1 || strlen($fn) != 1) {
throw new InvalidArgumentException("wrapperSendGraphicsData: m and fn must be one character each.");
@@ -1068,7 +1071,7 @@ protected function wrapperSendGraphicsData(string $m, string $fn, string$data =
* @param boolean $long True to use 4 bytes, false to use 2
* @return string
*/
- protected static function dataHeader(array $inputs, bool $long = true)
+ protected static function dataHeader(array $inputs, bool $long = true): string
{
$outp = [];
foreach ($inputs as $input) {
@@ -1088,7 +1091,7 @@ protected static function dataHeader(array $inputs, bool $long = true)
* @param int $input Input number
* @param int $length The number of bytes to output (1 - 4).
*/
- protected static function intLowHigh(int $input, int $length)
+ protected static function intLowHigh(int $input, int $length): string
{
$maxInput = (256 << ($length * 8) - 1);
self::validateInteger($length, 1, 4, __FUNCTION__);
@@ -1107,7 +1110,7 @@ protected static function intLowHigh(int $input, int $length)
* @param boolean $test the input to test
* @param string $source the name of the function calling this
*/
- protected static function validateBoolean(bool $test, string $source)
+ protected static function validateBoolean(bool $test, string $source): void
{
if (!($test === true || $test === false)) {
throw new InvalidArgumentException("Argument to $source must be a boolean");
@@ -1123,7 +1126,7 @@ protected static function validateBoolean(bool $test, string $source)
* @param string $source the name of the function calling this
* @param string $argument the name of the invalid parameter
*/
- protected static function validateFloat(float $test, float $min, float $max, string $source, string $argument = "Argument")
+ protected static function validateFloat(float $test, float $min, float $max, string $source, string $argument = "Argument"): void
{
if (!is_numeric($test)) {
throw new InvalidArgumentException("$argument given to $source must be a float, but '$test' was given.");
@@ -1142,7 +1145,7 @@ protected static function validateFloat(float $test, float $min, float $max, str
* @param string $source the name of the function calling this
* @param string $argument the name of the invalid parameter
*/
- protected static function validateInteger(int $test, int $min, int $max, string $source, string $argument = "Argument")
+ protected static function validateInteger(int $test, int $min, int $max, string $source, string $argument = "Argument"): void
{
self::validateIntegerMulti($test, [[$min, $max]], $source, $argument);
}
@@ -1153,10 +1156,9 @@ protected static function validateInteger(int $test, int $min, int $max, string
* @param int $test the input to test
* @param array $ranges array of two-item min/max ranges.
* @param string $source the name of the function calling this
- * @param string $source the name of the function calling this
* @param string $argument the name of the invalid parameter
*/
- protected static function validateIntegerMulti(int $test, array $ranges, string $source, string $argument = "Argument")
+ protected static function validateIntegerMulti(int $test, array $ranges, string $source, string $argument = "Argument"): void
{
if (!is_integer($test)) {
throw new InvalidArgumentException("$argument given to $source must be a number, but '$test' was given.");
@@ -1191,7 +1193,7 @@ protected static function validateIntegerMulti(int $test, array $ranges, string
* @param string $argument the name of the parameter being validated
* @throws InvalidArgumentException Where the argument is not valid
*/
- protected static function validateStringRegex(string $test, string $source, string $regex, string $argument = "Argument")
+ protected static function validateStringRegex(string $test, string $source, string $regex, string $argument = "Argument"): void
{
if (preg_match($regex, $test) === 0) {
throw new InvalidArgumentException("$argument given to $source is invalid. It should match regex '$regex', but '$test' was given.");
diff --git a/test/integration/ExampleTest.php b/test/integration/ExampleTest.php
index a19d64a1..0d751edf 100644
--- a/test/integration/ExampleTest.php
+++ b/test/integration/ExampleTest.php
@@ -1,6 +1,8 @@
exampleDir = dirname(__FILE__) . "/../../example/";
}
- /**
- * @medium
- */
public function testBitImage()
{
$this->markTestSkipped('Not repeatable on Travis CI.');
@@ -22,18 +21,12 @@ public function testBitImage()
$this -> outpTest($outp, "bit-image.bin");
}
- /**
- * @medium
- */
public function testCharacterEncodings()
{
$outp = $this -> runExample("character-encodings.php");
$this -> outpTest($outp, "character-encodings.bin");
}
- /**
- * @medium
- */
public function testCharacterTables()
{
$outp = $this -> runExample("character-tables.php");
@@ -49,9 +42,6 @@ private function outpTest($outp, $fn)
$this -> assertEquals($outp, file_get_contents($file));
}
- /**
- * @medium
- */
public function testDemo()
{
$this->markTestSkipped('Not repeatable on Travis CI.');
@@ -60,9 +50,6 @@ public function testDemo()
$this -> outpTest($outp, "demo.bin");
}
- /**
- * @medium
- */
public function testGraphics()
{
$this->markTestSkipped('Not repeatable on Travis CI.');
@@ -71,9 +58,6 @@ public function testGraphics()
$this -> outpTest($outp, "graphics.bin");
}
- /**
- * @medium
- */
public function testReceiptWithLogo()
{
$this->markTestSkipped('Not repeatable on Travis CI.');
@@ -82,54 +66,36 @@ public function testReceiptWithLogo()
$this -> outpTest($outp, "receipt-with-logo.bin");
}
- /**
- * @medium
- */
public function testQrCode()
{
$outp = $this -> runExample("qr-code.php");
$this -> outpTest($outp, "qr-code.bin");
}
- /**
- * @medium
- */
public function testBarcode()
{
$outp = $this -> runExample("barcode.php");
$this -> outpTest($outp, "barcode.bin");
}
- /**
- * @medium
- */
public function testTextSize()
{
$outp = $this -> runExample("text-size.php");
$this -> outpTest($outp, "text-size.bin");
}
- /**
- * @medium
- */
public function testMarginsAndSpacing()
{
$outp = $this -> runExample("margins-and-spacing.php");
$this -> outpTest($outp, "margins-and-spacing.bin");
}
- /**
- * @medium
- */
public function testPdf417Code()
{
$outp = $this -> runExample("pdf417-code.php");
$this -> outpTest($outp, "pdf417-code.bin");
}
- /**
- * @medium
- */
public function testUnifontPrintBuffer()
{
$this->markTestSkipped('Not repeatable on Travis CI.');
diff --git a/test/unit/AuresCustomerDisplayTest.php b/test/unit/AuresCustomerDisplayTest.php
index 394994c9..a73ef03e 100644
--- a/test/unit/AuresCustomerDisplayTest.php
+++ b/test/unit/AuresCustomerDisplayTest.php
@@ -1,7 +1,7 @@
outputConnector = new DummyPrintConnector();
+ $this -> outputConnector = new MemoryPrintConnector();
$profile = CapabilityProfile::load('OCD-300');
$this -> printer = new AuresCustomerDisplay($this -> outputConnector, $profile);
}
diff --git a/test/unit/CupsPrintConnectorTest.php b/test/unit/CupsPrintConnectorTest.php
index dec680b9..1a46d3c7 100644
--- a/test/unit/CupsPrintConnectorTest.php
+++ b/test/unit/CupsPrintConnectorTest.php
@@ -24,7 +24,7 @@ public function testNoPrinter()
}
private function getMockConnector($path, array $printers)
{
- $stub = $this->getMockBuilder('Mike42\Escpos\PrintConnectors\CupsPrintConnector')->setMethods(array (
+ $stub = $this->getMockBuilder('Mike42\Escpos\PrintConnectors\CupsPrintConnector')->onlyMethods(array (
'getCmdOutput',
'getLocalPrinters'
))->disableOriginalConstructor()->getMock();
diff --git a/test/unit/EscposPrintBufferTest.php b/test/unit/EscposPrintBufferTest.php
index 90ffd037..e6ca1f23 100644
--- a/test/unit/EscposPrintBufferTest.php
+++ b/test/unit/EscposPrintBufferTest.php
@@ -10,7 +10,7 @@
* - http://clagnut.com/blog/2380/ (mirrored from the English Wikipedia)
*/
use Mike42\Escpos\Printer;
-use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
+use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
class EscposPrintBufferTest extends PHPUnit\Framework\TestCase
{
@@ -19,7 +19,7 @@ class EscposPrintBufferTest extends PHPUnit\Framework\TestCase
protected function setUp(): void
{
- $this -> outputConnector = new DummyPrintConnector();
+ $this -> outputConnector = new MemoryPrintConnector();
$printer = new Printer($this -> outputConnector);
$this -> buffer = $printer -> getPrintBuffer();
}
diff --git a/test/unit/EscposTest.php b/test/unit/EscposTest.php
index 9bb4fd20..e03c4f4c 100644
--- a/test/unit/EscposTest.php
+++ b/test/unit/EscposTest.php
@@ -1,6 +1,6 @@
outputConnector = new DummyPrintConnector();
+ $this -> outputConnector = new MemoryPrintConnector();
$this -> printer = new Printer($this -> outputConnector);
}
diff --git a/test/unit/Experimental/Unifont/UnifontPrintBufferTest.php b/test/unit/Experimental/Unifont/UnifontPrintBufferTest.php
index 3710bd54..6631aec5 100644
--- a/test/unit/Experimental/Unifont/UnifontPrintBufferTest.php
+++ b/test/unit/Experimental/Unifont/UnifontPrintBufferTest.php
@@ -2,7 +2,7 @@
namespace Mike42\Escpos\Experimental\Unifont;
-use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
+use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
use Mike42\Escpos\Printer;
use PHPUnit\Framework\TestCase;
@@ -13,7 +13,7 @@ class UnifontPrintBufferTest extends TestCase
protected function setUp(): void
{
- $this -> outputConnector = new DummyPrintConnector();
+ $this -> outputConnector = new MemoryPrintConnector();
$this -> printer = new Printer($this -> outputConnector);
$filename = tempnam(sys_get_temp_dir(), "escpos-php-");
$glyphs = [
diff --git a/test/unit/GdEscposImageTest.php b/test/unit/GdEscposImageTest.php
index d481d7a6..9ca75436 100644
--- a/test/unit/GdEscposImageTest.php
+++ b/test/unit/GdEscposImageTest.php
@@ -1,7 +1,9 @@
loadAndCheckImg('not a real file.png', 1, 1, null, null);
}
- /**
- * @medium
- */
public function testGdEmpty()
{
$this -> loadAndCheckImg(null, 0, 0, "", array());
}
- /**
- * @medium
- */
public function testGdBlack()
{
foreach (array('png', 'jpg', 'gif') as $format) {
@@ -33,9 +29,6 @@ public function testGdBlack()
}
}
- /**
- * @medium
- */
public function testGdBlackTransparent()
{
foreach (array('png', 'gif') as $format) {
@@ -43,9 +36,6 @@ public function testGdBlackTransparent()
}
}
- /**
- * @medium
- */
public function testGdBlackWhite()
{
foreach (array('png', 'jpg', 'gif') as $format) {
@@ -53,9 +43,6 @@ public function testGdBlackWhite()
}
}
- /**
- * @medium
- */
public function testGdWhite()
{
foreach (array('png', 'jpg', 'gif') as $format) {
diff --git a/test/unit/ImagickEscposImageTest.php b/test/unit/ImagickEscposImageTest.php
index 4eed75a8..33a7e581 100644
--- a/test/unit/ImagickEscposImageTest.php
+++ b/test/unit/ImagickEscposImageTest.php
@@ -1,7 +1,9 @@
loadAndCheckImg('not a real file.png', 1, 1, null, null);
}
- /**
- * @medium
- */
public function testImagickEmpty()
{
$this -> loadAndCheckImg(null, 0, 0, "", array());
}
- /**
- * @medium
- */
public function testImagickBlack()
{
foreach (array('png', 'jpg', 'gif') as $format) {
@@ -33,9 +29,6 @@ public function testImagickBlack()
}
}
- /**
- * @medium
- */
public function testImagickBlackTransparent()
{
foreach (array('png', 'gif') as $format) {
@@ -43,9 +36,6 @@ public function testImagickBlackTransparent()
}
}
- /**
- * @medium
- */
public function testImagickBlackWhite()
{
foreach (array('png', 'jpg', 'gif') as $format) {
@@ -53,9 +43,6 @@ public function testImagickBlackWhite()
}
}
- /**
- * @medium
- */
public function testImagickBlackWhiteTall()
{
// We're very interested in correct column format chopping here at 8 pixels
@@ -63,9 +50,6 @@ public function testImagickBlackWhiteTall()
"\xc0\xc0\xc0\xc0\xc0\xc0\xc0\xc0\x00\x00\x00\x00\x00\x00\x00\x00", array("\xff\xff", "\x00\x00"));
}
- /**
- * @medium
- */
public function testImagickWhite()
{
foreach (array('png', 'jpg', 'gif') as $format) {
@@ -76,7 +60,6 @@ public function testImagickWhite()
/**
* PDF test - load tiny PDF and check for well-formedness
* These are also skipped if you don't have imagick
- * @medium
*/
public function testPdfAllPages()
{
@@ -115,7 +98,7 @@ private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $co
/**
* Same as above, loading document and checking pages against some expected values.
*/
- private function loadAndCheckPdf($fn, $width, $height, array $rasterFormat = null, array $columnFormat = null)
+ private function loadAndCheckPdf($fn, $width, $height, ?array $rasterFormat = null, ?array $columnFormat = null)
{
if (!EscposImage::isImagickLoaded()) {
$this -> markTestSkipped("imagick plugin required for this test");
diff --git a/test/unit/MultiplePrintConnectorTest.php b/test/unit/MultiplePrintConnectorTest.php
index 0508c97a..79fba37e 100644
--- a/test/unit/MultiplePrintConnectorTest.php
+++ b/test/unit/MultiplePrintConnectorTest.php
@@ -1,6 +1,6 @@
loadAndCheckImg('not a real file.png', 1, 1, null, null);
}
- /**
- * @medium
- */
+
public function testBlack()
{
foreach (array('bmp', 'gif', 'png') as $format) {
@@ -24,9 +24,6 @@ public function testBlack()
}
}
- /**
- * @medium
- */
public function testBlackTransparent()
{
foreach (array('gif', 'png') as $format) {
@@ -34,9 +31,6 @@ public function testBlackTransparent()
}
}
- /**
- * @medium
- */
public function testBlackWhite()
{
foreach (array('bmp', 'png', 'gif') as $format) {
@@ -44,9 +38,6 @@ public function testBlackWhite()
}
}
- /**
- * @medium
- */
public function testBlackWhiteTall()
{
// We're very interested in correct column format chopping here at 8 pixels
@@ -54,9 +45,6 @@ public function testBlackWhiteTall()
"\xc0\xc0\xc0\xc0\xc0\xc0\xc0\xc0\x00\x00\x00\x00\x00\x00\x00\x00", array("\xff\xff", "\x00\x00"));
}
- /**
- * @medium
- */
public function testWhite()
{
foreach (array('bmp', 'png', 'gif') as $format) {
diff --git a/test/unit/UriPrintConnectorTest.php b/test/unit/UriPrintConnectorTest.php
index 03fc9c31..846f680c 100644
--- a/test/unit/UriPrintConnectorTest.php
+++ b/test/unit/UriPrintConnectorTest.php
@@ -1,6 +1,6 @@
expectNotice();
- $this->expectNoticeMessage("not finalized");
+ // An smb:// URI should be accepted and resolve to a WindowsPrintConnector.
$connector = UriPrintConnector::get("smb://windows/printer");
- $this -> assertEquals('Mike42\Escpos\PrintConnectors\WindowsPrintConnector', get_class($connector));
- // We expect that this will throw an exception, we can't
- // realistically print to a real printer in this test though... :)
- $connector -> __destruct();
+ $this -> assertInstanceOf(WindowsPrintConnector::class, $connector);
+ // Hack: swallow the "not finalized" notice from the destructor, since we
+ // never finalize() (can't print to a real SMB share from a unit test).
+ set_error_handler(function () {
+ return true;
+ }, E_USER_NOTICE);
+ unset($connector);
+ restore_error_handler();
}
public function testBadUri()
diff --git a/test/unit/WindowsPrintConnectorTest.php b/test/unit/WindowsPrintConnectorTest.php
index a2370ddb..faaf7100 100644
--- a/test/unit/WindowsPrintConnectorTest.php
+++ b/test/unit/WindowsPrintConnectorTest.php
@@ -60,7 +60,7 @@ public function testComWindows()
-> method('runCopy');
$connector -> finalize();
}
-
+
public function testComMac()
{
// Cannot print to local printer on Mac with this connector
@@ -74,7 +74,7 @@ public function testComMac()
-> method('runCopy');
$connector -> finalize();
}
-
+
public function testComLinux()
{
// Cannot print to local printer on Linux with this connector
@@ -183,7 +183,7 @@ public function testSharedPrinterLinux()
-> method('runWrite');
$connector -> finalize();
}
-
+
public function testSharedPrinterLinuxUsername()
{
$connector = $this -> getMockConnector("smb://bob@example-pc/Printer", WindowsPrintConnector::PLATFORM_LINUX);
@@ -196,7 +196,7 @@ public function testSharedPrinterLinuxUsername()
-> method('runWrite');
$connector -> finalize();
}
-
+
public function testSharedPrinterLinuxUsernameDomain()
{
$connector = $this -> getMockConnector("smb://bob@example-pc/home/Printer", WindowsPrintConnector::PLATFORM_LINUX);
@@ -209,7 +209,7 @@ public function testSharedPrinterLinuxUsernameDomain()
-> method('runWrite');
$connector -> finalize();
}
-
+
public function testSharedPrinterLinuxUsernamePassword()
{
$connector = $this -> getMockConnector("smb://bob:secret@example-pc/Printer", WindowsPrintConnector::PLATFORM_LINUX);
@@ -226,7 +226,7 @@ public function testSharedPrinterLinuxUsernamePassword()
private function getMockConnector($path, $platform)
{
$stub = $this -> getMockBuilder('Mike42\Escpos\PrintConnectors\WindowsPrintConnector')
- -> setMethods(array('runCopy', 'runCommand', 'getCurrentPlatform', 'runWrite'))
+ -> onlyMethods(array('runCopy', 'runCommand', 'getCurrentPlatform', 'runWrite'))
-> disableOriginalConstructor()
-> getMock();
$stub -> method('runCommand')