From 21b70400885eadbd7df0d2be239c0e865eed46bd Mon Sep 17 00:00:00 2001 From: Yuichi Ishikawa Date: Thu, 25 Apr 2024 22:03:30 +0900 Subject: [PATCH] type definitions --- .github/workflows/tests.yml | 13 +++++++++++-- phpstan-interop.neon | 21 +++++++++++++++++++++ phpstan.neon.dist | 4 ++++ src/Buffer.php | 14 +++++++++++--- 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 phpstan-interop.neon create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index dc041fc..87bfd1a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,6 +1,9 @@ name: tests -on: [push] +on: + push: + branches: + - main jobs: no_ext_tests: @@ -21,11 +24,17 @@ jobs: php_version: ${{ matrix.php }} php_extensions: ffi + - name: PHP Static Analysys + uses: php-actions/phpstan@v3 + with: + php_version: ${{ matrix.php }} + path: src/ + - name: PHPUnit Tests uses: php-actions/phpunit@v3 with: configuration: tests/phpunit.xml version: 10.5 php_version: ${{ matrix.php }} - args: --filter ReleaseTest + php_extensions: ffi diff --git a/phpstan-interop.neon b/phpstan-interop.neon new file mode 100644 index 0000000..191bd81 --- /dev/null +++ b/phpstan-interop.neon @@ -0,0 +1,21 @@ +parameters: + ignoreErrors: + +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dtype\\(\\)#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:addr\\(\\)#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dump\\(\\)#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:value_size\\(\\)#" +# - +# message: "#function count expects array|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#" +# - +# message: "#^Argument of an invalid type Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray supplied for foreach, only iterables are supported#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:bytes\\(\\)#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:copy\\(\\)#" +# - +# message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:read\\(\\)#" diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..d7f8277 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: 6 + paths: + - src diff --git a/src/Buffer.php b/src/Buffer.php index 826e18c..a2efee6 100644 --- a/src/Buffer.php +++ b/src/Buffer.php @@ -9,11 +9,17 @@ use LogicException; use FFI; +class complex_t { + public float $real; + public float $imag; +} + class Buffer implements LinearBuffer { const MAX_BYTES = 2147483648; // 2**31 static protected ?FFI $ffi = null; + /** @var array $typeString */ protected static $typeString = [ NDArray::bool => 'uint8_t', NDArray::int8 => 'int8_t', @@ -33,6 +39,7 @@ class Buffer implements LinearBuffer NDArray::complex64 => 'rindow_complex_float', NDArray::complex128 => 'rindow_complex_double', ]; + /** @var array $valueSize */ protected static $valueSize = [ NDArray::bool => 1, NDArray::int8 => 1, @@ -76,7 +83,7 @@ public function __construct(int $size, int $dtype) $this->data = self::$ffi->new("{$declaration}[{$size}]"); } - protected function assertOffset($method, mixed $offset) : void + protected function assertOffset(string $method, mixed $offset) : void { if(!is_int($offset)) { throw new TypeError($method.'(): Argument #1 ($offset) must be of type int'); @@ -86,14 +93,14 @@ protected function assertOffset($method, mixed $offset) : void } } - protected function assertOffsetIsInt($method, mixed $offset) : void + protected function assertOffsetIsInt(string $method, mixed $offset) : void { if(!is_int($offset)) { throw new TypeError($method.'(): Argument #1 ($offset) must be of type int'); } } - protected function isComplex($dtype=null) : bool + protected function isComplex(int $dtype=null) : bool { $dtype = $dtype ?? $this->dtype; return $dtype==NDArray::complex64||$dtype==NDArray::complex128; @@ -148,6 +155,7 @@ public function offsetSet(mixed $offset, mixed $value): void $type = gettype($value); throw new InvalidArgumentException("Cannot convert to complex number.: ".$type); } + /** @var complex_t $value */ $value = self::$ffi->new(self::$typeString[$this->dtype]); $value->real = $real; $value->imag = $imag;