Author: ts Date: Fri Feb 15 20:02:28 2008 New Revision: 7390 Log: - Outsourced variable dumping code into its own tool class ezcDebugVariableDumpTool. - Adjusted test case to this change. - Extracted test object classes from test case file.
Added: trunk/Debug/src/tools/ trunk/Debug/src/tools/dump.php (with props) trunk/Debug/tests/classes/ trunk/Debug/tests/classes/debug_test_dump_extended_object.php (with props) trunk/Debug/tests/classes/debug_test_dump_object.php (with props) Modified: trunk/Debug/design/class_diagram.png trunk/Debug/src/debug_autoload.php trunk/Debug/src/stacktrace/php_iterator.php trunk/Debug/tests/php_stacktrace_iterator_test.php trunk/Debug/tests/suite.php Modified: trunk/Debug/design/class_diagram.png ============================================================================== Binary files - no diff available. Modified: trunk/Debug/src/debug_autoload.php ============================================================================== --- trunk/Debug/src/debug_autoload.php [iso-8859-1] (original) +++ trunk/Debug/src/debug_autoload.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -23,5 +23,6 @@ 'ezcDebugSwitchTimerStruct' => 'Debug/structs/switch_timer.php', 'ezcDebugTimer' => 'Debug/debug_timer.php', 'ezcDebugTimerStruct' => 'Debug/structs/timer.php', + 'ezcDebugVariableDumpTool' => 'Debug/tools/dump.php', ); ?> Modified: trunk/Debug/src/stacktrace/php_iterator.php ============================================================================== --- trunk/Debug/src/stacktrace/php_iterator.php [iso-8859-1] (original) +++ trunk/Debug/src/stacktrace/php_iterator.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -22,10 +22,6 @@ */ class ezcDebugPhpStacktraceIterator { - const MAX_DATA = 512; - - const MAX_CHILDREN = 128; - protected function prepare( $stackTrace ) { return parent::prepare( @@ -85,207 +81,9 @@ $params = array(); foreach ( $args as $arg ) { - $params[] = self::dumpVariable( $arg ); + $params[] = ezcDebugVariableDumpTool::dumpVariable( $arg ); } return $params; - } - - /** - * Returns the string representation of an variable. - * - * Returns the dump of the given variable, respecting the $maxData and - * $maxChildren paramaters when arrays or objects are dumped. - * - * @param mixed $arg - * @param int $maxData - * @param int $maxChildren - * @return string - */ - public static function dumpVariable( $arg, $maxData = self::MAX_DATA, $maxChildren = self::MAX_CHILDREN ) - { - switch ( gettype( $arg ) ) - { - case 'boolean': - return self::cutString( ( $arg ? 'TRUE' : 'FALSE' ), $maxData ); - case 'integer': - case 'double': - return self::cutString( (string) $arg, $maxData ); - case 'string': - return sprintf( - "'%s'", - self::cutString( (string) $arg, $maxData ) - ); - case 'array': - return self::dumpArray( $arg, $maxData, $maxChildren ); - case 'object': - return self::dumpObject( $arg, $maxData, $maxChildren ); - case 'resource': - return self::dumpResource( $arg, $maxData ); - case 'NULL': - return 'NULL'; - default: - return 'unknown type'; - } - } - - /** - * Returns the string representation of an array. - * - * Returns the dump of the given array, respecting the $maxData and - * $maxChildren paramaters. - * - * @param array $arg - * @param int $maxData - * @param int $maxChildren - * @return string - */ - private static function dumpArray( array $arg, $maxData, $maxChildren ) - { - $max = min( count( $arg ), $maxChildren ); - - $results = array(); - reset( $arg ); - for ( $i = 0; $i < $max; ++$i ) - { - $results[] = - self::dumpVariable( key( $arg ), $maxData, $maxChildren ) - . ' => ' - . self::dumpVariable( current( $arg ), $maxData, $maxChildren ); - next( $arg ); - } - - if ( $max < count( $arg ) ) - { - $results[] = '...'; - } - - return sprintf( - 'array (%s)', implode( ', ', $results ) - ); - } - - /** - * Returns the string representation of an object. - * - * Returns the dump of the given object, respecting the $maxData and - * $maxChildren paramaters. - * - * @param object $arg - * @param int $maxData - * @param int $maxChildren - * @return string - */ - private static function dumpObject( $arg, $maxData, $maxChildren ) - { - $refObj = new ReflectionObject( $arg ); - $refProps = $refObj->getProperties(); - - $max = min( - count( $refProps ), - $maxChildren - ); - - $results = array(); - reset( $refProps ); - for( $i = 0; $i < $max; $i++ ) - { - $refProp = current( $refProps ); - $results[] = sprintf( - '%s $%s = %s', - self::getPropertyVisibility( $refProp ), - $refProp->getName(), - self::getPropertyValue( $refProp, $arg ) - ); - next( $refProps ); - } - - return sprintf( - 'class %s { %s }', - $refObj->getName(), - implode( '; ', $results ) - ); - } - - private static function dumpResource( $res, $maxData ) - { - // resource(5) of type (stream) - preg_match( '(Resource id #(?P<id>\d+))', (string) $res, $matches ); - return sprintf( - 'resource(%s) of type (%s)', - $matches['id'], - get_resource_type( $res ) - ); - } - - /** - * Returns the $value cut to $length and padded with '...'. - * - * @param string $value - * @param int $length - * @return string - */ - private static function cutString( $value, $length ) - { - if ( strlen( $value ) > ( $length - 3 ) ) - { - return substr( $value, 0, ( $length - 3 ) ) . '...'; - } - return $value; - } - - /** - * Returns private, protected or public. - * - * Returns the visibility of the given relfected property $refProp as a - * readable string. - * - * @param ReflectionProperty $refProp - * @return string - */ - private static function getPropertyVisibility( ReflectionProperty $refProp ) - { - $info = '%s %s = %s'; - switch ( true ) - { - case $refProp->isPrivate(): - return 'private'; - case $refProp->isProtected(): - return 'protected'; - case $refProp->isPublic(): - default: - return 'public'; - } - } - - /** - * Returns the dumped property value. - * - * Returns the dumped value for the given reflected property ($refProp) on - * the given $obj. Makes use of the ugly array casting hack to determine - * values of private and protected properties. - * - * @param ReflectionProperty $refProp - * @param object $obj - * @return string - */ - private static function getPropertyValue( ReflectionProperty $refProp, $obj ) - { - $value = null; - // @TODO: If we switch to a PHP version where Reflection can access - // protected/private property values, we should change this to the - // correct way. - if ( !$refProp->isPublic() ) - { - $objArr = (array) $obj; - $className = ( $refProp->isProtected() ? '*' : $refProp->getDeclaringClass()->getName() ); - $propName = $refProp->getName(); - $value = $objArr["\0{$className}\0{$propName}"]; - } - else - { - $value = $refProp->getValue( $obj ); - } - return self::dumpVariable( $value ); } } Added: trunk/Debug/src/tools/dump.php ============================================================================== --- trunk/Debug/src/tools/dump.php (added) +++ trunk/Debug/src/tools/dump.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -1,0 +1,241 @@ +<?php +/** + * File containing the ezcDebugVariableDumpTool class. + * + * @package Debug + * @version //autogentag// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Tool class to dump variables in a way similar to Xdebug. + * + * This tool class allows to dump variables similar to the way it is done by + * Xdebug (@link http://xdebug.org). The class is used in the [EMAIL PROTECTED] + * ezcDebugPhpStacktraceIterator} to unify the variable dumps with those + * generated by [EMAIL PROTECTED] ezcDebugXdebugStacktraceIterator}. + * + * @package Debug + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +class ezcDebugVariableDumpTool +{ + const MAX_DATA = 512; + + const MAX_CHILDREN = 128; + + /** + * Returns the string representation of an variable. + * + * Returns the dump of the given variable, respecting the $maxData and + * $maxChildren paramaters when arrays or objects are dumped. + * + * @param mixed $arg + * @param int $maxData + * @param int $maxChildren + * @return string + */ + public static function dumpVariable( $arg, $maxData = self::MAX_DATA, $maxChildren = self::MAX_CHILDREN ) + { + switch ( gettype( $arg ) ) + { + case 'boolean': + return self::cutString( ( $arg ? 'TRUE' : 'FALSE' ), $maxData ); + case 'integer': + case 'double': + return self::cutString( (string) $arg, $maxData ); + case 'string': + return sprintf( + "'%s'", + self::cutString( (string) $arg, $maxData ) + ); + case 'array': + return self::dumpArray( $arg, $maxData, $maxChildren ); + case 'object': + return self::dumpObject( $arg, $maxData, $maxChildren ); + case 'resource': + return self::dumpResource( $arg, $maxData ); + case 'NULL': + return 'NULL'; + default: + return 'unknown type'; + } + } + + /** + * Returns the string representation of an array. + * + * Returns the dump of the given array, respecting the $maxData and + * $maxChildren paramaters. + * + * @param array $arg + * @param int $maxData + * @param int $maxChildren + * @return string + */ + private static function dumpArray( array $arg, $maxData, $maxChildren ) + { + $max = min( count( $arg ), $maxChildren ); + + $results = array(); + reset( $arg ); + for ( $i = 0; $i < $max; ++$i ) + { + $results[] = + self::dumpVariable( key( $arg ), $maxData, $maxChildren ) + . ' => ' + . self::dumpVariable( current( $arg ), $maxData, $maxChildren ); + next( $arg ); + } + + if ( $max < count( $arg ) ) + { + $results[] = '...'; + } + + return sprintf( + 'array (%s)', implode( ', ', $results ) + ); + } + + /** + * Returns the string representation of an object. + * + * Returns the dump of the given object, respecting the $maxData and + * $maxChildren paramaters. + * + * @param object $arg + * @param int $maxData + * @param int $maxChildren + * @return string + */ + private static function dumpObject( $arg, $maxData, $maxChildren ) + { + $refObj = new ReflectionObject( $arg ); + $refProps = $refObj->getProperties(); + + $max = min( + count( $refProps ), + $maxChildren + ); + + $results = array(); + reset( $refProps ); + for( $i = 0; $i < $max; $i++ ) + { + $refProp = current( $refProps ); + $results[] = sprintf( + '%s $%s = %s', + self::getPropertyVisibility( $refProp ), + $refProp->getName(), + self::getPropertyValue( $refProp, $arg ) + ); + next( $refProps ); + } + + return sprintf( + 'class %s { %s }', + $refObj->getName(), + implode( '; ', $results ) + ); + } + + /** + * Returns the string representation of a resource. + * + * Returns the dump of the given resource, respecting the $maxData + * paramater. + * + * @param resource $res + * @param int $maxData + * @return string + */ + private static function dumpResource( $res, $maxData ) + { + // @TODO: Ugly, but necessary. + // 'resource(5) of type (stream)' + preg_match( '(^Resource id #(?P<id>\d+)$)', (string) $res, $matches ); + return sprintf( + 'resource(%s) of type (%s)', + $matches['id'], + get_resource_type( $res ) + ); + } + + /** + * Returns the $value cut to $length and padded with '...'. + * + * @param string $value + * @param int $length + * @return string + */ + private static function cutString( $value, $length ) + { + if ( strlen( $value ) > ( $length - 3 ) ) + { + return substr( $value, 0, ( $length - 3 ) ) . '...'; + } + return $value; + } + + /** + * Returns private, protected or public. + * + * Returns the visibility of the given relfected property $refProp as a + * readable string. + * + * @param ReflectionProperty $refProp + * @return string + */ + private static function getPropertyVisibility( ReflectionProperty $refProp ) + { + $info = '%s %s = %s'; + switch ( true ) + { + case $refProp->isPrivate(): + return 'private'; + case $refProp->isProtected(): + return 'protected'; + case $refProp->isPublic(): + default: + return 'public'; + } + } + + /** + * Returns the dumped property value. + * + * Returns the dumped value for the given reflected property ($refProp) on + * the given $obj. Makes use of the ugly array casting hack to determine + * values of private and protected properties. + * + * @param ReflectionProperty $refProp + * @param object $obj + * @return string + */ + private static function getPropertyValue( ReflectionProperty $refProp, $obj ) + { + $value = null; + // @TODO: If we switch to a PHP version where Reflection can access + // protected/private property values, we should change this to the + // correct way. + if ( !$refProp->isPublic() ) + { + $objArr = (array) $obj; + $className = ( $refProp->isProtected() ? '*' : $refProp->getDeclaringClass()->getName() ); + $propName = $refProp->getName(); + $value = $objArr["\0{$className}\0{$propName}"]; + } + else + { + $value = $refProp->getValue( $obj ); + } + return self::dumpVariable( $value ); + } + +} + +?> Propchange: trunk/Debug/src/tools/dump.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Debug/tests/classes/debug_test_dump_extended_object.php ============================================================================== --- trunk/Debug/tests/classes/debug_test_dump_extended_object.php (added) +++ trunk/Debug/tests/classes/debug_test_dump_extended_object.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -1,0 +1,30 @@ +<?php +/** + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @version //autogentag// + * @filesource + * @package Debug + * @subpackage Tests + */ + +require_once 'debug_test_dump_object.php'; + +class DebugTestDumpExtendedObject extends DebugTestDumpObject +{ + private $extendedPrivate; + + protected $extendedProtected; + + public $extendedPublic; + + public function __construct( $private, $protected, $public, $extendedPrivate, $extendedProtected, $extendedPublic ) + { + $this->extendedPrivate = $extendedPrivate; + $this->extendedProtected = $extendedProtected; + $this->extendedPublic = $extendedPublic; + parent::__construct( $private, $protected, $public ); + } +} + +?> Propchange: trunk/Debug/tests/classes/debug_test_dump_extended_object.php ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Debug/tests/classes/debug_test_dump_object.php ============================================================================== --- trunk/Debug/tests/classes/debug_test_dump_object.php (added) +++ trunk/Debug/tests/classes/debug_test_dump_object.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -1,0 +1,27 @@ +<?php +/** + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @version //autogentag// + * @filesource + * @package Debug + * @subpackage Tests + */ + +class DebugTestDumpObject +{ + private $private; + + protected $protected; + + public $public; + + public function __construct( $private, $protected, $public ) + { + $this->private = $private; + $this->protected = $protected; + $this->public = $public; + } +} + +?> Propchange: trunk/Debug/tests/classes/debug_test_dump_object.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/Debug/tests/php_stacktrace_iterator_test.php ============================================================================== --- trunk/Debug/tests/php_stacktrace_iterator_test.php [iso-8859-1] (original) +++ trunk/Debug/tests/php_stacktrace_iterator_test.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -8,39 +8,7 @@ * @subpackage Tests */ -class DebugTestDumpObject -{ - private $private; - - protected $protected; - - public $public; - - public function __construct( $private, $protected, $public ) - { - $this->private = $private; - $this->protected = $protected; - $this->public = $public; - } -} - -class DebugTestDumpObjectExtended extends DebugTestDumpObject -{ - private $extendedPrivate; - - protected $extendedProtected; - - public $extendedPublic; - - public function __construct( $private, $protected, $public, $extendedPrivate, $extendedProtected, $extendedPublic ) - { - $this->extendedPrivate = $extendedPrivate; - $this->extendedProtected = $extendedProtected; - $this->extendedPublic = $extendedPublic; - parent::__construct( $private, $protected, $public ); - } -} - +require_once 'classes/debug_test_dump_extended_object.php'; /** * Test suite for the ezcDebugOptions class. @@ -48,7 +16,7 @@ * @package Debug * @subpackage Tests */ -class ezcDebugPhpStacktraceIteratorTest extends ezcTestCase +class ezcDebugVariableDumpToolTest extends ezcTestCase { public static function suite() { @@ -62,11 +30,11 @@ $this->assertEquals( 'TRUE', - ezcDebugPhpStacktraceIterator::dumpVariable( $true ) + ezcDebugVariableDumpTool::dumpVariable( $true ) ); $this->assertEquals( 'FALSE', - ezcDebugPhpStacktraceIterator::dumpVariable( $false ) + ezcDebugVariableDumpTool::dumpVariable( $false ) ); } @@ -78,15 +46,15 @@ $this->assertEquals( '0', - ezcDebugPhpStacktraceIterator::dumpVariable( $a ) + ezcDebugVariableDumpTool::dumpVariable( $a ) ); $this->assertEquals( '23', - ezcDebugPhpStacktraceIterator::dumpVariable( $b ) + ezcDebugVariableDumpTool::dumpVariable( $b ) ); $this->assertEquals( '-42', - ezcDebugPhpStacktraceIterator::dumpVariable( $c ) + ezcDebugVariableDumpTool::dumpVariable( $c ) ); } @@ -100,23 +68,23 @@ $this->assertEquals( '0', - ezcDebugPhpStacktraceIterator::dumpVariable( $a ) + ezcDebugVariableDumpTool::dumpVariable( $a ) ); $this->assertEquals( '23', - ezcDebugPhpStacktraceIterator::dumpVariable( $b ) + ezcDebugVariableDumpTool::dumpVariable( $b ) ); $this->assertEquals( '-42', - ezcDebugPhpStacktraceIterator::dumpVariable( $c ) + ezcDebugVariableDumpTool::dumpVariable( $c ) ); $this->assertEquals( '23.42', - ezcDebugPhpStacktraceIterator::dumpVariable( $d ) + ezcDebugVariableDumpTool::dumpVariable( $d ) ); $this->assertEquals( '-42.23', - ezcDebugPhpStacktraceIterator::dumpVariable( $e ) + ezcDebugVariableDumpTool::dumpVariable( $e ) ); } @@ -127,11 +95,11 @@ $this->assertEquals( "'foo'", - ezcDebugPhpStacktraceIterator::dumpVariable( $a ) + ezcDebugVariableDumpTool::dumpVariable( $a ) ); $this->assertEquals( "''", - ezcDebugPhpStacktraceIterator::dumpVariable( $b ) + ezcDebugVariableDumpTool::dumpVariable( $b ) ); } @@ -146,7 +114,7 @@ EOT; $this->assertEquals( $res, - ezcDebugPhpStacktraceIterator::dumpVariable( $arr ) + ezcDebugVariableDumpTool::dumpVariable( $arr ) ); } @@ -173,7 +141,7 @@ EOT; $this->assertEquals( $res, - ezcDebugPhpStacktraceIterator::dumpVariable( $arr ) + ezcDebugVariableDumpTool::dumpVariable( $arr ) ); } @@ -187,27 +155,27 @@ $this->assertEquals( $res, - ezcDebugPhpStacktraceIterator::dumpVariable( $obj ) + ezcDebugVariableDumpTool::dumpVariable( $obj ) ); } public function testDumpExtendedObject() { - $obj = new DebugTestDumpObjectExtended( 23, 42.23, 'foo bar baz', 42, true, false ); - - $res = <<<EOT -class DebugTestDumpObjectExtended { private \$extendedPrivate = 42; protected \$extendedProtected = TRUE; public \$extendedPublic = FALSE; protected \$protected = 42.23; public \$public = 'foo bar baz' } -EOT; - - $this->assertEquals( - $res, - ezcDebugPhpStacktraceIterator::dumpVariable( $obj ) + $obj = new DebugTestDumpExtendedObject( 23, 42.23, 'foo bar baz', 42, true, false ); + + $res = <<<EOT +class DebugTestDumpExtendedObject { private \$extendedPrivate = 42; protected \$extendedProtected = TRUE; public \$extendedPublic = FALSE; protected \$protected = 42.23; public \$public = 'foo bar baz' } +EOT; + + $this->assertEquals( + $res, + ezcDebugVariableDumpTool::dumpVariable( $obj ) ); } public function testDumpExtendedObjectComplex() { - $obj = new DebugTestDumpObjectExtended( + $obj = new DebugTestDumpExtendedObject( array(), 42.23, 'foo bar baz', @@ -227,7 +195,7 @@ true, array( new DebugTestDumpObject( - new DebugTestDumpObjectExtended( 1, 2, 3, 4, 5, 6 ), + new DebugTestDumpExtendedObject( 1, 2, 3, 4, 5, 6 ), array( true, false, 'string' ), new DebugTestDumpObject( 'a', 2, 'c' ) ), @@ -235,12 +203,12 @@ ); $res = <<<EOT -class DebugTestDumpObjectExtended { private \$extendedPrivate = array (0 => 1, 1 => 2, 2 => array (0 => class stdClass { }, 1 => 'some text', 2 => class stdClass { }, 3 => 23, 4 => array (0 => NULL, 1 => NULL, 2 => TRUE, 3 => FALSE, 4 => NULL, 5 => 23)), 3 => 3, 4 => 4); protected \$extendedProtected = TRUE; public \$extendedPublic = array (0 => class DebugTestDumpObject { private \$private = class DebugTestDumpObjectExtended { private \$extendedPrivate = 4; protected \$extendedProtected = 5; public \$extendedPublic = 6; protected \$protected = 2; public \$public = 3 }; protected \$protected = array (0 => TRUE, 1 => FALSE, 2 => 'string'); public \$public = class DebugTestDumpObject { private \$private = 'a'; protected \$protected = 2; public \$public = 'c' } }); protected \$protected = 42.23; public \$public = 'foo bar baz' } -EOT; - - $this->assertEquals( - $res, - ezcDebugPhpStacktraceIterator::dumpVariable( $obj ) +class DebugTestDumpExtendedObject { private \$extendedPrivate = array (0 => 1, 1 => 2, 2 => array (0 => class stdClass { }, 1 => 'some text', 2 => class stdClass { }, 3 => 23, 4 => array (0 => NULL, 1 => NULL, 2 => TRUE, 3 => FALSE, 4 => NULL, 5 => 23)), 3 => 3, 4 => 4); protected \$extendedProtected = TRUE; public \$extendedPublic = array (0 => class DebugTestDumpObject { private \$private = class DebugTestDumpExtendedObject { private \$extendedPrivate = 4; protected \$extendedProtected = 5; public \$extendedPublic = 6; protected \$protected = 2; public \$public = 3 }; protected \$protected = array (0 => TRUE, 1 => FALSE, 2 => 'string'); public \$public = class DebugTestDumpObject { private \$private = 'a'; protected \$protected = 2; public \$public = 'c' } }); protected \$protected = 42.23; public \$public = 'foo bar baz' } +EOT; + + $this->assertEquals( + $res, + ezcDebugVariableDumpTool::dumpVariable( $obj ) ); } @@ -252,7 +220,7 @@ $this->assertEquals( "resource({$matches['id']}) of type (stream)", - ezcDebugPhpStacktraceIterator::dumpVariable( $res ) + ezcDebugVariableDumpTool::dumpVariable( $res ) ); } } Modified: trunk/Debug/tests/suite.php ============================================================================== --- trunk/Debug/tests/suite.php [iso-8859-1] (original) +++ trunk/Debug/tests/suite.php [iso-8859-1] Fri Feb 15 20:02:28 2008 @@ -37,7 +37,7 @@ $this->addTest( ezcDebugTest::suite() ); $this->addTest( ezcDebugOptionsTest::suite() ); $this->addTest( ezcDebugHtmlFormatterTest::suite() ); - $this->addTest( ezcDebugPhpStacktraceIteratorTest::suite() ); + $this->addTest( ezcDebugVariableDumpToolTest::suite() ); } public static function suite() -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components