<commit_before>#include "string.h"
#include "ctype.h"
#include "php.h"
#include "main/php_main.h"
#include "Zend/zend_API.h"
#include "Zend/zend_variables.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "Zend/zend_interfaces.h"
#include "zend_object_handlers.h"
#include "ext/pcre/php_pcre.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_var.h"

#include "ct_helper.h"
#include "php_pux.h"
#include "pux_mux.h"
#include "pux_functions.h"
#include "php_expandable_mux.h"
#include "pux_controller.h"

#include "annotation/scanner.h"
#include "annotation/annot.h"

zend_class_entry *ce_pux_controller;

const zend_function_entry controller_methods[] = {
  PHP_ME(Controller, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  PHP_ME(Controller, expand, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, getActionMethods, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, getActionRoutes, NULL, ZEND_ACC_PUBLIC)

  PHP_ME(Controller, before, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, after, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, toJson, NULL, ZEND_ACC_PUBLIC)

  // PHP_ME(Controller, __destruct,  NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) 
  PHP_FE_END
};

zend_bool phannot_fetch_argument_value(zval **arg, zval** value TSRMLS_DC) {
    zval **expr;
    if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
        return FAILURE;
    }
    return zend_hash_find(Z_ARRVAL_PP(expr), "value", sizeof("value"), (void**) value);
}

zend_bool phannot_fetch_argument_type(zval **arg, zval **type TSRMLS_DC) {
    zval **expr;
    if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
        return FAILURE;
    }
    return zend_hash_find(Z_ARRVAL_PP(expr), "type", sizeof("type"), (void**)type);
}

int strpos(const char *haystack, char *needle)
{
   char *p = strstr(haystack, needle);
   if (p)
      return p - haystack;
   return -1;
}




void pux_init_controller(TSRMLS_D) {
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "Pux\\Controller", controller_methods);
    ce_pux_controller = zend_register_internal_class(&ce TSRMLS_CC);
    zend_class_implements(ce_pux_controller TSRMLS_CC, 1, ce_pux_expandable_mux);
}

PHP_METHOD(Controller, __construct) {
}


/**
 * Returns [ method, annotations ]
 */
PHP_METHOD(Controller, getActionMethods)
{
    // get function table hash
    HashTable *function_table = &Z_OBJCE_P(this_ptr)->function_table;
    HashPosition pos;

    array_init(return_value);

    zend_function *mptr;
    zend_hash_internal_pointer_reset_ex(function_table, &pos);

    const char *fn;
    size_t fn_len;
    int p;

    while (zend_hash_get_current_data_ex(function_table, (void **) &mptr, &pos) == SUCCESS) {
        fn     = mptr->common.function_name;
        fn_len = strlen(mptr->common.function_name);
        p      = strpos(fn, "Action");

        if ( p == -1 || (size_t)p != (fn_len - strlen("Action"))  ) {
            zend_hash_move_forward_ex(function_table, &pos);
            continue;
        }

        // append the structure [method name, annotations]
        zval *new_item;
        zval *z_method_annotations;
        zval *z_indexed_annotations;

        zval *z_comment;
        zval *z_file;
        zval *z_line_start;

        MAKE_STD_ZVAL(new_item);
        array_init(new_item);
        add_next_index_stringl(new_item, fn, fn_len, 1);

        /* 
         * @var 
         *
         * if there no annotation, we pass an empty array.
         *
         * The method annotation structure
         *
         */
        MAKE_STD_ZVAL(z_method_annotations);
        array_init(z_method_annotations);

        // simplified annotation information is saved to this variable.
        MAKE_STD_ZVAL(z_indexed_annotations);
        array_init(z_indexed_annotations);

        if ( mptr->type == ZEND_USER_FUNCTION && mptr->op_array.doc_comment ) {
            ALLOC_ZVAL(z_comment);
            ZVAL_STRING(z_comment, mptr->op_array.doc_comment, 1);

            ALLOC_ZVAL(z_file);
            ZVAL_STRING(z_file, mptr->op_array.filename, 1);

            ALLOC_ZVAL(z_line_start);
            ZVAL_LONG(z_line_start, mptr->op_array.line_start);

            /*
            zval *z_line_end;
            ALLOC_ZVAL(z_line_end);<commit_msg>use MAKE_STD_ZVAL instead of ZVAL_ALLOC<commit_after>#include "string.h"
#include "ctype.h"
#include "php.h"
#include "main/php_main.h"
#include "Zend/zend_API.h"
#include "Zend/zend_variables.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "Zend/zend_interfaces.h"
#include "zend_object_handlers.h"
#include "ext/pcre/php_pcre.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_var.h"

#include "ct_helper.h"
#include "php_pux.h"
#include "pux_mux.h"
#include "pux_functions.h"
#include "php_expandable_mux.h"
#include "pux_controller.h"

#include "annotation/scanner.h"
#include "annotation/annot.h"

zend_class_entry *ce_pux_controller;

const zend_function_entry controller_methods[] = {
  PHP_ME(Controller, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  PHP_ME(Controller, expand, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, getActionMethods, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, getActionRoutes, NULL, ZEND_ACC_PUBLIC)

  PHP_ME(Controller, before, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, after, NULL, ZEND_ACC_PUBLIC)
  PHP_ME(Controller, toJson, NULL, ZEND_ACC_PUBLIC)

  // PHP_ME(Controller, __destruct,  NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) 
  PHP_FE_END
};

zend_bool phannot_fetch_argument_value(zval **arg, zval** value TSRMLS_DC) {
    zval **expr;
    if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
        return FAILURE;
    }
    return zend_hash_find(Z_ARRVAL_PP(expr), "value", sizeof("value"), (void**) value);
}

zend_bool phannot_fetch_argument_type(zval **arg, zval **type TSRMLS_DC) {
    zval **expr;
    if (zend_hash_find(Z_ARRVAL_PP(arg), "expr", sizeof("expr"), (void**)&expr) == FAILURE ) {
        return FAILURE;
    }
    return zend_hash_find(Z_ARRVAL_PP(expr), "type", sizeof("type"), (void**)type);
}

int strpos(const char *haystack, char *needle)
{
   char *p = strstr(haystack, needle);
   if (p)
      return p - haystack;
   return -1;
}




void pux_init_controller(TSRMLS_D) {
    zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "Pux\\Controller", controller_methods);
    ce_pux_controller = zend_register_internal_class(&ce TSRMLS_CC);
    zend_class_implements(ce_pux_controller TSRMLS_CC, 1, ce_pux_expandable_mux);
}

PHP_METHOD(Controller, __construct) {
}


/**
 * Returns [ method, annotations ]
 */
PHP_METHOD(Controller, getActionMethods)
{
    // Get function table hash from the current object.
    HashTable *function_table = &Z_OBJCE_P(this_ptr)->function_table;
    HashPosition pos;

    array_init(return_value);

    zend_function *mptr;
    zend_hash_internal_pointer_reset_ex(function_table, &pos);

    const char *fn;
    size_t fn_len;
    int p;

    while (zend_hash_get_current_data_ex(function_table, (void **) &mptr, &pos) == SUCCESS) {
        fn     = mptr->common.function_name;
        fn_len = strlen(mptr->common.function_name);
        p      = strpos(fn, "Action");

        if ( p == -1 || (size_t)p != (fn_len - strlen("Action"))  ) {
            zend_hash_move_forward_ex(function_table, &pos);
            continue;
        }

        // append the structure [method name, annotations]
        zval *new_item;
        zval *z_method_annotations;
        zval *z_indexed_annotations;

        zval *z_comment;
        zval *z_file;
        zval *z_line_start;

        MAKE_STD_ZVAL(new_item);
        array_init(new_item);
        add_next_index_stringl(new_item, fn, fn_len, 1);

        /* 
         * @var 
         *
         * if there no annotation, we pass an empty array.
         *
         * The method annotation structure
         *
         */
        MAKE_STD_ZVAL(z_method_annotations);
        array_init(z_method_annotations);

        // simplified annotation information is saved to this variable.
        MAKE_STD_ZVAL(z_indexed_annotations);
        array_init(z_indexed_annotations);

        if ( mptr->type == ZEND_USER_FUNCTION && mptr->op_array.doc_comment ) {
            MAKE_STD_ZVAL(z_comment);
            ZVAL_STRING(z_comment, mptr->op_array.doc_comment, 1);

            MAKE_STD_ZVAL(z_file);
            ZVAL_STRING(z_file, mptr->op_array.filename, 1);

            MAKE_STD_ZVAL(z_line_start);
            ZVAL_LONG(z_line_start, mptr->op_array.line_start);

            /*
            zval *z_line_end;
            MAKE_STD_ZVAL(z_line_end);
{'fraction_non_alphanumeric': 0.11906686684374639, 'fraction_numerical': 0.0013858413211687262, 'mean_word_length': 3.5057232049947973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5407471', 'n_tokens_mistral': 3266, 'n_tokens_neox': 3129, 'n_words': 551}