Skip to content

Commit 443a762

Browse files
refactor: phpcs and composer related updates (#21)
Hey there, I know this looks huge, but as far as I can see, there are no standards in the PHP code. I thought I might add some standards to make the PHP SDK more collaborative for everyone. If you think these changes are too much, I wouldn't mind creating multiple PRs for a smoother transition. --------- Co-authored-by: Muhammad Azeez <[email protected]>
1 parent ac61e14 commit 443a762

24 files changed

+387
-308
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
src/ExtismLib.php
55
example/php_errors.log
66
php_errors.log
7+
.phpunit.cache
8+
.phpunit.result.cache
9+
.php-cs-fixer.cache
10+
.vscode

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ prepare:
55

66
test: prepare
77
php vendor/bin/phpunit ./tests
8+
9+
cscheck:
10+
vendor/bin/phpcs .
11+
12+
csfix:
13+
vendor/bin/php-cs-fixer fix .

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ First you should add a using statement for Extism:
4141
```php
4242
use Extism\Plugin;
4343
use Extism\Manifest;
44-
use Extism\UrlWasmSource;
44+
use Extism\Manifest\UrlWasmSource;
4545
```
4646

4747
## Creating A Plug-in

composer.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"psr-4": {
3434
"Extism\\": "src/"
3535
},
36-
"files": [
37-
"src/Manifest.php",
38-
"src/Plugin.php",
39-
"src/CurrentPlugin.php"
40-
]
36+
"psr-0": {
37+
"LibExtism": "src"
38+
}
4139
},
4240
"autoload-dev": {
43-
"psr-4": {}
41+
"psr-4": {
42+
"Extism\\Tests\\": "tests/"
43+
}
4444
},
4545
"config": {
4646
"sort-packages": true
@@ -49,6 +49,8 @@
4949
"scripts": {},
5050
"scripts-descriptions": {},
5151
"require-dev": {
52-
"phpunit/phpunit": "^9"
52+
"friendsofphp/php-cs-fixer": "^3.59",
53+
"phpunit/phpunit": "^9",
54+
"squizlabs/php_codesniffer": "^3.10"
5355
}
5456
}

example/index.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
2-
use Extism\UrlWasmSource;
3-
use Extism\Manifest;
2+
43
use Extism\Plugin;
4+
use Extism\Manifest;
5+
use Extism\Manifest\UrlWasmSource;
56

6-
require_once __DIR__ . "/../src/Plugin.php";
7+
require "../vendor/autoload.php";
78

89
$wasm = new UrlWasmSource("https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm");
910
$manifest = new Manifest($wasm);
@@ -17,4 +18,4 @@
1718

1819
$plugin = new Plugin($manifest, true);
1920
$output = $plugin->call("count_vowels", "Yellow, World!");
20-
var_dump($output);
21+
var_dump($output);

example/memory_test.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?php
2-
use Extism\PathWasmSource;
3-
use Extism\UrlWasmSource;
2+
43
use Extism\Manifest;
4+
use Extism\Manifest\PathWasmSource;
55
use Extism\Plugin;
66
use Extism\HostFunction;
77
use Extism\ExtismValType;
88
use Extism\CurrentPlugin;
99

10-
require_once __DIR__ . "/../src/Plugin.php";
11-
require_once __DIR__ . "/../src/HostFunction.php";
10+
require "../vendor/autoload.php";
1211

1312
$wasm = new PathWasmSource(__DIR__ . "/../wasm/count_vowels_kvstore.wasm");
1413
$manifest = new Manifest($wasm);
1514

16-
for ($i = 0; $i < 10_000; $i++){
15+
for ($i = 0; $i < 10_000; $i++) {
1716
$kvstore = [];
1817

1918
$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$kvstore) {
@@ -32,4 +31,4 @@
3231
}
3332
}
3433

35-
readline();
34+
readline();

phpcs.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="extism-php-sdk">
3+
<rule ref="PSR12">
4+
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
5+
<exclude name="Generic.Files.LineLength"/>
6+
</rule>
7+
<exclude-pattern>*/vendor/*</exclude-pattern>
8+
</ruleset>

phpunit.xml.dist

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
>
8+
<testsuites>
9+
<testsuite name="unit">
10+
<directory>tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/CurrentPlugin.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
<?php
2+
23
declare(strict_types=1);
3-
namespace Extism;
44

5-
require_once __DIR__ . "/LibExtism.php";
5+
namespace Extism;
66

77
/**
88
* Represents a plugin that is calling the currently running host function.
99
*/
1010
class CurrentPlugin
1111
{
1212
private \FFI\CData $handle;
13-
private \LibExtism $lib;
13+
private \Extism\Internal\LibExtism $lib;
1414

1515
/**
1616
* constructor.
17-
*
18-
* @param \LibExtism $lib
17+
*
18+
* @param \Extism\Internal\LibExtism $lib
1919
* @param \FFI\CData $handle
2020
*/
21-
function __construct($lib, \FFI\CData $handle)
21+
public function __construct($lib, \FFI\CData $handle)
2222
{
2323
$this->handle = $handle;
2424
$this->lib = $lib;
2525
}
2626

2727
/**
2828
* Reads a string from the plugin's memory at the given offset.
29-
*
29+
*
3030
* @param int $offset Offset of the block to read.
3131
*/
32-
function read_block(int $offset) : string
32+
public function read_block(int $offset): string
3333
{
3434
$ptr = $this->lib->extism_current_plugin_memory($this->handle);
3535
$ptr = $this->lib->ffi->cast("char *", $ptr);
@@ -42,20 +42,20 @@ function read_block(int $offset) : string
4242

4343
/**
4444
* Allocates a block of memory in the plugin's memory and returns the offset.
45-
*
45+
*
4646
* @param int $size Size of the block to allocate in bytes.
4747
*/
48-
function allocate_block(int $size) : int
48+
private function allocate_block(int $size): int
4949
{
5050
return $this->lib->extism_current_plugin_memory_alloc($this->handle, $size);
5151
}
5252

5353
/**
5454
* Writes a string to the plugin's memory, returning the offset of the block.
55-
*
55+
*
5656
* @param string $data Buffer to write to the plugin's memory.
5757
*/
58-
function write_block(string $data) : int
58+
public function write_block(string $data): int
5959
{
6060
$offset = $this->allocate_block(strlen($data));
6161
$this->fill_block($offset, $data);
@@ -64,11 +64,11 @@ function write_block(string $data) : int
6464

6565
/**
6666
* Fills a block of memory in the plugin's memory.
67-
*
67+
*
6868
* @param int $offset Offset of the block to fill.
6969
* @param string $data Buffer to fill the block with.
7070
*/
71-
function fill_block(int $offset, string $data) : void
71+
private function fill_block(int $offset, string $data): void
7272
{
7373
$ptr = $this->lib->extism_current_plugin_memory($this->handle);
7474
$ptr = $this->lib->ffi->cast("char *", $ptr);
@@ -79,11 +79,11 @@ function fill_block(int $offset, string $data) : void
7979

8080
/**
8181
* Frees a block of memory in the plugin's memory.
82-
*
82+
*
8383
* @param int $offset Offset of the block to free.
8484
*/
85-
function free_block(int $offset) : void
85+
private function free_block(int $offset): void
8686
{
8787
$this->lib->extism_current_plugin_memory_free($this->handle, $offset);
8888
}
89-
}
89+
}

src/ExtismValType.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Extism;
4+
5+
class ExtismValType
6+
{
7+
public const I32 = 0;
8+
public const I64 = 1;
9+
public const PTR = I64;
10+
public const F32 = 2;
11+
public const F64 = 3;
12+
public const V128 = 4;
13+
public const FUNC_REF = 5;
14+
public const EXTERN_REF = 6;
15+
}

src/HostFunction.php

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
2-
declare(strict_types=1);
3-
namespace Extism;
42

5-
require_once __DIR__ . "/LibExtism.php";
6-
require_once __DIR__ . "/CurrentPlugin.php";
3+
declare(strict_types=1);
74

5+
namespace Extism;
86
class ExtismValType
97
{
108
public const I32 = 0;
@@ -29,23 +27,23 @@ class ExtismValType
2927

3028
class HostFunction
3129
{
32-
private \LibExtism $lib;
30+
private \Extism\Internal\LibExtism $lib;
3331
private $callback;
3432

3533
public \FFI\CData $handle;
3634

3735
/**
3836
* Constructor
39-
*
37+
*
4038
* @param string $name Name of the function
4139
* @param array $inputTypes Array of input types. @see ExtismValType
4240
* @param array $outputTypes Array of output types
4341
* @param callable $callback Callback to invoke when the function is called
44-
*
42+
*
4543
* @example ../tests/PluginTest.php 82 84 Simple Example
4644
* @example ../tests/PluginTest.php 100 104 Manually read memory using CurrentPlugin
4745
*/
48-
function __construct(string $name, array $inputTypes, array $outputTypes, callable $callback)
46+
public function __construct(string $name, array $inputTypes, array $outputTypes, callable $callback)
4947
{
5048
$reflection = new \ReflectionFunction($callback);
5149
$arguments = $reflection->getParameters();
@@ -54,7 +52,7 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
5452
global $lib;
5553

5654
if ($lib == null) {
57-
$lib = new \LibExtism();
55+
$lib = new \Extism\Internal\LibExtism();
5856
}
5957

6058
$this->lib = $lib;
@@ -80,9 +78,9 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
8078

8179
$r = $callback(...$params);
8280

83-
if ($r == NULL) {
81+
if ($r == null) {
8482
$r = 0;
85-
} else if (gettype($r) == "string") {
83+
} elseif (gettype($r) == "string") {
8684
$r = $currentPlugin->write_block($r);
8785
}
8886

@@ -106,7 +104,6 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
106104
throw new \Exception("Unsupported type for output: " . $output->t);
107105
}
108106
}
109-
110107
// Throwing an exception in FFI callback is not supported and
111108
// causes a fatal error without a stack trace.
112109
// So we catch it and print the exception manually
@@ -123,12 +120,12 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
123120
$this->set_namespace("extism:host/user");
124121
}
125122

126-
function __destruct()
123+
public function __destruct()
127124
{
128125
$this->lib->extism_function_free($this->handle);
129126
}
130127

131-
function set_namespace(string $namespace)
128+
public function set_namespace(string $namespace)
132129
{
133130
$this->lib->extism_function_set_namespace($this->handle, $namespace);
134131
}
@@ -156,12 +153,12 @@ private static function get_type_name(\ReflectionParameter $param)
156153
}
157154

158155
private static function get_parameters(
159-
CurrentPlugin $currentPlugin,
156+
CurrentPlugin $currentPlugin,
160157
\FFI\CData $inputs,
161158
int $n_inputs,
162-
array $arguments,
163-
int $offset) : array
164-
{
159+
array $arguments,
160+
int $offset
161+
): array {
165162
$params = [];
166163

167164
if ($offset == 1) {
@@ -219,7 +216,7 @@ private static function validate_arguments(array $arguments, array $inputTypes)
219216

220217
if ($argType == null) {
221218
continue;
222-
} else if ($argType == "string") {
219+
} elseif ($argType == "string") {
223220
// string is represented as a pointer to a block of memory
224221
$argType = "int";
225222
}

0 commit comments

Comments
 (0)