1

Would it be possible for my local PHPCS standard to warn me when I use certain PHP functions like kint() or ksm()? It's not part of the Drupal Standard but I'm running a modified version with some improvements like this.

1
  • 1
    Create a custom ruleset and pass the filename to phpcs with --standard=custom_ruleset.xml. <?xml version="1.0"?> <ruleset name="Custom"> <rule ref="Generic.PHP.ForbiddenFunctions"> <properties> <property name="forbiddenFunctions" type="array"> <element key="kint" value="null"/> <element key="ksm" value="null"/> </property> </properties> </rule> </ruleset>
    – mbomb007
    Commented May 27, 2022 at 15:57

2 Answers 2

0

Yes, it is possible by Checking Drupal coding standards. You can configure PHP code sniffer to check for Drupal coding standards by following the instructions from https://www.drupal.org/docs/contributed-modules/code-review-module/installing-coder-sniffer and https://www.drupal.org/node/1587138

Edit: It turns out you can not identify all the debugging functions with standard Drupal Coding standard functions.In my testing drupalcs was able to identify only dpm()and dsm() functions but not ksm() or kint() .

There is an alternative way to identify debugging functions. You can add pre-commit hook to identify the debugging functions.

You can add the below script to git hooks.

#!/bin/sh

#
# Git pre-commit hook that prevents code that is not Drupal coding standard compliant of being committed.
#
# Fork of https://git.io/fNXBi by Dave Hall http://davehall.com.au
#

# Fetch the list of files that have changed.
FILES="$(git diff --staged --name-only HEAD --diff-filter=ACMRUXB)"

echo "Running PHP lint check" >&2
LINT_FAIL=0
for filename in $FILES; do
  if ! php -l $filename; then
    LINT_FAIL=1
  fi
done;

# There is no point in running PHP Code Sniffer on broken files.
if [ $LINT_FAIL -gt 0 ]; then
  echo "Commit aborted. Fix your code. Then 'git add' and 'git commit' again."
  exit 1
fi

echo "Running PHP Code Sniffer Checks" >&2
echo $FILES | xargs phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt --ignore=node_modules,bower_components,vendor,*.md
if [ $? -eq 0 ]; then
 # Exit executing the git commit.
 exit 0
else
  # Ask what to do, not executing the commit. Source: https://unix.stackexchange.com/a/121255/139407
  read -p "Fix the marked Sniff violations automatically [Y/n]?" line </dev/tty
  case "$line" in
    y|Y) echo $FILES | xargs phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt --ignore=node_modules,bower_components,vendor,*.md
      ;;
    *) echo "No automatic corrections made."
      ;;
  esac

  echo "Commit aborted. Make remaining manual code fixes if necessary. Then 'git add' and 'git commit' again."
  exit 1
fi

Source : Drupal git pre-commit hook

debug functions list

git pre commit hook

Drupal coding standards

7
  • Yes? The code sniffer for Drupal coding standards checks for the existence of debugging functions in the code?
    – leymannx
    Commented Aug 5, 2020 at 0:22
  • Yes , I will warn you with a message saying usage of dpm/ksm is discouraged. Commented Aug 5, 2020 at 0:25
  • Will edit my answer with a screenshot once I am at PC. Commented Aug 5, 2020 at 0:26
  • Cool, thank you for clarifying! 💙
    – leymannx
    Commented Aug 5, 2020 at 9:19
  • If the Drupal standard can detect dpm(), then we should be able to modify it to also detect ksm(), no? I'm just having trouble finding documentation on how to identify functions. I believe it's a PHP class that needs to extended...
    – atwixtor
    Commented Aug 5, 2020 at 14:27
0

Found the list of "discouraged functions" here:

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Functions/DiscouragedFunctionsSniff.php

So this class can simply be extended or modified directly, and PHPCS will warn about any other function you add to this list:

<?
class DiscouragedFunctionsSniff extends ForbiddenFunctionsSniff
{

    /**
     * A list of forbidden functions with their alternatives.
     *
     * The value is NULL if no alternative exists, i.e., the function should
     * just not be used.
     *
     * @var array<string, null>
     */
    public $forbiddenFunctions = [
                                     // Devel module debugging functions.
        'dargs'               => null,
        'dcp'                 => null,
        'dd'                  => null,
        'dfb'                 => null,
        'dfbt'                => null,
        'dpm'                 => null,
        'dpq'                 => null,
        'dpr'                 => null,
        'dprint_r'            => null,
        'drupal_debug'        => null,
        'dsm'                 => null,
        'dvm'                 => null,
        'dvr'                 => null,
        'kdevel_print_object' => null,
        'kpr'                 => null,
        'kprint_r'            => null,
        'sdpm'                => null,
                                  // Functions which are not available on all
                                  // PHP builds.
        'fnmatch'             => null,
                                  // Functions which are a security risk.
        'eval'                => null,
    ];
}

Working sniff

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.