Skip to content
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

Proposal: Support overriding short_open_tag in token_get_all() #9612

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Sep 25, 2022

Details

Add the flags TOKEN_ENABLE_SHORT_OPEN_TAG/TOKEN_DISABLE_SHORT_OPEN_TAG to token_get_all and PhpToken::tokenize()

If both flags are accidentally combined,
TOKEN_DISABLE_SHORT_OPEN_TAG takes precedence.

If neither bit flag is provided, then token_get_all continues to use the value of the system ini setting short_open_tag

Motivation

  • Allow linters/analyzers/IDEs to parse/analyze/lint projects targeting a deployment environment supporting short open tags/no short open tags, regardless of what the user configured locally.
  • Make it more convenient to programmatically convert short open tags to standard <?php tags in migration scripts
  • Allow linters/scripts to easily warn about code with short open tags that would be echoed instead of compiled/run (T_INLINE_HTML) when short tags are disabled
  • In applications/libraries that use token_get_all, avoid platform dependence when running the test suites (in PHP versions including this change). (And make it convenient to test both setting versions in the same test suite)

Implementation details

When preparing to scan tokens from a string (or from a file), this copies constant CG(short_tags) corresponding to the system ini setting short_open_tags to SCNG(short_tags) (scanner globals).
PHP modules such as tokenizer can then conditionally override it after the call to zend_prepare_string_for_scanning (PECLs can also make use of this safely starting)

To handle edge cases such as compilation triggering an error handler which calls token_get_all, zend_save_lexical_state and zend_restore_lexical_state also save/restore short_tags

Other Notes

This deliberately does not deprecate short open tags or change default settings and that is not planned. A previous RFC by another developer aimed to deprecate short open tags entirely but that did not go through.

Details
-------

Add the flags TOKEN_ENABLE_SHORT_OPEN_TAG/TOKEN_DISABLE_SHORT_OPEN_TAG to
`token_get_all` and `PhpToken::tokenize()`

If both flags are accidentally combined,
TOKEN_DISABLE_SHORT_OPEN_TAG takes precedence.

If neither bit flag is provided, then token_get_all continues to use the value
of the system ini setting `short_open_tag`
- https://www.php.net/manual/en/ini.core.php#ini.short-open-tag

Motivation
----------

- Allow linters/analyzers/IDEs to parse/analyze/lint projects targeting a
  deployment environment supporting short open tags/no short open tags,
  regardless of what the user configured locally.
- Make it more convenient to programmatically convert short open tags to
  standard `<?php` tags in migration scripts
- Allow linters/scripts to easily warn about code with short open tags
  that would be echoed instead of compiled/run (T_INLINE_HTML)
  when short tags are disabled
- In applications/libraries that use token_get_all, avoid platform dependence
  when running the test suites (in PHP versions including this change).
  (And make it convenient to test both setting versions in the same test suite)

Implementation details
----------------------

When preparing to scan tokens from a string or from a file, this copies constant
`CG(short_tags)` corresponding to the system ini setting `short_open_tags`
to `SCNG(short_tags)`.
PHP modules such as `tokenizer` can then conditionally override it
after the call to `zend_prepare_string_for_scanning`
(PECLs can also make use of this safely starting)

To handle edge cases such as compilation triggering an error handler
which calls token_get_all, zend_save_lexical_state and
zend_restore_lexical_state also save/restore `short_tags`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant