Skip to content

Instantly share code, notes, and snippets.

@martinbean
Last active December 19, 2023 22:14
Show Gist options
  • Save martinbean/0eb43b1ba647bca18064 to your computer and use it in GitHub Desktop.
Save martinbean/0eb43b1ba647bca18064 to your computer and use it in GitHub Desktop.
Pre-commit hook to detect if any .php files contain dd()
#!/usr/bin/php
<?php
$files = shell_exec('git diff-index --name-only --cached --diff-filter=ACMR HEAD | grep "\.php$"');
$files = explode("\n", trim($files));
$exitCode = 0;
foreach ($files as $file) {
if (empty($file)) {
continue;
}
$lines = file($file);
foreach ($lines as $line => $content) {
if (preg_match('/\bdd\(/', $content)) {
printf("\033[0;31mdd() found on line %d in %s\033[0m\n", $line + 1, $file);
$exitCode = 1;
}
}
}
exit($exitCode);
@martinbean
Copy link
Author

@it-can Thanks. Added that, as well as a check for a whitespace character before dd() to avoid it matching names like add().

@aaemnnosttv
Copy link

Cool idea. It would be better to prefix with a word-break \b instead of a whitespace \s.

You may also want to ignore matches which come after a line comment //

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment