Files and PHP tags¶
Rules for the bytes of a file and for its open and close tags.
drupal/byte-order-mark¶
- Level: error
- Fix: none
- Source: Coder's
Drupalstandard - Ports:
Generic.Files.ByteOrderMark.Found
Coder asks for no byte order mark at the start of a file. PHP sends the mark to the browser before any code runs.
Reports:
- a UTF-8 or UTF-16 byte order mark at the first bytes of a file, once per file.
Not reported:
- a mark further in.
There isn't a fix, because removing the mark changes what the file sends.
drupal/file-comment and drupal/missing-file-comment skip a byte order mark before the open tag,
so a procedural file with a mark and a correct docblock gets only this rule's report.
drupal/closing-tag-statement¶
- Level: error
- Fix: safe. It writes the
;and removes the tag. A statement with a comment between it and the tag doesn't get one. - Standard: PHP code tags
- Ports:
Zend.Files.ClosingTag.NotAllowed
Note that as of Drupal 4.7, the
?>at the end of code files is purposely omitted.
Reports:
- a
?>at the end of a file that ends the last statement in place of a;, as in$a = 1 ?>orendif ?>.
Not reported:
- a
?>after a complete statement, which is reported by Mago'sno-closing-tagand removed bymago format; - a
.tpl.phptemplate, which is skipped by Coder's ruleset.
Example
// Reported.
$settings['example'] = TRUE
?>
// Fine.
$settings['example'] = TRUE;
drupal/empty-php-tags¶
- Level: warning
- Fix: safe. It removes the pair and the line break that PHP drops after
?>, so the output stays the same. An empty<?= ?>doesn't have a fix, because PHP rejects it. - Source: Coder's
Drupalstandard - Ports:
Generic.CodeAnalysis.EmptyPHPStatement.EmptyPHPOpenCloseTagsDetected
Coder asks for no empty pair of PHP tags.
Reports:
- a
<?phpor<?=tag that a?>follows with only whitespace between, as in<?php ?>, in any position, also inside a function or an alternative-syntax block.
Not reported:
- a pair with a comment between the tags.
Compared with Coder
- The sniff has a second code, for a stray
;. It stays with Mago'sno-noop, which reports the same spot. - A pair at the end of a file is reported as in Coder. There Mago's
no-closing-tagalso reports the?>, asZend.Files.ClosingTagdoes in Coder.
drupal/file-encoding¶
- Level: warning
- Fix: none
- Source: Coder's
Drupalstandard - Ports:
Drupal.Files.FileEncoding.InvalidEncoding
Coder asks for files in UTF-8.
Reports:
- a file that isn't valid UTF-8. The report sits on the first open tag or run of inline text, not on
the bad byte, and the message doesn't name any bytes. The text that PHP drops after a
?>doesn't count as inline text.
Not reported:
- a file whose only tags are
<?=.
Compared with Coder
- The rule checks UTF-8 with a PCRE pattern, where Coder calls
mb_check_encoding(). - Coder has an
allowedEncodingsoption. The rule doesn't have one.
drupal/file-start-whitespace¶
- Level: error
- Fix: potentially unsafe. It deletes the whitespace, which a template can print.
- Source: Coder's
Drupalstandard - Ports:
Squiz.WhiteSpace.SuperfluousWhitespace.StartFile
Coder asks for no whitespace before the first <?php of a file.
Reports:
- whitespace before the first
<?phpof a file. Whitespace is ASCII whitespace and Unicode separators, Coder's pattern.
Not reported:
- text that isn't whitespace, such as a byte order mark, a zero-width space or a
#!line; - whitespace before a later open tag of the file.
Compared with Coder
- Coder's fix is safe. This fix is potentially unsafe, because PHP sends the removed text as output.
drupal/short-echo-tag¶
- Level: error
- Fix: safe. It writes
<?php echowith one space before the value, and keeps a line break and any comment where they are. - Source: Coder's
Drupalstandard - Ports:
Generic.PHP.DisallowShortOpenTag.EchoFound
Coder asks for <?php echo in place of the <?= short echo tag.
Reports:
- a
<?=tag that has a value to echo.
Not reported:
- an echo tag with no value, which is left to
drupal/empty-php-tags.
Compared with Coder
- Mago's
no-short-opening-tagcovers the<?tag, so the other two codes of the sniff aren't ported. - Coder's fix keeps the whitespace after the tag as it is, such as two spaces, a tab or a line break. This fix turns spaces and tabs after the tag into one space.
- Mago stops parsing a file that ends right after a bare
<?=, so the rules don't see that file. Coder reports it, and its fixer crashes on it.
drupal/tab-character¶
- Level: error
- Fix: safe in a comment, potentially unsafe in HTML. It writes 4 spaces for each tab.
- Standard: Indenting and Whitespace
- Ports:
Generic.WhiteSpace.DisallowTabIndent:NonIndentTabsUsed,TabsUsed
Use an indent of 2 spaces, with no tabs.
mago format writes spaces in the code and in the indent of a comment's lines. It prints the text
of a comment and the HTML outside the PHP tags as written, so the rule reports the tabs left there.
Reports:
- a tab in the text of a comment or a docblock, once per line;
- a tab in the HTML outside the PHP tags, once per line.
Not reported:
- a tab in the whitespace at the start of a comment's line, which
mago formatreplaces; - a tab in a string or a heredoc, as in Coder;
- a
.ymlfile, which Mago reads as text outside PHP tags. Coder doesn't report tabs there.
The fix in HTML is potentially unsafe, because the tabs are part of the page output, as in a
<pre> element or a plain-text mail body.
Example
The fix turns the reported code into the fine code. The gap after Net: in the reported comment
is a tab.
// Reported.
function example_total(array $prices): int {
// Net: the price before tax.
return array_sum($prices);
}
// Fine.
function example_total(array $prices): int {
// Net: the price before tax.
return array_sum($prices);
}
Compared with Coder
- Coder also reports a tab in the whitespace of the code and at the start of a comment's lines.
mago formatreplaces those. - A tab that becomes 4 spaces can push a comment line past 80 characters, which
drupal/comment-line-lengththen reports. phpcbf does the same. - The fix also changes a tab in an
@codesample, which can show output that holds a tab. mago formatcopies the whitespace before an<?phptag in HTML onto the code after the tag, up to the next?>. A tab there comes out in front of each statement, and Coder reports those lines. The fix replaces the tab in the HTML, and the nextmago formatwrites spaces in front of the code.