1.2.363. Don’t Be Too Manual

Adapt the examples from the PHP manual to the code. Don’t reuse directly the same names in the source: be more specific about what to expect in those variables.

Here are the variables names that are classic with specific functions:

  • for($i = 0; $i < 10; ++$i) {}, $j, $k`

  • catch(`Exception <https://www.php.net/exception>`_ $e)

  • $fp = fopen(`...) <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_, $fh

  • $conn = new `PDO( <https://www.php.net/pdo>`_...):, $dbh, $fh, $conn, $link

  • $stmt = mysql_prepare(`...) <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_, $sth

  • $row = $`pdo <https://www.php.net/pdo>`_->fetchArray(`...) <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_, $`result <https://www.php.net/result>`_, $line, $record

  • preg_match(`... <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_, `... <https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list>`_, $r), $matches`

  • str_contains($haystack, $needle) and strpos

<?php

// Search for phone numbers in a text
preg_match_all('/((\d{3})-(\d{3})-(\d{4}))/', $string, $phoneNumber);

// Search for phone numbers in a text
preg_match_all('/(\d{3})-(\d{3})-(\d{4})/', $string, $matches);

?>

1.2.363.1. Suggestions

  • Use precise and adapted name with your variables

1.2.363.2. Specs

Short name

Structures/DontBeTooManual

Rulesets

All, Coding conventions

Exakat since

1.6.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud