Skip to content

Commit

Permalink
add type hinting with PHP 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
remicollet committed Jan 23, 2024
1 parent dcb88e5 commit 93342c6
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ config.guess
config.h.in
config.h.in~
config.sub
configure~
configure
configure.in
configure.ac
Expand Down
4 changes: 4 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ more than adequate.
<license uri="https://www.php.net/license/3_01.txt" filesource="LICENSE">PHP-3.01</license>
<notes>
- drop PHP 5 support
- add type hinting with PHP 8+
</notes>
<contents>
<dir name="/">
Expand All @@ -65,6 +66,9 @@ more than adequate.
<file name="config.w32" role="src" />
<dir name="src">
<file name="dio.c" role="src" />
<file name="dio.stub.php" role="src" />
<file name="dio_arginfo.h" role="src" />
<file name="dio_legacy_arginfo.h" role="src" />
<file name="dio_common.c" role="src" />
<file name="dio_posix.c" role="src" />
<file name="dio_win32.c" role="src" />
Expand Down
107 changes: 6 additions & 101 deletions src/dio.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
# endif /* CNEW_RTSCTS */
#endif /* !CRTSCTS */

#if PHP_VERSION_ID < 80000
#include "dio_legacy_arginfo.h"
#else
#include "dio_arginfo.h"
#endif
/*
+----------------------------------------------------------------------+
| DEPRECATED FUNCTIONALITY |
Expand Down Expand Up @@ -738,116 +743,16 @@ static void dio_init_legacy_defines(int module_number) {
#endif
}

ZEND_BEGIN_ARG_INFO_EX(dio_open_args, 0, 0, 2)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, flags)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_fdopen_args, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_dup_args, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()


ZEND_BEGIN_ARG_INFO_EX(dio_read_args, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, n)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_write_args, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, len)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_stat_args, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_truncate_args, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_seek_args, 0, 0, 3)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, pos)
ZEND_ARG_INFO(0, whence)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_fcntl_args, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, cmd)
ZEND_ARG_INFO(0, arg)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_tcsetattr_args, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_INFO(0, args)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_close_args, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

/*
+----------------------------------------------------------------------+
| END OF DEPRECATED FUNCTIONALITY |
+----------------------------------------------------------------------+
*/

ZEND_BEGIN_ARG_INFO_EX(dio_raw_args, 0, 0, 2)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(dio_serial_args, 0, 0, 2)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

/* not used static zend_object_handlers dio_raw_object_handlers; */

static zend_function_entry dio_functions[] = {
/* Class functions. */

/* Legacy functions (Deprecated - See dio_legacy.c) */
PHP_FE(dio_open, dio_open_args)
#ifndef PHP_WIN32
PHP_FE(dio_fdopen, dio_fdopen_args)
PHP_FE(dio_dup, dio_dup_args)
PHP_FE(dio_truncate, dio_truncate_args)
#endif
PHP_FE(dio_stat, dio_stat_args)
PHP_FE(dio_seek, dio_seek_args)
#ifndef PHP_WIN32
PHP_FE(dio_fcntl, dio_fcntl_args)
#endif
PHP_FE(dio_read, dio_read_args)
PHP_FE(dio_write, dio_write_args)
PHP_FE(dio_close, dio_close_args)
#ifndef PHP_WIN32
PHP_FE(dio_tcsetattr, dio_tcsetattr_args)
#endif

/* Stream functions */
PHP_FE(dio_raw, dio_raw_args)
PHP_FE(dio_serial, dio_serial_args)

/* End of functions */
{NULL, NULL, NULL}
};

zend_module_entry dio_module_entry = {
STANDARD_MODULE_HEADER,
"dio",
dio_functions,
ext_functions,
PHP_MINIT(dio),
NULL,
NULL,
Expand Down
84 changes: 84 additions & 0 deletions src/dio.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* @generate-class-entries
* @generate-legacy-arginfo
*/

/**
* @return resource|false
*/
function dio_open(string $filename, int $flags, int $mode=0) {}

#ifndef PHP_WIN32
/**
* @param resource $fd
* @return resource|false
*/
function dio_fdopen($fd) {}

/**
* @param resource $fd
* @return resource|false
*/
function dio_dup($fd) {}

/**
* @param resource $fd
*/
function dio_truncate($fd, int $offset): bool {}
#endif

/**
* @param resource $fd
*/
function dio_stat($fd): array|false {}

/**
* @param resource $fd
*/
function dio_seek($fd, int $pos, int $whence=SEEK_SET): int|false {}

#ifndef PHP_WIN32
/**
* @param resource $fd
* @param mixed $arg
* @return mixed
*/
function dio_fcntl($fd, int $cmd, $arg=NULL) {}
#endif

/**
* @param resource $fd
*/
function dio_read($fd, int $n=1024): int|null|false {}

/**
* @param resource $fd
*/
function dio_write($fd, string $data, int $len=0): int|false {}

/**
* @param resource $fd
*/
function dio_close($fd): null|false {}

#ifndef PHP_WIN32
/**
* @param resource $fd
* @param mixed $arg
* @return mixed
*/
function dio_tcsetattr($fd, array $args): bool {}
#endif

/**
* @return resource|false
*/
function dio_raw(string $filename, string $mode, ?array $options=NULL) {}

/**
* @return resource|false
*/
function dio_serial(string $filename, string $mode, ?array $options=NULL) {}

126 changes: 126 additions & 0 deletions src/dio_arginfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 74f93c791a08145405f1f08b0d8ee26876e92798 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_open, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, mode, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

#if !defined(PHP_WIN32)
ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_fdopen, 0, 0, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()
#endif

#if !defined(PHP_WIN32)
#define arginfo_dio_dup arginfo_dio_fdopen
#endif

#if !defined(PHP_WIN32)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_truncate, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
ZEND_END_ARG_INFO()
#endif

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_stat, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_seek, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, pos, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, whence, IS_LONG, 0, "SEEK_SET")
ZEND_END_ARG_INFO()

#if !defined(PHP_WIN32)
ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_fcntl, 0, 0, 2)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, cmd, IS_LONG, 0)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, arg, "NULL")
ZEND_END_ARG_INFO()
#endif

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_read, 0, 1, MAY_BE_LONG|MAY_BE_NULL|MAY_BE_FALSE)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, n, IS_LONG, 0, "1024")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_dio_write, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, len, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_close, 0, 1, IS_FALSE, 1)
ZEND_ARG_INFO(0, fd)
ZEND_END_ARG_INFO()

#if !defined(PHP_WIN32)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dio_tcsetattr, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, fd)
ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
#endif

ZEND_BEGIN_ARG_INFO_EX(arginfo_dio_raw, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, mode, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "NULL")
ZEND_END_ARG_INFO()

#define arginfo_dio_serial arginfo_dio_raw


ZEND_FUNCTION(dio_open);
#if !defined(PHP_WIN32)
ZEND_FUNCTION(dio_fdopen);
#endif
#if !defined(PHP_WIN32)
ZEND_FUNCTION(dio_dup);
#endif
#if !defined(PHP_WIN32)
ZEND_FUNCTION(dio_truncate);
#endif
ZEND_FUNCTION(dio_stat);
ZEND_FUNCTION(dio_seek);
#if !defined(PHP_WIN32)
ZEND_FUNCTION(dio_fcntl);
#endif
ZEND_FUNCTION(dio_read);
ZEND_FUNCTION(dio_write);
ZEND_FUNCTION(dio_close);
#if !defined(PHP_WIN32)
ZEND_FUNCTION(dio_tcsetattr);
#endif
ZEND_FUNCTION(dio_raw);
ZEND_FUNCTION(dio_serial);


static const zend_function_entry ext_functions[] = {
ZEND_FE(dio_open, arginfo_dio_open)
#if !defined(PHP_WIN32)
ZEND_FE(dio_fdopen, arginfo_dio_fdopen)
#endif
#if !defined(PHP_WIN32)
ZEND_FE(dio_dup, arginfo_dio_dup)
#endif
#if !defined(PHP_WIN32)
ZEND_FE(dio_truncate, arginfo_dio_truncate)
#endif
ZEND_FE(dio_stat, arginfo_dio_stat)
ZEND_FE(dio_seek, arginfo_dio_seek)
#if !defined(PHP_WIN32)
ZEND_FE(dio_fcntl, arginfo_dio_fcntl)
#endif
ZEND_FE(dio_read, arginfo_dio_read)
ZEND_FE(dio_write, arginfo_dio_write)
ZEND_FE(dio_close, arginfo_dio_close)
#if !defined(PHP_WIN32)
ZEND_FE(dio_tcsetattr, arginfo_dio_tcsetattr)
#endif
ZEND_FE(dio_raw, arginfo_dio_raw)
ZEND_FE(dio_serial, arginfo_dio_serial)
ZEND_FE_END
};
Loading

0 comments on commit 93342c6

Please sign in to comment.