Skip to content

Declare ext/spl constants in stubs #9226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
#include "zend_exceptions.h"

#include "php_spl.h"
#include "spl_array_arginfo.h"
#include "spl_functions.h"
#include "spl_engine.h"
#include "spl_iterators.h"
#include "spl_array.h"
#include "spl_array_arginfo.h"
#include "spl_exceptions.h"

zend_object_handlers spl_handler_ArrayObject;
Expand All @@ -41,18 +41,6 @@ zend_object_handlers spl_handler_ArrayIterator;
PHPAPI zend_class_entry *spl_ce_ArrayIterator;
PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterator;

#define SPL_ARRAY_STD_PROP_LIST 0x00000001
#define SPL_ARRAY_ARRAY_AS_PROPS 0x00000002
#define SPL_ARRAY_CHILD_ARRAYS_ONLY 0x00000004
#define SPL_ARRAY_IS_SELF 0x01000000
#define SPL_ARRAY_USE_OTHER 0x02000000
#define SPL_ARRAY_INT_MASK 0xFFFF0000
#define SPL_ARRAY_CLONE_MASK 0x0100FFFF

#define SPL_ARRAY_METHOD_NO_ARG 0
#define SPL_ARRAY_METHOD_CALLBACK_ARG 1
#define SPL_ARRAY_METHOD_SORT_FLAGS_ARG 2

typedef struct _spl_array_object {
zval array;
uint32_t ht_iter;
Expand Down Expand Up @@ -1819,18 +1807,10 @@ PHP_MINIT_FUNCTION(spl_array)

memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));

REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "STD_PROP_LIST", SPL_ARRAY_STD_PROP_LIST);
REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "ARRAY_AS_PROPS", SPL_ARRAY_ARRAY_AS_PROPS);

REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST", SPL_ARRAY_STD_PROP_LIST);
REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS", SPL_ARRAY_ARRAY_AS_PROPS);

spl_ce_RecursiveArrayIterator = register_class_RecursiveArrayIterator(spl_ce_ArrayIterator, spl_ce_RecursiveIterator);
spl_ce_RecursiveArrayIterator->create_object = spl_array_object_new;
spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;

REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);

return SUCCESS;
}
/* }}} */
12 changes: 12 additions & 0 deletions ext/spl/spl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
#include "php_spl.h"
#include "spl_iterators.h"

#define SPL_ARRAY_STD_PROP_LIST 0x00000001
#define SPL_ARRAY_ARRAY_AS_PROPS 0x00000002
#define SPL_ARRAY_CHILD_ARRAYS_ONLY 0x00000004
#define SPL_ARRAY_IS_SELF 0x01000000
#define SPL_ARRAY_USE_OTHER 0x02000000
#define SPL_ARRAY_INT_MASK 0xFFFF0000
#define SPL_ARRAY_CLONE_MASK 0x0100FFFF

#define SPL_ARRAY_METHOD_NO_ARG 0
#define SPL_ARRAY_METHOD_CALLBACK_ARG 1
#define SPL_ARRAY_METHOD_SORT_FLAGS_ARG 2

extern PHPAPI zend_class_entry *spl_ce_ArrayObject;
extern PHPAPI zend_class_entry *spl_ce_ArrayIterator;
extern PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterator;
Expand Down
28 changes: 28 additions & 0 deletions ext/spl/spl_array.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
{
/**
* @var int
* @cvalue SPL_ARRAY_STD_PROP_LIST
*/
const STD_PROP_LIST = UNKNOWN;
/**
* @var int
* @cvalue SPL_ARRAY_ARRAY_AS_PROPS
*/
const ARRAY_AS_PROPS = UNKNOWN;

public function __construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class) {}

/** @tentative-return-type */
Expand Down Expand Up @@ -81,6 +92,17 @@ public function __debugInfo(): array {}

class ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable
{
/**
* @var int
* @cvalue SPL_ARRAY_STD_PROP_LIST
*/
public const STD_PROP_LIST = UNKNOWN;
/**
* @var int
* @cvalue SPL_ARRAY_ARRAY_AS_PROPS
*/
public const ARRAY_AS_PROPS = UNKNOWN;

public function __construct(array|object $array = [], int $flags = 0) {}

/**
Expand Down Expand Up @@ -224,6 +246,12 @@ public function __debugInfo(): array {}

class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator
{
/**
* @var int
* @cvalue SPL_ARRAY_CHILD_ARRAYS_ONLY
*/
public const CHILD_ARRAYS_ONLY = UNKNOWN;

/** @tentative-return-type */
public function hasChildren(): bool {}

Expand Down
32 changes: 31 additions & 1 deletion ext/spl/spl_array_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2772,19 +2772,6 @@ PHP_MINIT_FUNCTION(spl_directory)
spl_ce_FilesystemIterator->create_object = spl_filesystem_object_new;
spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;

REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_MODE_MASK", SPL_FILE_DIR_CURRENT_MODE_MASK);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_PATHNAME", SPL_FILE_DIR_CURRENT_AS_PATHNAME);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_FILEINFO", SPL_FILE_DIR_CURRENT_AS_FILEINFO);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_SELF", SPL_FILE_DIR_CURRENT_AS_SELF);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_MODE_MASK", SPL_FILE_DIR_KEY_MODE_MASK);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_PATHNAME", SPL_FILE_DIR_KEY_AS_PATHNAME);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "FOLLOW_SYMLINKS", SPL_FILE_DIR_FOLLOW_SYMLINKS);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_FILENAME", SPL_FILE_DIR_KEY_AS_FILENAME);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "NEW_CURRENT_AND_KEY", SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "OTHER_MODE_MASK", SPL_FILE_DIR_OTHERS_MASK);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "SKIP_DOTS", SPL_FILE_DIR_SKIPDOTS);
REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "UNIX_PATHS", SPL_FILE_DIR_UNIXPATHS);

spl_ce_RecursiveDirectoryIterator = register_class_RecursiveDirectoryIterator(spl_ce_FilesystemIterator, spl_ce_RecursiveIterator);
spl_ce_RecursiveDirectoryIterator->create_object = spl_filesystem_object_new;

Expand All @@ -2800,11 +2787,6 @@ PHP_MINIT_FUNCTION(spl_directory)
spl_ce_SplFileObject = register_class_SplFileObject(spl_ce_SplFileInfo, spl_ce_RecursiveIterator, spl_ce_SeekableIterator);
spl_ce_SplFileObject->create_object = spl_filesystem_object_new_check;

REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "DROP_NEW_LINE", SPL_FILE_OBJECT_DROP_NEW_LINE);
REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_AHEAD", SPL_FILE_OBJECT_READ_AHEAD);
REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "SKIP_EMPTY", SPL_FILE_OBJECT_SKIP_EMPTY);
REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_CSV", SPL_FILE_OBJECT_READ_CSV);

spl_ce_SplTempFileObject = register_class_SplTempFileObject(spl_ce_SplFileObject);
spl_ce_SplTempFileObject->create_object = spl_filesystem_object_new_check;

Expand Down
1 change: 1 addition & 0 deletions ext/spl/spl_directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct _spl_filesystem_object {
#define SPL_FILE_DIR_KEY_AS_PATHNAME 0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
#define SPL_FILE_DIR_KEY_AS_FILENAME 0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
#define SPL_FILE_DIR_KEY_MODE_MASK 0x00000F00 /* mask RecursiveDirectoryTree::key() */
#define SPL_FILE_NEW_CURRENT_AND_KEY SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this even need to be defined? Wouldn't it be possible to compose the constant values in the stub? (or am I asking for a feature which does not yet exist)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not strictly required to have, but I prefer not to put too much C code into stubs, so I usually create a new macro when an expression needs to be used.

#define SPL_FILE_DIR_KEY(intern,mode) ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)

#define SPL_FILE_DIR_SKIPDOTS 0x00001000 /* Tells whether it should skip dots or not */
Expand Down
82 changes: 82 additions & 0 deletions ext/spl/spl_directory.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,67 @@ public function __toString(): string {}

class FilesystemIterator extends DirectoryIterator
{
/**
* @var int
* @cvalue SPL_FILE_DIR_CURRENT_MODE_MASK
*/
public const CURRENT_MODE_MASK = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_CURRENT_AS_PATHNAME
*/
public const CURRENT_AS_PATHNAME = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_CURRENT_AS_FILEINFO
*/
public const CURRENT_AS_FILEINFO = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_CURRENT_AS_SELF
*/
public const CURRENT_AS_SELF = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_KEY_MODE_MASK
*/
public const KEY_MODE_MASK = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_KEY_AS_PATHNAME
*/
public const KEY_AS_PATHNAME = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_FOLLOW_SYMLINKS
*/
public const FOLLOW_SYMLINKS = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_KEY_AS_FILENAME
*/
public const KEY_AS_FILENAME = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_NEW_CURRENT_AND_KEY
*/
public const NEW_CURRENT_AND_KEY = UNKNOWN;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public const NEW_CURRENT_AND_KEY = UNKNOWN;
public const NEW_CURRENT_AND_KEY = self::CURRENT_AS_FILEINFO|self::KEY_AS_FILENAME;

Would be the idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, great idea, I'll try it if gen_stub properly support it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, unfortunately, constant evaluation doesn't work properly :( So I'll stay with my original implementation

/**
* @var int
* @cvalue SPL_FILE_DIR_OTHERS_MASK
*/
public const OTHER_MODE_MASK = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_SKIPDOTS
*/
public const SKIP_DOTS = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_DIR_UNIXPATHS
*/
public const UNIX_PATHS = UNKNOWN;

public function __construct(string $directory, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS) {}

/** @tentative-return-type */
Expand Down Expand Up @@ -197,6 +258,27 @@ public function count(): int {}

class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator
{
/**
* @var int
* @cvalue SPL_FILE_OBJECT_DROP_NEW_LINE
*/
public const DROP_NEW_LINE = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_OBJECT_READ_AHEAD
*/
public const READ_AHEAD = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_OBJECT_SKIP_EMPTY
*/
public const SKIP_EMPTY = UNKNOWN;
/**
* @var int
* @cvalue SPL_FILE_OBJECT_READ_CSV
*/
public const READ_CSV = UNKNOWN;

/** @param resource|null $context */
public function __construct(string $filename, string $mode = "r", bool $useIncludePath = false, $context = null) {}

Expand Down
Loading