Skip to content

Commit

Permalink
PHP8 tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo101 committed Nov 10, 2022
1 parent 67ac6ab commit 6f40182
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/IPC/SysV.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class SysV implements IPCInterface
private int $size;
private Mediator $mediator;

/** @var resource|false */
/** @var resource|\SysvSemaphore|false */
private $sem = false;
/** @var resource|false */
/** @var resource|\SysvSharedMemory|false */
private $shm = false;
/** @var resource|false */
/** @var resource|\SysvMessageQueue|false */
private $queue = false;

public function __construct($size = null)
Expand All @@ -50,25 +50,28 @@ public function purge()
$this->setupIPC();
}

private function check($var): bool
/**
* Check if the variable given is a resource or PHP8 'resource' class
* @param $var
*
* @return bool
*/
private function isResource($var): bool
{
if (PHP_MAJOR_VERSION >= 8) {
return is_object($var);
}
return is_resource($var);
return PHP_MAJOR_VERSION < 8 ? is_resource($var) : is_object($var);
}

private function purgeSEM()
{
if ($this->sem) {
sem_remove($this->sem);
@sem_remove($this->sem);
}
$this->sem = null;
}

private function purgeSHM()
{
if (!$this->check($this->shm)) {
if (!$this->isResource($this->shm)) {
$this->setupIPC();
}

Expand All @@ -81,7 +84,7 @@ private function purgeSHM()

private function purgeQueue()
{
if (!$this->check($this->queue)) {
if (!$this->isResource($this->queue)) {
$this->setupIPC();
}

Expand All @@ -99,12 +102,12 @@ private function setupIPC()
{
$this->sem = sem_get($this->mediator->getGuid());
$this->shm = shm_attach($this->mediator->getGuid(), $this->size);
if (!$this->check($this->shm)) {
if (!$this->isResource($this->shm)) {
throw new Exception(sprintf("Could not attach to Shared Memory Block 0x%08x", $this->mediator->getGuid()));
}

$this->queue = msg_get_queue($this->mediator->getGuid());
if (!$this->check($this->queue)) {
if (!$this->isResource($this->queue)) {
throw new Exception(sprintf("Could not attach to message queue 0x%08x", $this->mediator->getGuid()));
}
}
Expand Down Expand Up @@ -388,7 +391,7 @@ public function setMediator(Mediator $mediator)
*/
private function testIpc(): bool
{
if (!$this->check($this->shm)) {
if (!$this->isResource($this->shm)) {
return false;
}

Expand Down

0 comments on commit 6f40182

Please sign in to comment.