Skip to content
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

Improved type checking in PHP mode #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions kphp_checks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

#ifndef KPHP

function typeof($arg)
{
return get_class($arg);
}


function kphp_polyfill_getDeclaredProps($v, array &$errors):array
{
$errors = [];
$refClass = new ReflectionClass($v);
$declaredProps = [];
foreach ( $refClass->getProperties(ReflectionProperty::IS_PUBLIC|ReflectionProperty::IS_PROTECTED|ReflectionProperty::IS_PRIVATE) as $prop ) {
$declaredProps[$prop->name] = 1;
};
return $declaredProps;
}
function kphp_polyfill_checkPropExists(?array &$declaredProps, string $propName, $value, array &$errors)
{
if (!isset($declaredProps[$propName])) {
$type = gettype($value);
$errors [] = "public $type \$$propName = null;";
}
}
function kphp_polyfill_verifyProps($v, array $errors)
{
if (count($errors)) {
throw new Exception("Fields not declared as property in ".get_class($v) ."\n".implode("\n", $errors));
}
}

function kphp_polyfill_checkInstance(?object $instance, string $class_name)
{
if ($instance && ! ($instance instanceof $class_name)) {
throw new Exception("Can't cast instance of ".get_class($instance) ." to class $class_name.");
}
}
#endif

14 changes: 13 additions & 1 deletion kphp_polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function instance_cache_delete(string $key): bool {
*/
function instance_cast($instance, string $class_name) {
// it just returns the argument, but KPHP infers it as SomeClass
kphp_polyfill_checkInstance($instance, $class_name);
return $instance;
}

Expand Down Expand Up @@ -180,13 +181,22 @@ function to_array_debug($instance, $with_class_names = false, $public_members_on

$toArray = function($v) use (&$toArray, &$demangleField, &$with_class_names, $public_members_only, $isPrivateField) {
if (is_object($v)) {
if ($v instanceof \stdClass) {
throw new Exception("Invalid use stdclass");
}
$result = [];
$errors = [];
$declaredProps = kphp_polyfill_getDeclaredProps($v, $errors);
foreach ((array)$v as $field => $value) {
$propName = $demangleField($field);
if ($public_members_only && $isPrivateField($field)) {
continue;
}
kphp_polyfill_checkPropExists($declaredProps, $propName, $value, $errors);

$result[$demangleField($field)] = $toArray($value);
}
kphp_polyfill_verifyProps($v, $errors);
if ($with_class_names) {
$result['__class_name'] = get_class($v);
}
Expand All @@ -206,7 +216,7 @@ function to_array_debug($instance, $with_class_names = false, $public_members_on
* @see to_array_debug function
*/
function instance_to_array($instance, $with_class_names = false) {
return to_array_debug($instance, $with_class_names);
return to_array_debug($instance, $with_class_names, true);
}

#endregion
Expand Down Expand Up @@ -1124,3 +1134,5 @@ function ffi_array_get(\FFI\CData $arr, int $index) {


#endif

require_once __DIR__ .'/kphp_checks.php';