Skip to content
Snippets Groups Projects
Commit 473f3603 authored by Jared Hancock's avatar Jared Hancock
Browse files

Add test to find leading and trailing whitespace

parent b9451064
No related branches found
No related tags found
No related merge requests found
......@@ -100,4 +100,3 @@
<?php }elseif($warn) { ?>
<div id="msg_warning"><?php echo $warn; ?></div>
<?php } ?>
......@@ -62,5 +62,14 @@ class Test {
}
}
function line_number_for_offset($filename, $offset) {
$lines = file($filename);
$bytes = $line = 0;
while ($bytes < $offset) {
$bytes += strlen(array_shift($lines));
$line += 1;
}
return $line;
}
}
?>
<?php
require_once "class.test.php";
class ExtraWhitespace extends Test {
var $name = "PHP Leading and Trailing Whitespace";
function testFindWhitespace() {
foreach ($this->getAllScripts() as $s) {
$matches = array();
if (preg_match_all('/^\s+<\?php|\?>\n\s+$/s',
file_get_contents($s), $matches,
PREG_OFFSET_CAPTURE) > 0) {
foreach ($matches[0] as $match)
$this->fail(
$s, $this->line_number_for_offset($s, $match[1]),
(strpos('?>', $match[0]) !== false)
? 'Leading whitespace'
: 'Trailing whitespace');
}
else $this->pass();
}
}
}
return 'ExtraWhitespace';
?>
......@@ -13,23 +13,12 @@ class ShortOpenTag extends Test {
foreach ($matches[0] as $match)
$this->fail(
$s,
line_number_for_offset($s, $match[1]),
$this->line_number_for_offset($s, $match[1]),
$match[0]);
}
else $this->pass();
}
}
}
function line_number_for_offset($filename, $offset) {
$lines = file($filename);
$bytes = $line = 0;
while ($bytes < $offset) {
$bytes += strlen(array_shift($lines));
$line += 1;
}
return $line;
}
return 'ShortOpenTag';
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment