How to identify the file that is calling a php method?

Asked

Viewed 221 times

0

Good morning Guys, I have a problem that is the following, I have a framework developed in the company, in this framework has a method that prepares data to be saved correctly in the database, it always expects a string , but at some point from time to time it receives an array and generates a PHP Warning, how can I identify which file called this method in the context of my application? Because in my environment there are several projects that consume this framework and I can’t identify, there is no tracert to go through the logs. Follow the framework method that appears in the log:

insira o código aqui/**
 * Prepara o valor para ser salvo no banco de dados.
 * Em 2013-06-07 17:07:33 alterei o nome deste método de _to_mysql para normalize_to_mysql
 * e deixe ele público para permitir alterarmos o comportamento local ou em futuras versões.
 *
 * @param   string  $field  Nome do campo.
 * @param   mixed   $value  Valor do campo.
 *
 * @return  string  $value
 */
public function normalize_to_mysql( $field , $value ) {
    // Se for false ou null retornar null direto
    if ( is_null( $value ) || $value === false || $value === "" ) {
        return "NULL" ;
    }

    $properties = $this->_config{"fields"}{$field} ;

    // Tratamento especial para campos de data (date e datetime)
    if ( ( $properties{"type"} == "date" || $properties{"type"} == "datetime" || $properties{"type"} == "time" ) && ! empty( $value ) ) {
        if ( spiffy_types::is_object_date( $value ) ) {
            $object_date = $value ;
        }
        else {
            $object_date = DateTime::createFromFormat( $properties{"input_format"} , $value ) ;
            $date_errors = DateTime::getLastErrors() ;
        }

        if ( spiffy_types::is_object_date( $object_date ) && empty( $date_errors{"warning_count"} ) && empty( $date_errors{"error_count"} ) ) {
            if ( $properties{"type"} == "date" ) {
                $value = $object_date->format( "Y-m-d" ) ;
            }
            else if ( $properties{"type"} == "time" ) {
                $value = $object_date->format( "H:i:s" ) ;
            }
            else {
                $value = $object_date->format( "Y-m-d H:i:s" ) ;
            }
        }
        else {
            $value = "NULL" ;
        }
    }

    return sprintf( "'%s'" , mysql_real_escape_string( $value ) ) ;
}

The error occurs in the mysql_real_escape_string( $value ) that does not receive a string, sometimes it receives an array.

1 answer

1


Use the function debug_backtrace. So you will capture all the step by step that the algorithm did until you get into the file.

var_dump( debug_backtrace() );

/* Imprime na ordem decrescente (de acesso) */
array(2) {
  [0]=>
  array(7) {
    ["file"]=>
    string(32) "/var/www/html/arquivo3.php"
    ["line"]=>
    int(8)
    ["function"]=>
    string(12) "imprimeDebug"
    ["class"]=>
    string(5) "Three"
    ["object"]=>
    object(Three)#2 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(7) {
    ["file"]=>
    string(32) "/var/www/html/arquivo2.php"
    ["line"]=>
    int(6)
    ["function"]=>
    string(5) "teste"
    ["class"]=>
    string(3) "Two"
    ["object"]=>
    object(Two)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}

Or you can use the Exceptions

if (gettype($value) != "string") {
    throw new InvalidArgumentException("Not String");
}

/* Output */
<br />
<b>Fatal error</b>:  Uncaught InvalidArgumentException: Not string in /var/www/html/arquivo4.php:6
Stack trace:
#0 /var/www/html/arquivo3.php(8): Three->imprimeDebug()
#1 /var/www/html/arquivo.php(6): Two->teste()
#2 {main}
  thrown in <b>/var/www/html/arquivo4.php</b> on line <b>6</b><br />

If you use version 7 (or higher) of PHP, you can use Type Statements.

With this you can inform that, in that function/method, you only accept a certain type of variable. Ex:

class Logger
{
    public function writeLog(String $msg)
    {
        /* Code Here */
    }
}

With this, in case you try to pass the value as int, boolean, null etc. PHP 7 will release a Exception.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.