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

Breadcrumb

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

function AnnotationParser::forMethod

@psalm-param class-string $className @psalm-param non-empty-string $methodName

Throws

AnnotationsAreNotSupportedForInternalClassesException

InvalidVersionOperatorException

ReflectionException

Overrides Parser::forMethod

1 call to AnnotationParser::forMethod()
AnnotationParser::forClassAndMethod in vendor/phpunit/phpunit/src/Metadata/Parser/AnnotationParser.php
@psalm-param class-string $className @psalm-param non-empty-string $methodName

File

vendor/phpunit/phpunit/src/Metadata/Parser/AnnotationParser.php, line 187

Class

AnnotationParser
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Namespace

PHPUnit\Metadata\Parser

Code

public function forMethod(string $className, string $methodName) : MetadataCollection {
    assert(class_exists($className));
    assert(method_exists($className, $methodName));
    $result = [];
    foreach (AnnotationRegistry::getInstance()->forMethod($className, $methodName)
        ->symbolAnnotations() as $annotation => $values) {
        switch ($annotation) {
            case 'after':
                $result[] = Metadata::after();
                break;
            case 'afterClass':
                $result[] = Metadata::afterClass();
                break;
            case 'backupGlobals':
                $result[] = Metadata::backupGlobalsOnMethod($this->stringToBool($values[0]));
                break;
            case 'backupStaticAttributes':
            case 'backupStaticProperties':
                $result[] = Metadata::backupStaticPropertiesOnMethod($this->stringToBool($values[0]));
                break;
            case 'before':
                $result[] = Metadata::before();
                break;
            case 'beforeClass':
                $result[] = Metadata::beforeClass();
                break;
            case 'covers':
                foreach ($values as $value) {
                    $value = $this->cleanUpCoversOrUsesTarget($value);
                    $result[] = Metadata::coversOnMethod($value);
                }
                break;
            case 'coversNothing':
                $result[] = Metadata::coversNothingOnMethod();
                break;
            case 'dataProvider':
                foreach ($values as $value) {
                    $value = rtrim($value, " ()\n\r\t\v\x00");
                    if (str_contains($value, '::')) {
                        $result[] = Metadata::dataProvider(...explode('::', $value));
                        continue;
                    }
                    $result[] = Metadata::dataProvider($className, $value);
                }
                break;
            case 'depends':
                foreach ($values as $value) {
                    $deepClone = false;
                    $shallowClone = false;
                    if (str_starts_with($value, 'clone ')) {
                        $deepClone = true;
                        $value = substr($value, strlen('clone '));
                    }
                    elseif (str_starts_with($value, '!clone ')) {
                        $value = substr($value, strlen('!clone '));
                    }
                    elseif (str_starts_with($value, 'shallowClone ')) {
                        $shallowClone = true;
                        $value = substr($value, strlen('shallowClone '));
                    }
                    elseif (str_starts_with($value, '!shallowClone ')) {
                        $value = substr($value, strlen('!shallowClone '));
                    }
                    if (str_contains($value, '::')) {
                        [
                            $_className,
                            $_methodName,
                        ] = explode('::', $value);
                        assert($_className !== '');
                        assert($_methodName !== '');
                        if ($_methodName === 'class') {
                            $result[] = Metadata::dependsOnClass($_className, $deepClone, $shallowClone);
                            continue;
                        }
                        $result[] = Metadata::dependsOnMethod($_className, $_methodName, $deepClone, $shallowClone);
                        continue;
                    }
                    $result[] = Metadata::dependsOnMethod($className, $value, $deepClone, $shallowClone);
                }
                break;
            case 'doesNotPerformAssertions':
                $result[] = Metadata::doesNotPerformAssertionsOnMethod();
                break;
            case 'excludeGlobalVariableFromBackup':
                foreach ($values as $value) {
                    $result[] = Metadata::excludeGlobalVariableFromBackupOnMethod($value);
                }
                break;
            case 'excludeStaticPropertyFromBackup':
                foreach ($values as $value) {
                    $tmp = explode(' ', $value);
                    if (count($tmp) !== 2) {
                        continue;
                    }
                    $result[] = Metadata::excludeStaticPropertyFromBackupOnMethod(trim($tmp[0]), trim($tmp[1]));
                }
                break;
            case 'group':
            case 'ticket':
                foreach ($values as $value) {
                    $result[] = Metadata::groupOnMethod($value);
                }
                break;
            case 'large':
                $result[] = Metadata::groupOnMethod('large');
                break;
            case 'medium':
                $result[] = Metadata::groupOnMethod('medium');
                break;
            case 'postCondition':
                $result[] = Metadata::postCondition();
                break;
            case 'preCondition':
                $result[] = Metadata::preCondition();
                break;
            case 'preserveGlobalState':
                $result[] = Metadata::preserveGlobalStateOnMethod($this->stringToBool($values[0]));
                break;
            case 'runInSeparateProcess':
                $result[] = Metadata::runInSeparateProcess();
                break;
            case 'small':
                $result[] = Metadata::groupOnMethod('small');
                break;
            case 'test':
                $result[] = Metadata::test();
                break;
            case 'testdox':
                $result[] = Metadata::testDoxOnMethod($values[0]);
                break;
            case 'uses':
                foreach ($values as $value) {
                    $value = $this->cleanUpCoversOrUsesTarget($value);
                    $result[] = Metadata::usesOnMethod($value);
                }
                break;
        }
    }
    if (method_exists($className, $methodName)) {
        try {
            $result = array_merge($result, $this->parseRequirements(AnnotationRegistry::getInstance()->forMethod($className, $methodName)
                ->requirements(), 'method'));
        } catch (InvalidVersionRequirementException $e) {
            EventFacade::emitter()->testRunnerTriggeredWarning(sprintf('Method %s::%s is annotated using an invalid version requirement: %s', $className, $methodName, $e->getMessage()));
        }
    }
    return MetadataCollection::fromArray($result);
}
RSS feed
Powered by Drupal