Skip to content

Commit

Permalink
Merge pull request #196 from PHPCSStandards/feature/27-squiz-embedded…
Browse files Browse the repository at this point in the history
…php-support-short-open-tag

Squiz/EmbeddedPhp: add support for short open tag and various bug fixes
  • Loading branch information
jrfnl committed Jan 19, 2024
2 parents c8414c5 + 50ae189 commit 73a6cae
Show file tree
Hide file tree
Showing 39 changed files with 1,613 additions and 336 deletions.
242 changes: 176 additions & 66 deletions src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php

Large diffs are not rendered by default.

271 changes: 271 additions & 0 deletions src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
<?php
// Not embedded. Do not check here.
echo 'Long open tag test case file';
?>
<html>
<head>
<title><?php echo $title ?></title>
<script><?php echo $script; ?></script>
</head>
<body>
<?php
echo $body;
?>
hello
<?php
echo $moreBody;
?>
<?php echo 'one'; ?>
<?php echo 'two'; ?>
<?php echo 'three;' ?>
<?php echo 'fourA'; echo 'fourB;'; ?>
<?php echo 'fiveA'; echo 'fiveB;'; ?>

<?php
?>
<?php ?>

<?php

echo $start - $indent + $end;

?>
<?php

echo $blankLines;

?>

<?php
echo $closerNeedsOwnLine; ?>
<?php echo $openerNeedsOwnLine;
?>

<?php
echo 'hi';
?>

<?php
echo 'hi';
?>
</body>
</html>
<?php
function test()
{
foreach ($root->section as $section) {
?>
<table>
<?php if ($foo) {
?>
<tr>
</tr>
<?php }
?>
<?php
foreach ($bar as $bar) {
echo $bar;
}
}

foreach ($root->section as $section) {
?>
<table>
<?php
if ($foo) {
?>
<tr>
</tr>
<?php
}
?>
<?php
foreach ($bar as $bar) {
echo $bar;
}
}
}

echo 'goodbye';

function foo()
{

?><a onClick="Javascript: set_hidden_field('<?php echo $link_offset - $num_per_page; ?>'); set_hidden_field('process_form', '0'); submit_form(); return false;"><?php

}

?>

<strong><?php
echo 'foo';
?></strong>

?>

</html>

<?php if ($foo) { ?>
<?php } ?>

<?php echo 'okay'; // Something. ?>
<?php echo 'too much space before close'; // Something. ?>
<?php echo 'no space before close'; // Something.?>

<?php /* translators: okay */ ?>
<?php /* translators: no space before close */?>
<?php /* translators: too much space before close */ ?>

<?php echo 'okay'; // phpcs:ignore Standard.Category.Sniff -- reason. ?>
<?php echo 'too much space before close'; // phpcs:ignore Standard.Category.Sniff -- reason. ?>
<?php echo 'no space before close'; // phpcs:ignore Standard.Category.Sniff -- reason.?>

<!--
Make sure the empty tag set fixer removes the complete line, including indentation for an empty tag set and ensure
that the fixer does not remove too many tokens, like new lines tokens _before_ the affected line
or indentation if there is code _after_ the removed empty tag.
-->
<div><?php ?></div>
<?php ?><?php echo $i; ?>


<?php ?>

<?php if (true) { ?><?php echo $i; ?> <?php ?> <?php } ?>

<div><?php
?></div>
<?php
?><?php echo $i; ?>


<?php
?>

<?php if (true) { ?><?php echo $i; ?> <?php
?> <?php } ?>

<!--
Make sure the "content after opener" fixer does not leave trailing space behind.
-->
<?php echo $openerNeedsOwnLine;
?>

<!--
Make sure the "content before closer" fixer does not leave trailing space behind.
-->
<?php
echo $closerNeedsOwnLine; ?>

<!--
Make sure the "content after closer" fixer does not leave trailing space behind.
-->
<?php
echo $closerNeedsOwnLine;
?> </div>

<?php
echo $closerNeedsOwnLine;
?> <?php echo $i; ?>

<!--
Make sure the fixer does not add stray new lines when there are consecutive PHP blocks.
-->
<?php
// Do something.
?><?php
echo 'embedded';
?> <?php
echo 'embedded';
?><?php
echo 'embedded';
?>

<!--
Safeguard closing tag indent calculation for when the last content on the close tag line is a scope closer.
-->
<?php if (true) { ?>
<?php
} ?>

<!--
Safeguard that blank lines between a PHP tag and a scope closer are not touched as the scope closer may have its own rules.
-->
<?php if ($foo) { ?>
<tr></tr>
<?php


}
?>

<!--
Safeguard correct content indent calculation.
-->
<?php
echo 'indent is correct - first on line for open tag is open tag';
?>

<?php
echo 'indent is correct - first on line for open tag is inline HTML without content';
?>
<div><?php
echo 'indent is correct - first on line for open tag is inline HTML with content';
?><?php
echo 'indent is correct - first on line for open tag is whitespace';
?>

<?php
echo 'indent is incorrect - first on line for open tag is open tag';
?>

<?php
echo 'indent is incorrect - first on line for open tag is inline HTML';
?>
<div><?php
echo 'indent is correct - first on line for open tag is inline HTML with content';
?><?php
echo 'indent is incorrect - first on line for open tag is whitespace';
?>

<!--
Safeguard correct close tag indent calculation.
-->
<?php
/* Single line star comments are not problematic. */ ?>

<?php
/* Subsequent lines of inline slash-star comments
contain the indent in the comment token.
This means the calculated indent would be 0 unless we
walk to the first line in the comment. */ ?>

<!--
Safeguard correct open tag indent calculation when the previous non-empty line contains a comment.
-->
<?php
/* Subsequent lines of inline slash-star comments
contain the indent in the comment token. */ ?>
<?php
echo 'the PHP tag is correctly indented as an indent less than the previous code indent is accepted';

/* Subsequent lines of inline slash-star comments
contain the indent in the comment token. */ ?>
<?php
echo 'the PHP tag is correctly indented';

/* Subsequent lines of inline slash-star comments
contain the indent in the comment token. */ ?>
<?php
echo 'the PHP tag is correctly indented as a diff of 4 is accepted';

/* Subsequent lines of inline slash-star comments
contain the indent in the comment token. */ ?>
<?php
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
?>

<?php
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
// Tests related to that "last PHP closing tag excepted" condition should go in separate files.

0 comments on commit 73a6cae

Please sign in to comment.