Single Line Comments
ID |
php.single_line_comments |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, style |
Description
Reports single-line comments that use the Perl/shell # style and recommends the // style
instead, keeping comment style consistent across the codebase.
Rationale
PHP accepts both // and to begin a single-line comment, but mixing the two within a project
hurts readability and makes diffs and tooling noisier. The widely-adopted PSR coding conventions
and the bulk of modern PHP code use // exclusively, so a stray comment stands out as an
inconsistency. The […] attribute syntax introduced in PHP 8 and a ! shebang on the first
line of a script are not comments and are left untouched.
<?php
class Worker
{
#[Deprecated] // OK — PHP 8 attribute, not a comment
public int $count = 0;
public function run(): void
{
$x = 1; # set counter // FLAW — # single-line comment
# initialise totals // FLAW — # single-line comment
$y = 2; // ready // OK — already uses // style
}
}