Skip to content

Commit

Permalink
parse token instead of regex (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed Oct 23, 2016
1 parent f74b402 commit 9397e18
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 65 deletions.
85 changes: 25 additions & 60 deletions classes/phing/filters/StripPhpComments.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

/*
* $Id$
*
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -18,8 +15,7 @@
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

*/
include_once 'phing/filters/BaseFilterReader.php';
include_once 'phing/filters/ChainableReader.php';

Expand Down Expand Up @@ -55,7 +51,7 @@ class StripPhpComments extends BaseFilterReader implements ChainableReader
* Returns the stream without Php comments.
*
* @param null $len
* @return the resulting stream, or -1
* @return string the resulting stream, or -1
* if the end of the resulting stream has been reached
*
*/
Expand All @@ -66,72 +62,41 @@ public function read($len = null)
if ($buffer === -1) {
return -1;
}
$newStr = '';
$tokens = token_get_all($buffer);

foreach ($tokens as $token) {
if (is_array($token)) {
list($id, $text) = $token;

// This regex replace /* */ and // style comments
$buffer = preg_replace(
'/\/\*[^*]*\*+([^\/*][^*]*\*+)*\/|# [^\n]*|\/\/[^\n]*|("(\\\\.|[^"\\\\])*"|\'(\\\\.|[^\'\\\\])*\'|.[^\/#"\'\\\\]*)/s',
"$2",
$buffer
);
switch ($id) {
case T_COMMENT:
case T_DOC_COMMENT:
// no action on comments
continue 2;

// The regex above is not identical to, but is based on the expression below:
//
// created by Jeffrey Friedl
// and later modified by Fred Curtis.
// s{
// /\* ## Start of /* ... */ comment
// [^*]*\*+ ## Non-* followed by 1-or-more *'s
// (
// [^/*][^*]*\*+
// )* ## 0-or-more things which don't start with /
// ## but do end with '*'
// / ## End of /* ... */ comment
//
// | ## OR various things which aren't comments:
//
// (
// " ## Start of " ... " string
// (
// \\. ## Escaped char
// | ## OR
// [^"\\] ## Non "\
// )*
// " ## End of " ... " string
//
// | ## OR
//
// ' ## Start of ' ... ' string
// (
// \\. ## Escaped char
// | ## OR
// [^'\\] ## Non '\
// )*
// ' ## End of ' ... ' string
//
// | ## OR
//
// . ## Anything other char
// [^/"'\\]* ## Chars which doesn't start a comment, string or escape
// )
// }{$2}gxs;
default:
$newStr .= $text;
continue 2;
}
}
$newStr .= $token;
}

return $buffer;
return $newStr;
}

/*
/**
* Returns the next character in the filtered stream, not including
* Php comments.
*
* @return the next character in the resulting stream, or -1
* @return int the next character in the resulting stream, or -1
* if the end of the resulting stream has been reached
*
* @throws IOException if the underlying stream throws an IOException
* during reading
* @deprecated
*/
/**
* @return int
*/
public function readChar()
{
$ch = -1;
Expand Down Expand Up @@ -186,7 +151,7 @@ public function readChar()
* @internal param A $reader Reader object providing the underlying stream.
* Must not be <code>null</code>.
*
* @return a new filter based on this configuration, but filtering
* @return $this a new filter based on this configuration, but filtering
* the specified reader
*/
public function chain(Reader $reader)
Expand Down
6 changes: 2 additions & 4 deletions test/etc/filters/expected/stripphpcomments.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
echo '/* this is not a comment */';
echo '# this is not a comment either';
echo '// nor is this';
echo 'This is a test';

echo 'This is a test';
echo 'This is yet another test';
echo 'One Final Test';
?>
echo 'One Final Test';
1 change: 0 additions & 1 deletion test/etc/filters/input/stripphpcomments.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment
?>

0 comments on commit 9397e18

Please sign in to comment.