Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
debug output somewhat imprecise

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Thu Oct 26, 2006 2:01 pm    Post subject: debug output somewhat imprecise Reply with quote

Testing:
Code:
<?php
require_once 'Smarty.class.php';

/**
 * @global Smarty $smarty
 */
$smarty = new Smarty;
$smarty->template_dir   = '/www/kfp-online/templates/';
$smarty->compile_dir    = '/www/kfp-online/templates_c/';
$smarty->config_dir     = '/www/kfp-online/configs/';
$smarty->cache_dir      = '/www/kfp-online/cache/';

$smarty->debug = true;

$smarty->assign('bool_true', true);
$smarty->assign('bool_false', false);
$smarty->assign('string_empty', '');
$smarty->assign('null', null);
$smarty->assign('int_zero', 0);
$smarty->assign('string_zero', "0");

$smarty->display('debug.tpl');
?>


debug window output:
Code:
{$SCRIPT_NAME}  /office/test.php
{$bool_false}   
{$bool_true}    1
{$int_zero}     0
{$null}         [i]empty[/i]
{$string_empty} [i]empty[/i]
{$string_zero}  0


possible solution:
Code:
function smarty_modifier_debug_print_var($var, $depth = 0, $length = 40)
{
    $_replace = array(
        "\n" => '<i>\n</i>',
        "\r" => '<i>\r</i>',
        "\t" => '<i>\t</i>'
    );

    switch (gettype($var)) {
        case 'array' :
            $results = '<b>Array (' . count($var) . ')</b>';
            foreach ($var as $curr_key => $curr_val) {
                $return = smarty_modifier_debug_print_var($curr_val, $depth + 1, $length);
                $results .= '<br>' . str_repeat('&nbsp;', $depth*2) . '<b>'
                         . strtr($curr_key, $_replace) . '</b> =&gt; ' . $return;
            }
            break;
        case 'object' :
            $object_vars = get_object_vars($var);
            $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
            foreach ($var as $curr_key => $curr_val) {
                $return = smarty_modifier_debug_print_var($curr_val, $depth + 1, $length);
                $results .= '<br>' . str_repeat('&nbsp;', $depth*2) . '<b>'
                         . strtr($curr_key, $_replace) . '</b> =&gt; ' . $return;
            }
            break;
        case 'boolean' :
        case 'NULL' :
        case 'resource' :
            if (true === $var) {
                $results = 'true';
            } elseif (false === $var) {
                $results = 'false';
            } elseif (null === $var) {
                $results = 'null';
            } else {
                $results = htmlspecialchars((string) $var);
            }
            $results = '<i>' . $results . '</i>';
            break;
        case 'integer' :
        case 'float' :
            $results = htmlspecialchars((string) $var);
            break;
        case 'string' :
            $results = strtr($var, $_replace);
            if (strlen($var) > $length ) {
                $results = substr($var, 0, $length - 3) . '...';
            }
            $results = htmlspecialchars('"' . $results . '"');
            break;
        case 'unknown type' :
        default :
            $results = strtr((string) $var, $_replace);
            if (strlen($results) > $length ) {
                $results = substr($results, 0, $length - 3) . '...';
            }
            $results = htmlspecialchars($results);
    }

    return $results;
}


better output:
Code:
{$SCRIPT_NAME}  "/office/test.php"
{$bool_false}   [i]false[/i]
{$bool_true}    [i]true[/i]
{$int_zero}     0
{$null}         [i]null[/i]
{$string_empty} ""
{$string_zero}  "0"
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Fri Oct 27, 2006 8:02 am    Post subject: Reply with quote

Tbh it's no where near a bug, it is just a matter of personal taste.

You say that when a boolean is set to false it shows nothing, which is correct. a boolean is just an enum of 1 or 0. If a variable is null it can also in fact be 0 or "".

I do agree on the string_zero, it could be confusing. But then again others disagree.
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Fri Oct 27, 2006 8:21 am    Post subject: Reply with quote

Hielke Hoeve wrote:
Tbh it's no where near a bug, it is just a matter of personal taste.


of course, but it could help alot if you see more clear what your variables are.


Hielke Hoeve wrote:
You say that when a boolean is set to false it shows nothing, which is correct.


what is correct, what i said or the behavior?
the behavior is of course like above a matter of taste

Hielke Hoeve wrote:
a boolean is just an enum of 1 or 0. If a variable is null it can also in fact be 0 or "".


if a variable is null it can not be 0 or "" or "0" or false, in a loose comparison with '==' this are all equal, but this says not that null can be empty, it is not the same empty is empty and null is undefined

gettype() or '===' will never tell you that en empty variable is null

displaying null instead of empty can help to see that i have assigned an undefined variable by accident

the same goes to all other (1 instead of true, '' instead of false, 0 instead of '0'), it can help me alot to track down wrong assigned variables or return values.

at least following your argument false should also be displayed as empty and not just nothing
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Fri Oct 27, 2006 8:24 am    Post subject: Reply with quote

And if i use a debug output it is not interesting 'HOW' a variable CAN BE interpreted, it is more interesting WHAT a variable IS.
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Fri Oct 27, 2006 8:57 am    Post subject: Reply with quote

cybot wrote:
at least following your argument false should also be displayed as empty and not just nothing


I agree, showing nothing is confusing. Samen as the string_zero, it should be quoted.

cybot wrote:
if a variable is null it can not be 0 or "" or "0" or false, in a loose comparison with '==' this are all equal.

Exactly. Since it is a loose comparison it can be either of the 3.

If you agree on that it is not a bug, then why post in the Bugs forum Wink
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Fri Oct 27, 2006 9:22 am    Post subject: Reply with quote

i do not agree that this is not a bug, the debug output presents me wrong or at least ambiguous information about variables

second not all cases (of variable types) are handled correctly and could lead to Notices or even warnings
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5608
Location: Toronto, Canada

PostPosted: Fri Oct 27, 2006 3:40 pm    Post subject: Reply with quote

IMHO, cybot's representation in more reasonable.

@Cybot: please consider submiting your proposed changes as a patch against CVS for testing.
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Mon Oct 30, 2006 8:54 am    Post subject: Reply with quote

http://www.sebastianmendel.de/smarty/libs_plugins_modifier.debug_print_var.php.patch.txt
Back to top
View user's profile Send private message
cybot
Smarty Regular


Joined: 20 Apr 2005
Posts: 83

PostPosted: Thu Nov 02, 2006 7:14 am    Post subject: Reply with quote

btw. is_object() returns false on an incomplete object (object without class definition) but produces a FATAL error when casted to string - this can happen with the current code, and is fixed with my patch - gettype() returns 'object' also for incomplete objects

http://php.net/is_object

"Note: This function will return FALSE if used on an unserialized object where the class definition is not present (even though gettype() returns object)."


@Hielke Hoeve: now it is definitely a bug! Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5608
Location: Toronto, Canada

PostPosted: Tue Nov 07, 2006 8:33 pm    Post subject: Reply with quote

Committed in CVS.

Thanks cybot!
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Wed Nov 08, 2006 8:41 am    Post subject: Reply with quote

I never disagreed that the output was odd, just said that I didn't see it as a bug. Eitherway happy to see better output Razz
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5608
Location: Toronto, Canada

PostPosted: Mon Nov 20, 2006 9:08 pm    Post subject: Reply with quote

I just committed an additional patch to fix the depth formatting for arrays and objects. Also, I changed the notation for objects slightly.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP