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

Breadcrumb

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

function AssertSameWithCountRule::processNode

File

vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/AssertSameWithCountRule.php, line 25

Class

AssertSameWithCountRule
@implements Rule<NodeAbstract>

Namespace

PHPStan\Rules\PHPUnit

Code

public function processNode(Node $node, Scope $scope) : array {
    if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) {
        return [];
    }
    if (count($node->getArgs()) < 2) {
        return [];
    }
    if (!$node->name instanceof Node\Identifier || $node->name
        ->toLowerString() !== 'assertsame') {
        return [];
    }
    $right = $node->getArgs()[1]->value;
    if ($right instanceof Node\Expr\FuncCall && $right->name instanceof Node\Name && $right->name
        ->toLowerString() === 'count') {
        return [
            RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')->identifier('phpunit.assertCount')
                ->build(),
        ];
    }
    if ($right instanceof Node\Expr\MethodCall && $right->name instanceof Node\Identifier && $right->name
        ->toLowerString() === 'count' && count($right->getArgs()) === 0) {
        $type = $scope->getType($right->var);
        if ((new ObjectType(Countable::class))->isSuperTypeOf($type)
            ->yes()) {
            return [
                RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')
                    ->identifier('phpunit.assertCount')
                    ->build(),
            ];
        }
    }
    return [];
}

API Navigation

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