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

Breadcrumb

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

function RequireExplicitAssertionSniff::createConditions

*

Parameters

IdentifierTypeNode|ThisTypeNode|GenericTypeNode $typeNode: * @return list<string>

1 call to RequireExplicitAssertionSniff::createConditions()
RequireExplicitAssertionSniff::createAssert in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php
*

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php, line 372

Class

RequireExplicitAssertionSniff

Namespace

SlevomatCodingStandard\Sniffs\PHP

Code

private function createConditions(string $variableName, TypeNode $typeNode) : array {
    if ($typeNode instanceof GenericTypeNode) {
        $conditions = [
            sprintf('\\is_int(%s)', $variableName),
        ];
        if ($typeNode->genericTypes[0] instanceof ConstTypeNode) {
            $conditions[] = sprintf('%s >= %s', $variableName, (string) $typeNode->genericTypes[0]);
        }
        if ($typeNode->genericTypes[1] instanceof ConstTypeNode) {
            $conditions[] = sprintf('%s <= %s', $variableName, (string) $typeNode->genericTypes[1]);
        }
        return [
            implode(' && ', $conditions),
        ];
    }
    if ($typeNode instanceof ThisTypeNode) {
        return [
            sprintf('%s instanceof $this', $variableName),
        ];
    }
    if ($typeNode->name === 'self') {
        return [
            sprintf('%s instanceof %s', $variableName, $typeNode->name),
        ];
    }
    if ($typeNode->name === 'static') {
        return [
            sprintf('%s instanceof static', $variableName),
        ];
    }
    if (in_array($typeNode->name, [
        'true',
        'false',
        'null',
    ], true)) {
        return [
            sprintf('%s === %s', $variableName, $typeNode->name),
        ];
    }
    if ($typeNode->name === 'mixed' || TypeHintHelper::isVoidTypeHint($typeNode->name) || TypeHintHelper::isNeverTypeHint($typeNode->name)) {
        return [];
    }
    if (TypeHintHelper::isSimpleTypeHint($typeNode->name)) {
        return [
            sprintf('\\is_%s(%s)', TypeHintHelper::convertLongSimpleTypeHintToShort($typeNode->name), $variableName),
        ];
    }
    if (in_array($typeNode->name, [
        'resource',
        'object',
    ], true)) {
        return [
            sprintf('\\is_%s(%s)', $typeNode->name, $variableName),
        ];
    }
    if ($typeNode->name === 'numeric') {
        return [
            sprintf('\\is_int(%s)', $variableName),
            sprintf('\\is_float(%s)', $variableName),
        ];
    }
    if ($typeNode->name === 'scalar') {
        return [
            sprintf('\\is_int(%s)', $variableName),
            sprintf('\\is_float(%s)', $variableName),
            sprintf('\\is_bool(%s)', $variableName),
            sprintf('\\is_string(%s)', $variableName),
        ];
    }
    if ($this->enableIntegerRanges) {
        if ($typeNode->name === 'positive-int') {
            return [
                sprintf('\\is_int(%1$s) && %1$s > 0', $variableName),
            ];
        }
        if ($typeNode->name === 'negative-int') {
            return [
                sprintf('\\is_int(%1$s) && %1$s < 0', $variableName),
            ];
        }
    }
    if ($this->enableAdvancedStringTypes && in_array($typeNode->name, [
        'non-empty-string',
        'non-falsy-string',
        'callable-string',
        'numeric-string',
    ], true)) {
        $conditions = [
            sprintf('\\is_string(%s)', $variableName),
        ];
        if ($typeNode->name === 'non-empty-string') {
            $conditions[] = sprintf("%s !== ''", $variableName);
        }
        elseif ($typeNode->name === 'non-falsy-string') {
            $conditions[] = sprintf('(bool) %s === true', $variableName);
        }
        elseif ($typeNode->name === 'callable-string') {
            $conditions[] = sprintf('\\is_callable(%s)', $variableName);
        }
        else {
            $conditions[] = sprintf('\\is_numeric(%s)', $variableName);
        }
        return [
            implode(' && ', $conditions),
        ];
    }
    if (TypeHintHelper::isSimpleUnofficialTypeHints($typeNode->name)) {
        return [];
    }
    return [
        sprintf('%s instanceof %s', $variableName, $typeNode->name),
    ];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal