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

php include_once space inserted in associative array key value #42

Closed
esetnik opened this issue Apr 16, 2018 · 5 comments
Closed

php include_once space inserted in associative array key value #42

esetnik opened this issue Apr 16, 2018 · 5 comments

Comments

@esetnik
Copy link

esetnik commented Apr 16, 2018

When fixing the following example, an extra space is inserted in the key $_SERVER['DOCUMENT_ROOT'] so that it becomes $_SERVER['DOCUMENT_ROOT ']

Example:

<a href='javascript:void(0);' id='<?php echo $DeptID; ?>' onclick='changeDepartment("<?php echo $DeptID; ?>","<?php echo str_replace("'
  ", "\\ ", $DeptName); ?>  ", "<?php echo $DeptShortName; ?>",
  1, "<?php echo $DeptState ?>"); '>
                            <span class="icon-office"></span><?php echo $Pro_DeptName . ' TO '; ?>
                        </a>
                
<!-- ### BEGIN: Help Modal ### -->
<?php include_once $_SERVER['DOCUMENT_ROOT'] . "/shared/helpModal.php";  ?>
<!-- ### END: Help Modal ### -->

Output:

<a href='javascript:void(0);' id='<?php echo $DeptID; ?>' onclick='changeDepartment("<?php echo $DeptID; ?>","<?php echo str_replace("'
  ", "\\ ", $DeptName); ?>  ", "<?php echo $DeptShortName; ?>",
  1, "<?php echo $DeptState ?>"); '>
                            <span class="icon-office"></span><?php echo $Pro_DeptName . ' TO '; ?>
                        </a>
                
<!-- ### BEGIN: Help Modal ### -->
<?php include_once $_SERVER['DOCUMENT_ROOT '] . "/shared/helpModal.php";  ?>
<!-- ### END: Help Modal ### -->
@junstyle
Copy link
Owner

junstyle commented Apr 16, 2018

onclick='changeDepartment("<?php echo $DeptID; ?>","<?php echo str_replace("' ", "\\ ", $DeptName); ?> ", "<?php echo $DeptShortName; ?>", 1, "<?php echo $DeptState ?>"); '

this line has unmatched single quote, maybe js-beautify can't parse correctly. I have no idea.
I think you should avoid too complex tags with php code.
you can do like this:
<?php echo "<a href='javascript:void(0);' id='", $DeptID, "' onclick='changeDepartment(\"", $DeptID, '","', ........

@esetnik
Copy link
Author

esetnik commented Apr 16, 2018

Sorry I don't see the unmatched single quote here

onclick='changeDepartment("<?php echo $DeptID; ?>","<?php echo str_replace("' ", "\\ ", $DeptName); ?> ", "<?php echo $DeptShortName; ?>", 1, "<?php echo $DeptState ?>"); '

This is not really unmatched because when the php is rendered it would be removed. But I can confirm that this single quote the cause of the issue.
<?php echo str_replace("' ", "\\ ", $DeptName); ?>?

Example that does not produce the problem.

<a href='javascript:void(0);' id='<?php echo $DeptID; ?>' onclick='changeDepartment("<?php echo $DeptID; ?>","`<?php echo str_replace("a", "\\ ", $DeptName); ?>`", "<?php echo $DeptShortName; ?>",1,"<?php echo $DeptState ?>");'>
  <span class="icon-office"></span>
  <?php echo $Pro_DeptName . ' TO '; ?>
</a>

<!-- ### BEGIN: Help Modal ### -->
<?php include_once $_SERVER['DOCUMENT_ROOT'] . "/shared/helpModal.php";  ?>
<!-- ### END: Help Modal ### -->

My concern is that php-cs-fixer breaks valid php / html code on save in this case. If this is a js-beautify issue can you help me open an issue with them so that we can get it fixed? I was not aware that php-cs-fixer was using js-beautify for the html beautification, so you might have more detail to provide to them to get this fixed.

@junstyle
Copy link
Owner

single quote in this parts :

str_replace("' ",

because this extension do html format first, and then run php-cs-fixer. so js-beautify can't format the code correctly.

you can test your code here: http://jsbeautifier.org/

<a href='javascript:void(0);' id='<?php echo $DeptID; ?>' onclick='changeDepartment("<?php echo $DeptID; ?>","<?php echo str_replace("'
  ", "\\ ", $DeptName); ?>  ", "<?php echo $DeptShortName; ?>",
  1, "<?php echo $DeptState ?>"); '>
                            <span class="icon-office"></span><?php echo $Pro_DeptName . ' TO '; ?>
                        </a>
                
<!-- ### BEGIN: Help Modal ### -->
<?php include_once $_SERVER['DOCUMENT_ROOT'] . "/shared/helpModal.php";  ?>
<!-- ### END: Help Modal ### -->

@esetnik
Copy link
Author

esetnik commented Apr 16, 2018

Ok, that makes it clear. The issue is that the code we send to js-beautify needs to have php stripped out so that the formatting pass doesn't include code that is valid in php but not valid for js-beautify. Maybe it would be possible for this plugin to strip out code between php tags, then send to js-beautfiy, then reinsert the embedded php.

I did a bit of research and it looks like js-beautify is not officially supporting templated languages like php:
beautifier/js-beautify#688
beautifier/js-beautify#490
beautifier/js-beautify#532

And it looks like there was an attempt to treat php tags as strings, but I guess it doesn't work in this case.

@esetnik
Copy link
Author

esetnik commented Apr 16, 2018

And also something you should track is the newly announced prettier plugin ecosystem, specifically https://github.com/prettier/plugin-php. Although they are currently very early on, maybe it would be viable to switch to using prettier since js-beautify seems to be untenable for html / js beautification of php. Currently prettier html support is not implemented, see prettier/prettier#1882 for progress on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants