-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathFFITest.php
More file actions
92 lines (87 loc) · 3.05 KB
/
FFITest.php
File metadata and controls
92 lines (87 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace Psalm\Tests;
use Override;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
/**
* tests for the handling of code using the PHP FFI extension
*
* https://www.php.net/manual/en/book.ffi.php
*/
final class FFITest extends TestCase
{
use ValidCodeAnalysisTestTrait;
/**
* @psalm-pure
*/
#[Override]
public function providerValidCodeParse(): iterable
{
return [
// Examples are from https://www.php.net/manual/en/ffi.examples-basic.php
// Some of them needed tweaks, e.g. due to unsafe use of var_dump().
'Example #1 Calling a function from shared library' => [
'code' => '<?php
$ffi = FFI::cdef(
"int printf(const char *format, ...);", // this is a regular C declaration
"libc.so.6"
);
$ffi->printf("Hello %s!\n", "world");
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '7.4',
],
'Example #3 Accessing existing C variables' => [
'code' => '<?php
$ffi = FFI::cdef(
"int errno;", // this is a regular C declaration
"libc.so.6"
);
echo $ffi->errno;
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '7.4',
],
'Example #5 Working with C arrays' => [
'code' => '<?php
// create C data structure
$a = FFI::new("long[1024]");
// work with it like with a regular PHP array
$size = count($a);
for ($i = 0; $i < $size; $i++) {
$a[$i] = $i;
}
$sum = 0;
/** @psalm-suppress MixedAssignment */
foreach ($a as $n) {
/** @psalm-suppress MixedOperand */
$sum += $n;
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '7.4',
],
'Example #6 Working with C enums' => [
'code' => '<?php
$a = FFI::cdef(
"typedef enum _zend_ffi_symbol_kind {
ZEND_FFI_SYM_TYPE,
ZEND_FFI_SYM_CONST = 2,
ZEND_FFI_SYM_VAR,
ZEND_FFI_SYM_FUNC
} zend_ffi_symbol_kind;"
);
echo $a->ZEND_FFI_SYM_TYPE;
echo $a->ZEND_FFI_SYM_CONST;
echo $a->ZEND_FFI_SYM_VAR;
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '7.4',
],
];
}
}