Skip to content

Commit

Permalink
Improve proxy class constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Nov 3, 2021
1 parent 5e9694d commit c5bf40c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
12 changes: 11 additions & 1 deletion src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use FFI\Proxy\Exception\NotAvailableException;

abstract class Proxy implements ProxyInterface, ApiInterface
abstract class Proxy implements ApiInterface
{
use ProxyAwareTrait;
use ApiAwareTrait;
Expand All @@ -25,6 +25,16 @@ abstract class Proxy implements ProxyInterface, ApiInterface
*/
public \FFI $ffi;

/**
* @param \FFI|null $ffi
*/
public function __construct(\FFI $ffi = null)
{
if ($ffi !== null) {
$this->ffi = $ffi;
}
}

/**
* Proxy should not be serializable.
*/
Expand Down
30 changes: 20 additions & 10 deletions src/ProxyAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@

/**
* @property-read \FFI $ffi
* @mixin ProxyInterface
* @psalm-require-implements ProxyInterface
*/
trait ProxyAwareTrait
{
/**
* @see ProxyInterface::__call()
*
* @psalm-param string $method
* @psalm-param array $args
* @psalm-return mixed
Expand All @@ -31,9 +27,7 @@ public function __call(string $method, array $args)
}

/**
* @see ProxyInterface::__get()
*
* @psalm-param string $name
* @psalm-param non-empty-string $name
* @psalm-return mixed
*/
public function __get(string $name)
Expand All @@ -42,14 +36,30 @@ public function __get(string $name)
}

/**
* @see ProxyInterface::__set()
*
* @psalm-param string $name
* @psalm-param non-empty-string $name
* @psalm-param mixed $value
* @psalm-return void
*/
public function __set(string $name, $value): void
{
$this->ffi->$name = $value;
}

/**
* @psalm-param non-empty-string $name
* @psalm-return bool
*/
public function __isset(string $name): bool
{
return isset($this->ffi->$name);
}

/**
* @psalm-param non-empty-string $name
* @psalm-return bool
*/
public function __unset(string $name): void
{
unset($this->ffi->$name);
}
}
35 changes: 0 additions & 35 deletions src/ProxyInterface.php

This file was deleted.

0 comments on commit c5bf40c

Please sign in to comment.