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

0

Hello everyone, I have a problem that is as follows, I have a framework developed in the company, this framework has a method that prepares data to be saved correctly in the database, it always expects a string, but at some point I do not know how to do this, but I do not know how to do this, but I do not know how to do this. to scroll through the logs. Here is 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 mysql_real_escape_string ($ value) that does not receive a string, sometimes receives an array.

    
asked by anonymous 08.02.2018 / 14:24

1 answer

1

Use the debug_backtrace function. 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 PHP version 7 (or higher), you can use Type Declarations .

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

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

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

    
08.02.2018 / 14:44