From 96128112ca436237bd76ee372e6f274da6219be2 Mon Sep 17 00:00:00 2001
From: Aranya Sen
Date: Sat, 31 Dec 2022 17:42:17 +0530
Subject: [PATCH] Upgrade minimum php version to 8.0 (#90)
* #89 Update minimum php version to 8.0
* Code refactor and cleanup
* Declare strict
* Remove redundant phpdocs
---
.github/workflows/main_ci.yml | 2 +-
README.md | 6 +-
composer.json | 2 +-
src/HL7.php | 31 ++++------
src/HL7/Connection.php | 13 +---
src/HL7/Message.php | 78 ++++++++----------------
src/HL7/MessageHelpersTrait.php | 44 ++------------
src/HL7/Messages/ACK.php | 6 +-
src/HL7/Segment.php | 10 +---
src/HL7/Segments/AIG.php | 3 +-
src/HL7/Segments/AIL.php | 3 +-
src/HL7/Segments/AIP.php | 3 +-
src/HL7/Segments/DG1.php | 6 +-
src/HL7/Segments/EQU.php | 3 +-
src/HL7/Segments/FHS.php | 2 +
src/HL7/Segments/FTS.php | 2 +
src/HL7/Segments/GT1.php | 4 +-
src/HL7/Segments/IN1.php | 3 +-
src/HL7/Segments/IN3.php | 3 +-
src/HL7/Segments/MRG.php | 2 +
src/HL7/Segments/MSA.php | 2 +
src/HL7/Segments/MSH.php | 11 +---
src/HL7/Segments/NK1.php | 1 -
src/HL7/Segments/NTE.php | 3 +-
src/HL7/Segments/OBR.php | 5 +-
src/HL7/Segments/OBX.php | 6 +-
src/HL7/Segments/ORC.php | 2 +
src/HL7/Segments/PID.php | 9 +--
src/HL7/Segments/PV1.php | 101 +-------------------------------
src/HL7/Segments/PV2.php | 96 +-----------------------------
src/HL7/Segments/RGS.php | 6 +-
src/HL7/Segments/SAC.php | 5 +-
src/HL7/Segments/SCH.php | 2 +
src/HL7/Segments/TQ1.php | 2 +
tests/Segments/OBXTest.php | 22 +++++++
tests/Segments/PIDTest.php | 2 +
36 files changed, 132 insertions(+), 369 deletions(-)
create mode 100644 tests/Segments/OBXTest.php
diff --git a/.github/workflows/main_ci.yml b/.github/workflows/main_ci.yml
index ed46e8a..da91338 100644
--- a/.github/workflows/main_ci.yml
+++ b/.github/workflows/main_ci.yml
@@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
- php-versions: [ '7.3', '7.4', '8.0', '8.1' ]
+ php-versions: [ '8.0', '8.1', '8.2' ]
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
steps:
- name: Checkout
diff --git a/README.md b/README.md
index 81a05f3..d54a765 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,11 @@
-**Important: Supported PHP version has been updated to 7.3+. Last supported versions: for PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2), for PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)**
+**Important: Minimum supported PHP version has been updated to 8.0
+Last supported versions:
+-> PHP 7.0 or 7.1 => [1.5.4](https://github.com/senaranya/HL7/tree/1.5.4)
+-> PHP 7.2 => [2.0.2](https://github.com/senaranya/HL7/tree/2.0.2)
+-> PHP 7.4 => [2.1.7](https://github.com/senaranya/HL7/tree/2.1.7)**
## Introduction
diff --git a/composer.json b/composer.json
index d45c475..d3719f1 100644
--- a/composer.json
+++ b/composer.json
@@ -14,7 +14,7 @@
"minimum-stability": "dev",
"prefer-stable" : true,
"require": {
- "php": ">=7.3",
+ "php": "^8.0",
"ext-mbstring": "*"
},
"require-dev": {
diff --git a/src/HL7.php b/src/HL7.php
index 5e18277..559d4a6 100644
--- a/src/HL7.php
+++ b/src/HL7.php
@@ -1,5 +1,7 @@
throwSocketError("Failed to connect to server ($host:$port)");
}
if (!$result) {
@@ -107,7 +105,6 @@ protected function setSocket(string $host, int $port, int $timeout = 10): void
}
/**
- * @param string $message
* @throws HL7ConnectionException
*/
protected function throwSocketError(string $message): void
@@ -118,14 +115,10 @@ protected function throwSocketError(string $message): void
/**
* Sends a Message object over this connection.
*
- * @param Message $msg
- * @param string $responseCharEncoding The expected character encoding of the response.
- * @param bool $noWait Do no wait for ACK. Helpful for building load testing tools...
- * @return Message|null
+ * @param string $responseCharEncoding The expected character encoding of the response.
+ * @param bool $noWait Do no wait for ACK. Helpful for building load testing tools...
* @throws HL7ConnectionException
* @throws HL7Exception
- * @throws ReflectionException
- * @access public
*/
public function send(Message $msg, string $responseCharEncoding = 'UTF-8', bool $noWait = false): ?Message
{
diff --git a/src/HL7/Message.php b/src/HL7/Message.php
index 2b8ec70..90cc146 100644
--- a/src/HL7/Message.php
+++ b/src/HL7/Message.php
@@ -22,10 +22,9 @@ class Message
{
use MessageHelpersTrait;
- /**
- * Array holding all segments of this message.
- */
- protected $segments;
+
+ /** @var array Array holding all segments of this message */
+ protected array $segments = [];
/**
* local value for segment separator
@@ -76,9 +75,6 @@ public function __construct(
bool $autoIncrementIndices = true,
bool $doNotSplitRepetition = null
) {
- // Array holding the segments
- $this->segments = [];
-
// Control characters and other HL7 properties
$this->segmentSeparator = $hl7Globals['SEGMENT_SEPARATOR'] ?? '\n';
$this->segmentEndingBar = $hl7Globals['SEGMENT_ENDING_BAR'] ?? true; // '|' at end of each segment
@@ -109,7 +105,7 @@ public function __construct(
$segmentName = array_shift($fields);
foreach ($fields as $j => $field) {
- // Skip control field
+ // Skip control field (i.e. first field in MSH segment)
if ($index === 0 && $j === 0) {
continue;
}
@@ -117,24 +113,7 @@ public function __construct(
$fields[$j] = $this->extractComponentsFromField($field, $keepEmptySubFields);
}
- $segment = null;
-
- // If a class exists for the segment under segments/, (e.g., MSH)
- $className = "Aranyasen\\HL7\\Segments\\$segmentName";
- if (class_exists($className)) {
- if ($segmentName === 'MSH') {
- array_unshift($fields, $this->fieldSeparator); # First field for MSH is '|'
- $segment = new $className($fields);
- } else {
- $segment = new $className($fields, $autoIncrementIndices);
- }
- } else {
- $segment = new Segment($segmentName, $fields);
- }
-
- if (!$segment) {
- trigger_error('Segment not created', E_USER_WARNING);
- }
+ $segment = $this->getSegmentClass($segmentName, $fields, $autoIncrementIndices);
$this->addSegment($segment);
}
@@ -142,10 +121,6 @@ public function __construct(
/**
* Append a segment to the end of the message
- *
- * @param Segment $segment
- * @return bool
- * @access public
*/
public function addSegment(Segment $segment): bool
{
@@ -161,9 +136,8 @@ public function addSegment(Segment $segment): bool
/**
* Insert a segment.
*
- * @param Segment $segment
* @param null|int $index Index where segment is inserted
- * @throws \InvalidArgumentException
+ * @throws InvalidArgumentException
*/
public function insertSegment(Segment $segment, int $index = null): void
{
@@ -193,7 +167,6 @@ public function insertSegment(Segment $segment, int $index = null): void
* Note: Segment count within the message starts at 0.
*
* @param int $index Index where segment is inserted
- * @return Segment
*/
public function getSegmentByIndex(int $index): ?Segment
{
@@ -204,10 +177,6 @@ public function getSegmentByIndex(int $index): ?Segment
return $this->segments[$index];
}
- /**
- * @param Segment $segment
- * @return int|null
- */
public function getSegmentIndex(Segment $segment): ?int
{
foreach ($this->segments as $ii => $value) {
@@ -244,8 +213,6 @@ public function getSegmentsByName(string $name): array
* after this one will be moved one index up.
*
* @param int $index Index where segment is removed
- * @return boolean
- * @access public
*/
public function removeSegmentByIndex(int $index): bool
{
@@ -259,7 +226,6 @@ public function removeSegmentByIndex(int $index): bool
/**
* Remove given segment
*
- * @param string $segmentName
* @return int Count of segments removed
*/
public function removeSegmentsByName(string $segmentName): int
@@ -278,9 +244,7 @@ public function removeSegmentsByName(string $segmentName): int
* If index is out of range, or not provided, do nothing. Setting MSH on index 0 will re-validate field separator,
* control characters and hl7 version, based on MSH(1), MSH(2) and MSH(12).
*
- * @param Segment $segment
* @param int $index Index where segment is set
- * @return boolean
* @throws \InvalidArgumentException
*/
public function setSegment(Segment $segment, int $index): bool
@@ -300,10 +264,6 @@ public function setSegment(Segment $segment, int $index): bool
/**
* After change of MSH, reset control fields
- *
- * @param Segment $segment
- * @return bool
- * @access protected
*/
protected function resetCtrl(Segment $segment): bool
{
@@ -372,8 +332,6 @@ public function toString(bool $pretty = false)
/**
* Convert Segment object to string
- * @param Segment $seg
- * @return string
*/
public function segmentToString(Segment $seg): string
{
@@ -404,8 +362,6 @@ public function segmentToString(Segment $seg): string
/**
* Reset index attribute of each given segment, so when those are added the indices start from 1
- *
- * @return void
*/
public function resetSegmentIndices(): void
{
@@ -422,8 +378,6 @@ public function resetSegmentIndices(): void
}
/**
- * @param string $field
- * @param bool $keepEmptySubFields
* @return array|string
*/
private function extractComponentsFromField(string $field, bool $keepEmptySubFields)
@@ -432,7 +386,7 @@ private function extractComponentsFromField(string $field, bool $keepEmptySubFie
? 0
: PREG_SPLIT_NO_EMPTY;
- if ((strpos($field, $this->repetitionSeparator) !== false) && (! $this->doNotSplitRepetition)) {
+ if ((str_contains($field, $this->repetitionSeparator)) && (! $this->doNotSplitRepetition)) {
$components = preg_split("/\\" . $this->repetitionSeparator . '/', $field, -1, $pregFlags);
$fields = [];
foreach ($components as $index => $component) {
@@ -459,8 +413,6 @@ private function extractComponentsFromField(string $field, bool $keepEmptySubFie
/**
* Set various separators - segment, field etc.
*
- * @param string $msh
- * @return void
* @throws HL7Exception
*/
private function setSeparators(string $msh): void
@@ -485,4 +437,20 @@ private function setSeparators(string $msh): void
$this->escapeChar = $esc;
$this->repetitionSeparator = $repSep;
}
+
+ private function getSegmentClass(string $segmentName, array $fields, bool $autoIncrementIndices): Segment
+ {
+ // If a class exists for the segment under segments/, (e.g., MSH)
+ $className = "Aranyasen\\HL7\\Segments\\$segmentName";
+ if (!class_exists($className)) {
+ return new Segment($segmentName, $fields);
+ }
+
+ if ($segmentName === 'MSH') {
+ array_unshift($fields, $this->fieldSeparator); # First field for MSH is '|'
+ return new $className($fields);
+ }
+
+ return new $className($fields, $autoIncrementIndices);
+ }
}
diff --git a/src/HL7/MessageHelpersTrait.php b/src/HL7/MessageHelpersTrait.php
index a80a6a3..7d5da55 100644
--- a/src/HL7/MessageHelpersTrait.php
+++ b/src/HL7/MessageHelpersTrait.php
@@ -1,5 +1,7 @@
getFirstSegmentInstance('MSH');
- return false !== strpos($msh->getMessageType(), 'ORM');
+ return str_contains($msh->getMessageType(), 'ORM');
}
- /**
- * Check if given message is an ORU
- *
- * @return bool
- */
public function isOru(): bool
{
/** @var MSH $msh */
$msh = $this->getFirstSegmentInstance('MSH');
- return false !== strpos($msh->getMessageType(), 'ORU');
+ return str_contains($msh->getMessageType(), 'ORU');
}
- /**
- * Check if given message is an ADT
- *
- * @return bool
- */
public function isAdt(): bool
{
/** @var MSH $msh */
$msh = $this->getFirstSegmentInstance('MSH');
- return false !== strpos($msh->getMessageType(), 'ADT');
+ return str_contains($msh->getMessageType(), 'ADT');
}
- /**
- * Check if given message is a SIU
- *
- * @return bool
- */
public function isSiu(): bool
{
/** @var MSH $msh */
$msh = $this->getFirstSegmentInstance('MSH');
- return false !== strpos($msh->getMessageType(), 'SIU');
+ return str_contains($msh->getMessageType(), 'SIU');
}
/**
* Check if given segment is present in the message object
- *
- * @param string $segment
- * @return bool
*/
public function hasSegment(string $segment): bool
{
@@ -143,7 +120,6 @@ public function hasSegment(string $segment): bool
/**
* Return the first segment with given name in the message
*
- * @param string $segment name of the segment to return
* @return mixed|null
*/
public function getFirstSegmentInstance(string $segment)
@@ -155,9 +131,6 @@ public function getFirstSegmentInstance(string $segment)
}
/**
- * Remove a segment from the message
- *
- * @param Segment $segment
* @param bool $reIndex After deleting, re-index remaining segments of same name
*/
public function removeSegment(Segment $segment, bool $reIndex = false): void
@@ -178,11 +151,6 @@ public function removeSegment(Segment $segment, bool $reIndex = false): void
}
}
- /**
- * Check if the message has any data
- *
- * @return bool
- */
public function isEmpty(): bool
{
return empty($this->getSegments());
diff --git a/src/HL7/Messages/ACK.php b/src/HL7/Messages/ACK.php
index bf638ca..6f8ed06 100644
--- a/src/HL7/Messages/ACK.php
+++ b/src/HL7/Messages/ACK.php
@@ -1,9 +1,10 @@
fields = [];
-
$this->fields[0] = $name;
if (\is_array($fields)) {
@@ -69,7 +67,6 @@ public function __construct(string $name, array $fields = null)
*
* @param int $index Index to set
* @param string|array $value Value for field
- * @return boolean
*/
public function setField(int $index, $value = ''): bool
{
@@ -108,7 +105,6 @@ private function isValueEmpty($value): bool
* Remove any existing value from the field
*
* @param int $index Field index
- * @return void
*/
public function clearField(int $index): void
{
@@ -136,7 +132,6 @@ public function getField(int $index)
* Get the number of fields for this segment, not including the name
*
* @return int number of fields
- * @access public
*/
public function size(): int
{
@@ -164,8 +159,7 @@ public function getFields(int $from = 0, int $to = null): array
/**
* Get the name of the segment. This is basically the value at index 0
*
- * @return mixed Name of segment
- * @access public
+ * @return string Name of segment
*/
public function getName(): string
{
diff --git a/src/HL7/Segments/AIG.php b/src/HL7/Segments/AIG.php
index 9f9768e..e789340 100644
--- a/src/HL7/Segments/AIG.php
+++ b/src/HL7/Segments/AIG.php
@@ -1,5 +1,7 @@
assertSame(1, $obx->getID());
+ $obx = new OBX();
+ $this->assertSame(2, $obx->getID());
+ }
+}
diff --git a/tests/Segments/PIDTest.php b/tests/Segments/PIDTest.php
index 777678e..8234dbb 100644
--- a/tests/Segments/PIDTest.php
+++ b/tests/Segments/PIDTest.php
@@ -1,5 +1,7 @@