-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow parsing mixed HTML and PHP #234
Conversation
return array_merge( | ||
array_values(Tokens::$emptyTokens), | ||
[ | ||
T_INLINE_HTML, | ||
T_CLOSE_TAG, | ||
] | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return array_merge( | |
array_values(Tokens::$emptyTokens), | |
[ | |
T_INLINE_HTML, | |
T_CLOSE_TAG, | |
] | |
); | |
$empty = Tokens::$emptyTokens; | |
$empty[T_INLINE_HTML] = T_INLINE_HTML; | |
$empty[T_CLOSE_TAG] = T_CLOSE_TAG; | |
return $empty; |
Array functions like array_merge()
and array_values()
are "expensive".
Changing it to the above will be more efficient performance + memory-use-wise.
You could even choose to optimize it further like this:
return array_merge( | |
array_values(Tokens::$emptyTokens), | |
[ | |
T_INLINE_HTML, | |
T_CLOSE_TAG, | |
] | |
); | |
static $empty; | |
if (isset($empty) === false) { | |
$empty = Tokens::$emptyTokens; | |
$empty[T_INLINE_HTML] = T_INLINE_HTML; | |
$empty[T_CLOSE_TAG] = T_CLOSE_TAG; | |
} | |
return $empty; |
It's not as if this compound array will change at any time during the run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When mixing HTML and PHP, we must ignore the HTML tokens and the closing PHP tokens. This PR alters the parsing to consider them "empty" for the sake of all the functions that skip over empty space. Notably, this allows considering a closing HTML tag as the end of the global scope.
Fixes #232