Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Ruleset.php

function Ruleset::setSniffProperty

Set a single property for a sniff.

Parameters

string $sniffClass The class name of the sniff.:

string $name The name of the property to change.:

array $settings Array with the new value of the property and the scope of the property being set.:

Return value

void

Throws

\PHP_CodeSniffer\Exceptions\RuntimeException When attempting to set a non-existent property on a sniff which doesn't declare the property or explicitly supports dynamic properties.

1 call to Ruleset::setSniffProperty()
Ruleset::populateTokenListeners in vendor/squizlabs/php_codesniffer/src/Ruleset.php
Populates the array of PHP_CodeSniffer_Sniff objects for this file.

File

vendor/squizlabs/php_codesniffer/src/Ruleset.php, line 1474

Class

Ruleset

Namespace

PHP_CodeSniffer

Code

public function setSniffProperty($sniffClass, $name, $settings) {
    // Setting a property for a sniff we are not using.
    if (isset($this->sniffs[$sniffClass]) === false) {
        return;
    }
    $name = trim($name);
    $propertyName = $name;
    if (substr($propertyName, -2) === '[]') {
        $propertyName = substr($propertyName, 0, -2);
    }
    
    /*
     * BC-compatibility layer for $settings using the pre-PHPCS 3.8.0 format.
     *
     * Prior to PHPCS 3.8.0, `$settings` was expected to only contain the new _value_
     * for the property (which could be an array).
     * Since PHPCS 3.8.0, `$settings` is expected to be an array with two keys: 'scope'
     * and 'value', where 'scope' indicates whether the property should be set to the given 'value'
     * for one individual sniff or for all sniffs in a standard.
     *
     * This BC-layer is only for integrations with PHPCS which may call this method directly
     * and will be removed in PHPCS 4.0.0.
     */
    if (is_array($settings) === false || isset($settings['scope'], $settings['value']) === false) {
        // This will be an "old" format value.
        $settings = [
            'value' => $settings,
            'scope' => 'standard',
        ];
        trigger_error(__FUNCTION__ . ': the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.', E_USER_DEPRECATED);
    }
    $isSettable = false;
    $sniffObject = $this->sniffs[$sniffClass];
    if (property_exists($sniffObject, $propertyName) === true || $sniffObject instanceof stdClass === true || method_exists($sniffObject, '__set') === true) {
        $isSettable = true;
    }
    if ($isSettable === false) {
        if ($settings['scope'] === 'sniff') {
            $notice = "Ruleset invalid. Property \"{$propertyName}\" does not exist on sniff ";
            $notice .= array_search($sniffClass, $this->sniffCodes, true);
            throw new RuntimeException($notice);
        }
        return;
    }
    $value = $settings['value'];
    if (is_string($value) === true) {
        $value = trim($value);
    }
    if ($value === '') {
        $value = null;
    }
    // Special case for booleans.
    if ($value === 'true') {
        $value = true;
    }
    else {
        if ($value === 'false') {
            $value = false;
        }
        else {
            if (substr($name, -2) === '[]') {
                $name = $propertyName;
                $values = [];
                if ($value !== null) {
                    foreach (explode(',', $value) as $val) {
                        list($k, $v) = explode('=>', $val . '=>');
                        if ($v !== '') {
                            $values[trim($k)] = trim($v);
                        }
                        else {
                            $values[] = trim($k);
                        }
                    }
                }
                $value = $values;
            }
        }
    }
    $sniffObject->{$name} = $value;
}
RSS feed
Powered by Drupal