Skip to content

Better handle <?php and ?> tags #138

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

Closed
alexander-akait opened this issue Mar 20, 2018 · 2 comments
Closed

Better handle <?php and ?> tags #138

alexander-akait opened this issue Mar 20, 2018 · 2 comments

Comments

@alexander-akait
Copy link
Collaborator

alexander-akait commented Mar 20, 2018

We have problem when printing statement and inline nodes.
Example:

<?php echo 'test'; ?>
<?php
echo 'test;
echo 'test';
echo 'test';
?>

We need information when php tag opening and closing. Any idea how best we can get this information? Maybe add node.hasOpenTag and node.hasCloseTag (or maybe best names). It is allow output all tags as is.

<?php if (...): ?>
  <div>foo</div>
<?php endif; ?>

But, for control structure it is not good solution 😞


Maybe you can export tags array as comment with position? I think it will be solve our problem 👍

@ichiriac
Copy link
Member

I could provide an option in order to export tokens into an array, so it would be more generic. By joining all tokens you have exactly the same output as the original file.

The implementation is compliant with this function : http://php.net/manual/en/function.token-get-all.php

@ichiriac ichiriac self-assigned this Mar 23, 2018
@ichiriac ichiriac added this to the 3.0.0 milestone Mar 23, 2018
ichiriac added a commit that referenced this issue Mar 25, 2018
ichiriac added a commit that referenced this issue Mar 26, 2018
ichiriac added a commit that referenced this issue Mar 26, 2018
ichiriac added a commit that referenced this issue Mar 26, 2018
@ichiriac
Copy link
Member

Hi @evilebottnawi,

You can try the new tokens extraction here - https://glayzzle.com/php-parser/

Here a sample code that has no nodes, but can be serialized with tokens :

The offsets on tokens are :

  • token name - if it's not defined, it's plain text node
  • text - the token inner text
  • line - the line (keeped from php get_tokens_all)
  • start offset
  • end offset

PHP Script :

<?php /** **/ ?>

AST Structure :

{
  "kind": "program",
  "children": [],
  "errors": [],
  "comments": [
    {
      "kind": "commentblock",
      "value": "/** **/"
    }
  ],
  "tokens": [
    [
      "T_OPEN_TAG",
      "<?php ",
      1,
      0,
      6
    ],
    [
      "T_DOC_COMMENT",
      "/** **/",
      1,
      6,
      13
    ],
    [
      "T_WHITESPACE",
      " ",
      1,
      13,
      14
    ],
    [
      "T_CLOSE_TAG",
      "?>",
      1,
      14,
      16
    ]
  ]
}

ichiriac added a commit that referenced this issue Mar 27, 2018
ichiriac added a commit that referenced this issue Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants