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

Breadcrumb

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

class ClassMethodCoversExistsRule

@implements Rule<Node\Stmt\ClassMethod>

Hierarchy

  • class \PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule implements \PHPStan\Rules\Rule

Expanded class hierarchy of ClassMethodCoversExistsRule

File

vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/ClassMethodCoversExistsRule.php, line 22

Namespace

PHPStan\Rules\PHPUnit
View source
class ClassMethodCoversExistsRule implements Rule {
    
    /**
     * Covers helper.
     *
     * @var CoversHelper
     */
    private $coversHelper;
    
    /**
     * The file type mapper.
     *
     * @var FileTypeMapper
     */
    private $fileTypeMapper;
    public function __construct(CoversHelper $coversHelper, FileTypeMapper $fileTypeMapper) {
        $this->coversHelper = $coversHelper;
        $this->fileTypeMapper = $fileTypeMapper;
    }
    public function getNodeType() : string {
        return Node\Stmt\ClassMethod::class;
    }
    public function processNode(Node $node, Scope $scope) : array {
        $classReflection = $scope->getClassReflection();
        if ($classReflection === null) {
            return [];
        }
        if (!$classReflection->isSubclassOf(TestCase::class)) {
            return [];
        }
        $classPhpDoc = $classReflection->getResolvedPhpDoc();
        [
            $classCovers,
            $classCoversDefaultClasses,
        ] = $this->coversHelper
            ->getCoverAnnotations($classPhpDoc);
        $classCoversStrings = array_map(static function (PhpDocTagNode $covers) : string {
            return (string) $covers->value;
        }, $classCovers);
        $docComment = $node->getDocComment();
        if ($docComment === null) {
            return [];
        }
        $coversDefaultClass = count($classCoversDefaultClasses) === 1 ? array_shift($classCoversDefaultClasses) : null;
        $methodPhpDoc = $this->fileTypeMapper
            ->getResolvedPhpDoc($scope->getFile(), $classReflection->getName(), $scope->isInTrait() ? $scope->getTraitReflection()
            ->getName() : null, $node->name
            ->toString(), $docComment->getText());
        [
            $methodCovers,
            $methodCoversDefaultClasses,
        ] = $this->coversHelper
            ->getCoverAnnotations($methodPhpDoc);
        $errors = [];
        if (count($methodCoversDefaultClasses) > 0) {
            $errors[] = RuleErrorBuilder::message(sprintf('@coversDefaultClass defined on class method %s.', $node->name))
                ->identifier('phpunit.covers')
                ->build();
        }
        foreach ($methodCovers as $covers) {
            if (in_array((string) $covers->value, $classCoversStrings, true)) {
                $errors[] = RuleErrorBuilder::message(sprintf('Class already @covers %s so the method @covers is redundant.', $covers->value))
                    ->identifier('phpunit.coversDuplicate')
                    ->build();
            }
            $errors = array_merge($errors, $this->coversHelper
                ->processCovers($node, $covers, $coversDefaultClass));
        }
        return $errors;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ClassMethodCoversExistsRule::$coversHelper private property * Covers helper.
*
*
ClassMethodCoversExistsRule::$fileTypeMapper private property * The file type mapper.
*
*
ClassMethodCoversExistsRule::getNodeType public function
ClassMethodCoversExistsRule::processNode public function
ClassMethodCoversExistsRule::__construct public function
RSS feed
Powered by Drupal