Skip to content

Commit 73a6cae

Browse files
authored
Merge pull request #196 from PHPCSStandards/feature/27-squiz-embeddedphp-support-short-open-tag
Squiz/EmbeddedPhp: add support for short open tag and various bug fixes
2 parents c8414c5 + 50ae189 commit 73a6cae

39 files changed

+1613
-336
lines changed

src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php

+176-66
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
<?php
2+
// Not embedded. Do not check here.
3+
echo 'Long open tag test case file';
4+
?>
5+
<html>
6+
<head>
7+
<title><?php echo $title ?></title>
8+
<script><?php echo $script; ?></script>
9+
</head>
10+
<body>
11+
<?php
12+
echo $body;
13+
?>
14+
hello
15+
<?php
16+
echo $moreBody;
17+
?>
18+
<?php echo 'one'; ?>
19+
<?php echo 'two'; ?>
20+
<?php echo 'three;' ?>
21+
<?php echo 'fourA'; echo 'fourB;'; ?>
22+
<?php echo 'fiveA'; echo 'fiveB;'; ?>
23+
24+
<?php
25+
?>
26+
<?php ?>
27+
28+
<?php
29+
30+
echo $start - $indent + $end;
31+
32+
?>
33+
<?php
34+
35+
echo $blankLines;
36+
37+
?>
38+
39+
<?php
40+
echo $closerNeedsOwnLine; ?>
41+
<?php echo $openerNeedsOwnLine;
42+
?>
43+
44+
<?php
45+
echo 'hi';
46+
?>
47+
48+
<?php
49+
echo 'hi';
50+
?>
51+
</body>
52+
</html>
53+
<?php
54+
function test()
55+
{
56+
foreach ($root->section as $section) {
57+
?>
58+
<table>
59+
<?php if ($foo) {
60+
?>
61+
<tr>
62+
</tr>
63+
<?php }
64+
?>
65+
<?php
66+
foreach ($bar as $bar) {
67+
echo $bar;
68+
}
69+
}
70+
71+
foreach ($root->section as $section) {
72+
?>
73+
<table>
74+
<?php
75+
if ($foo) {
76+
?>
77+
<tr>
78+
</tr>
79+
<?php
80+
}
81+
?>
82+
<?php
83+
foreach ($bar as $bar) {
84+
echo $bar;
85+
}
86+
}
87+
}
88+
89+
echo 'goodbye';
90+
91+
function foo()
92+
{
93+
94+
?><a onClick="Javascript: set_hidden_field('<?php echo $link_offset - $num_per_page; ?>'); set_hidden_field('process_form', '0'); submit_form(); return false;"><?php
95+
96+
}
97+
98+
?>
99+
100+
<strong><?php
101+
echo 'foo';
102+
?></strong>
103+
104+
?>
105+
106+
</html>
107+
108+
<?php if ($foo) { ?>
109+
<?php } ?>
110+
111+
<?php echo 'okay'; // Something. ?>
112+
<?php echo 'too much space before close'; // Something. ?>
113+
<?php echo 'no space before close'; // Something.?>
114+
115+
<?php /* translators: okay */ ?>
116+
<?php /* translators: no space before close */?>
117+
<?php /* translators: too much space before close */ ?>
118+
119+
<?php echo 'okay'; // phpcs:ignore Standard.Category.Sniff -- reason. ?>
120+
<?php echo 'too much space before close'; // phpcs:ignore Standard.Category.Sniff -- reason. ?>
121+
<?php echo 'no space before close'; // phpcs:ignore Standard.Category.Sniff -- reason.?>
122+
123+
<!--
124+
Make sure the empty tag set fixer removes the complete line, including indentation for an empty tag set and ensure
125+
that the fixer does not remove too many tokens, like new lines tokens _before_ the affected line
126+
or indentation if there is code _after_ the removed empty tag.
127+
-->
128+
<div><?php ?></div>
129+
<?php ?><?php echo $i; ?>
130+
131+
132+
<?php ?>
133+
134+
<?php if (true) { ?><?php echo $i; ?> <?php ?> <?php } ?>
135+
136+
<div><?php
137+
?></div>
138+
<?php
139+
?><?php echo $i; ?>
140+
141+
142+
<?php
143+
?>
144+
145+
<?php if (true) { ?><?php echo $i; ?> <?php
146+
?> <?php } ?>
147+
148+
<!--
149+
Make sure the "content after opener" fixer does not leave trailing space behind.
150+
-->
151+
<?php echo $openerNeedsOwnLine;
152+
?>
153+
154+
<!--
155+
Make sure the "content before closer" fixer does not leave trailing space behind.
156+
-->
157+
<?php
158+
echo $closerNeedsOwnLine; ?>
159+
160+
<!--
161+
Make sure the "content after closer" fixer does not leave trailing space behind.
162+
-->
163+
<?php
164+
echo $closerNeedsOwnLine;
165+
?> </div>
166+
167+
<?php
168+
echo $closerNeedsOwnLine;
169+
?> <?php echo $i; ?>
170+
171+
<!--
172+
Make sure the fixer does not add stray new lines when there are consecutive PHP blocks.
173+
-->
174+
<?php
175+
// Do something.
176+
?><?php
177+
echo 'embedded';
178+
?> <?php
179+
echo 'embedded';
180+
?><?php
181+
echo 'embedded';
182+
?>
183+
184+
<!--
185+
Safeguard closing tag indent calculation for when the last content on the close tag line is a scope closer.
186+
-->
187+
<?php if (true) { ?>
188+
<?php
189+
} ?>
190+
191+
<!--
192+
Safeguard that blank lines between a PHP tag and a scope closer are not touched as the scope closer may have its own rules.
193+
-->
194+
<?php if ($foo) { ?>
195+
<tr></tr>
196+
<?php
197+
198+
199+
}
200+
?>
201+
202+
<!--
203+
Safeguard correct content indent calculation.
204+
-->
205+
<?php
206+
echo 'indent is correct - first on line for open tag is open tag';
207+
?>
208+
209+
<?php
210+
echo 'indent is correct - first on line for open tag is inline HTML without content';
211+
?>
212+
<div><?php
213+
echo 'indent is correct - first on line for open tag is inline HTML with content';
214+
?><?php
215+
echo 'indent is correct - first on line for open tag is whitespace';
216+
?>
217+
218+
<?php
219+
echo 'indent is incorrect - first on line for open tag is open tag';
220+
?>
221+
222+
<?php
223+
echo 'indent is incorrect - first on line for open tag is inline HTML';
224+
?>
225+
<div><?php
226+
echo 'indent is correct - first on line for open tag is inline HTML with content';
227+
?><?php
228+
echo 'indent is incorrect - first on line for open tag is whitespace';
229+
?>
230+
231+
<!--
232+
Safeguard correct close tag indent calculation.
233+
-->
234+
<?php
235+
/* Single line star comments are not problematic. */ ?>
236+
237+
<?php
238+
/* Subsequent lines of inline slash-star comments
239+
contain the indent in the comment token.
240+
This means the calculated indent would be 0 unless we
241+
walk to the first line in the comment. */ ?>
242+
243+
<!--
244+
Safeguard correct open tag indent calculation when the previous non-empty line contains a comment.
245+
-->
246+
<?php
247+
/* Subsequent lines of inline slash-star comments
248+
contain the indent in the comment token. */ ?>
249+
<?php
250+
echo 'the PHP tag is correctly indented as an indent less than the previous code indent is accepted';
251+
252+
/* Subsequent lines of inline slash-star comments
253+
contain the indent in the comment token. */ ?>
254+
<?php
255+
echo 'the PHP tag is correctly indented';
256+
257+
/* Subsequent lines of inline slash-star comments
258+
contain the indent in the comment token. */ ?>
259+
<?php
260+
echo 'the PHP tag is correctly indented as a diff of 4 is accepted';
261+
262+
/* Subsequent lines of inline slash-star comments
263+
contain the indent in the comment token. */ ?>
264+
<?php
265+
echo 'the PHP tag is incorrectly indented as the indent is more than 4 different from the indent of the previous code';
266+
?>
267+
268+
<?php
269+
// This test case file MUST always end with an unclosed long open PHP tag (with this comment) to prevent
270+
// the tests running into the "last PHP closing tag excepted" condition breaking tests.
271+
// Tests related to that "last PHP closing tag excepted" condition should go in separate files.

0 commit comments

Comments
 (0)