note 103213 added to function.assert
| From: | johncrenshaw at priacta dot com | Date: | Fri, 01 Apr 2011 02:21:20 +0000 |
| Subject: | note 103213 added to function.assert | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178399@lists.php.net to get a copy of this message | ||
Better assert. Returns the assert expression from the file, rather than writing asserts as a string.
Includes related comments, and works with multiline asserts.
<?php
function simple_assert_callback($file, $line, $code)
{
$lines = file ($file);
$start_of_assert = '~(^|[^A-Za-z0-9_])assert($|[^A-Za-z0-9_])~';
$comment_line_regex = '~^\s*(\*|//|/\*)~';
$end_of_statement_regex = '~;\s*$~';
// if this is a multiline assert, the given line may be the end of the
// assert statement. Look backwards until we find something that looks
// like it could be the start of the assert
for ($startline = $line; $startline > 0; $startline--)
{
// see if the line contains the start of the assert
// -1 to convert to 0 indexed
if (preg_match($start_of_assert, $lines[($startline - 1)]))
{
// found the assert function
break;
}
}
// find all comment lines before the assert
for (; $startline > 0; $startline--)
{
// see if the previous line is also a comment
// -1 to convert to 0 indexed
// -1 to look at previous line
if (!preg_match($comment_line_regex, $lines[($startline - 1) - 1]))
{
// No more related comments before this line
break;
}
}
// the assert might be spread across more than one line. Just for fun,
// keep looking until we reach a line that ends with ';'
for ($endline = $line; $endline < count($lines); $endline++)
{
// see if we have the end of the statement yet
// NOTE: This isn't a perfect test, but it's better than nothing.
// -1 to convert to 0 indexed
// +1 to look at next line
if (!preg_match($end_of_statement_regex, $lines[($endline - 1) + 1]))
{
// No more related comments before this line
break;
}
}
// get the lines from start to end
$assertlines = array();
for($a = $startline; $a <= $endline; $a++)
{
// -1 to convert to 0 indexed
$assertlines[$a] = $lines[$a - 1];
}
trigger_error("Assert Failed in ".htmlentities($file)." on line
".htmlentities($line).format_assert_code($assertlines), E_USER_WARNING);
}
// Format however you want. This is just an example.
function format_assert_code($code, $line_start_number = null, $highlight = true)
{
if (is_array($code)) $code = implode("", $code);
if (ereg("^<\?(php)?[^[:graph:]]", $code)) {
$code = highlight_string($code, 1);
} else {
$code = highlight_string("<?php ".$code, 1);
$code = substr($code, strpos($code, '<?php ') +
strlen('<?php '));
}
return '<div style="background: white; border: 1px solid
black;">'.$code."</div>";
}
assert_options(ASSERT_CALLBACK, 'simple_assert_callback');
assert(false); // assert with no preceding comment
// This is an assert test. It should dump the comment
assert(false);
/**
* Another assert test :)
* Doc style comment. Pretty cool
*/
assert(false);
/**
* Multiline assert!!
*/
assert(
false
);
----
Server IP: 69.147.83.197
Probable Submitter: 184.76.248.207
----
Manual Page -- http://www.php.net/manual/en/function.assert.php
Edit -- https://master.php.net/note/edit/103213
Del: integrated -- https://master.php.net/note/delete/103213/integrated
Del: useless -- https://master.php.net/note/delete/103213/useless
Del: bad code -- https://master.php.net/note/delete/103213/bad+code
Del: spam -- https://master.php.net/note/delete/103213/spam
Del: non-english -- https://master.php.net/note/delete/103213/non-english
Del: in docs -- https://master.php.net/note/delete/103213/in+docs
Del: other reasons-- https://master.php.net/note/delete/103213
Reject -- https://master.php.net/note/reject/103213
Search -- https://master.php.net/manage/user-notes.php