Skip to content

Squiz/EmbeddedPhp: add support for short open tag and various bug fixes #196

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

Merged
merged 25 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
61e20af
Squiz/EmbeddedPhp: remove some redundant code
jrfnl Dec 23, 2023
6875b9e
Squiz/EmbeddedPhp: rename two confusingly named variables
jrfnl Dec 27, 2023
2fc1ca9
Squiz/EmbeddedPhp: make test cases more descriptive
jrfnl Dec 23, 2023
16f4a3a
Squiz/EmbeddedPhp: add some extra tests with ignore annotations
jrfnl Dec 23, 2023
38a90c1
Squiz/EmbeddedPhp: prevent tests being skipped
jrfnl Dec 24, 2023
b2f4d32
Squiz/EmbeddedPhp: empty tag set fixer should remove trailing spaces
jrfnl Dec 24, 2023
77ebd28
Squiz/EmbeddedPhp: opener on own line fixer should remove trailing sp…
jrfnl Dec 24, 2023
9fbbe04
Squiz/EmbeddedPhp: closer on own line fixers should remove trailing s…
jrfnl Dec 24, 2023
bac3be5
Squiz/EmbeddedPhp: bug fix - fixer adds stray new line
jrfnl Dec 24, 2023
b166d65
Squiz/EmbeddedPhp: rename test case file
jrfnl Dec 23, 2023
1e64373
Squiz/EmbeddedPhp: add tests safeguarding that single line embedded P…
jrfnl Dec 26, 2023
3172a21
Squiz/EmbeddedPhp: sniff for short open tags
jrfnl Dec 24, 2023
36e1a8c
Squiz/EmbeddedPhp: add dedicated tests related to ignoring of first o…
jrfnl Dec 26, 2023
c2ee356
Squiz/EmbeddedPhp: efficiency fix - bow out early if there is nothing…
jrfnl Dec 26, 2023
d6f8958
Squiz/EmbeddedPhp: document behaviour for unclosed tag block with con…
jrfnl Dec 27, 2023
f2c3f47
Squiz/EmbeddedPhp: document the behaviour for closed tag block at end…
jrfnl Dec 27, 2023
1016d40
Squiz/EmbeddedPhp: document the behaviour for closed tag block at end…
jrfnl Dec 27, 2023
e9e2011
Squiz/EmbeddedPhp: bug fix - closer on own line indent is not calcula…
jrfnl Dec 27, 2023
efb9e44
Squiz/EmbeddedPhp: add test safeguarding handling of blank lines and …
jrfnl Dec 27, 2023
0fff3b5
Squiz/EmbeddedPhp: bug fix - content intended indent calculation
jrfnl Dec 27, 2023
d1c63af
Squiz/EmbeddedPhp: bug fix - open tag on own line intended indent cal…
jrfnl Dec 27, 2023
b4c1682
Squiz/EmbeddedPhp: bug fix - close tag intended indent calculation
jrfnl Dec 27, 2023
46f7102
Squiz/EmbeddedPhp: bug fix - open tag intended indent calculation
jrfnl Dec 27, 2023
ac5fd07
Squiz/EmbeddedPhp: add some extra defensive coding
jrfnl Dec 27, 2023
50ae189
Squiz/EmbeddedPhp: document the behaviour for closed tag block at end…
jrfnl Dec 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading