Skip to content

Instantly share code, notes, and snippets.

@francescozanoni
Last active October 1, 2018 08:25
Show Gist options
  • Save francescozanoni/9abf1c95d3db47fe65f6d42328a67171 to your computer and use it in GitHub Desktop.
Save francescozanoni/9abf1c95d3db47fe65f6d42328a67171 to your computer and use it in GitHub Desktop.
[PHP] print_r() function enhanced to output variables ready to be used in comments
function print_r_for_comments($variable, $return = false)
{

    $output = trim(print_r($variable, true));

    // Pull open round bracket to previous line
    $output = preg_replace('/\r?\n *\(/', ' (', $output);

    // Remove empty lines
    $output = preg_replace('/\r?\n\r?\n/', PHP_EOL, $output);

    // Reset indentation
    $output = preg_replace('/\r?\n */', PHP_EOL, $output);
    $output = explode(PHP_EOL, $output);
    $blanks = [];
    for ($i = 0; $i < count($output); $i++) {
        $output[$i] = implode($blanks) . $output[$i];
        if (preg_match('/(Array|Object) \($/', $output[$i]) === 1) {
            array_push($blanks, '  ');
        }
        if (preg_match('/^ *\)$/', $output[$i]) === 1) {
            array_pop($blanks);
            $output[$i] = implode($blanks) . trim($output[$i]);
        }
    }

    $variableType = gettype($variable);
    if ($variableType === 'object') {
        $variableType = get_class($variable);
    }
    if ($variableType === 'resource (closed)') {
        $variableType = 'resource';
    }
    
    // Wrap into a comment
    for ($i = 0; $i < count($output); $i++) {
        switch ($i) {
            case 0:
                $output[$i] = ' * @param ' . $variableType . ' $data e.g. ' . $output[$i];
                break;
            default:
                $output[$i] = ' *        ' . str_pad('', strlen($variableType)) . '            ' . $output[$i];
        }
    }
    $output = '/**' . PHP_EOL . implode(PHP_EOL, $output) . PHP_EOL . ' */';

    // Return or print, exactly as the original print_r() function
    if ($return === true) {
        return $output;
    }
    echo $output . PHP_EOL;

}

Example:

$exampleObject = new stdClass();
$exampleObject->property = 'property value';

$example = [
    'first' => 'abc',
    'second' => [1, 2, 4],
    'third' => $exampleObject
];

// Simple string
print_r_for_comments($example['first']);
/**
 * @param string $data e.g. abc
 */

// Indexed array
print_r_for_comments($example['second']);
/**
 * @param array $data e.g. Array (
 *                           [0] => 1
 *                           [1] => 2
 *                           [2] => 4
 *                         )
 */

// Object
print_r_for_comments($example['third']);
/**
 * @param stdClass $data e.g. stdClass Object (
 *                              [property] => property value
 *                            )
 */

// Associative array
print_r_for_comments($example);
/**
 * @param array $data e.g. Array (
 *                           [first] => abc
 *                           [second] => Array (
 *                             [0] => 1
 *                             [1] => 2
 *                             [2] => 4
 *                             [3] => 8
 *                           )
 *                           [third] => stdClass Object (
 *                             [property] => property value
 *                           )
 *                         )
 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment