final class PhpDocConformanceService (View source)

Whether a PhpDoc type and a signature type describe the same thing.

Used by FunctionPhpDocConformsSignatureCheck, ClassMethodsPhpDocConformsSignatureCheck, and their Enum/Interface variants.

Three concerns that used to live here have moved out, leaving this class to hold the compatibility relation and nothing else:

  • phpstan/psalm annotation narrowing → {\StubTests\Framework\Validator\Services\PhpStanTypeNormalizer}, which is pure string rewriting and whose leaf table grows on a different schedule than the algorithm below;
  • "which type names are primitives" → {\StubTests\Framework\Validator\Services\TypeResolver::PRIMITIVES}, general type vocabulary that ReturnTypeComparator also needs and was reaching across for;
  • @template name extraction → {\StubTests\Framework\PhpDoc\TemplateTypeNormalizer}, which the stub parsers already had a better implementation of.

Methods

bool
isPhpDocCompatibleWithSignature(string $sig, string $doc, array $templateNames = [])

Check if a PhpDoc type is compatible with a signature type.

array
extractTemplateNames(string|null $rawPhpDoc)

Extract @template variable names from a raw PhpDoc comment.

string
normalizeDocType(string $type)

Normalise a PhpDoc type string: strip phpstan/psalm annotations, then apply the shared normalizeType() (union ordering, FQN backslash, T[] → array).

array
splitUnionComponents(string $type)

Split a union type string into individual components.

string|null
getParamSigTypeForPhpDoc(PHPParameter $param, string $phpVersion)

Get the signature type string for a parameter.

string|null
getPropertySigTypeForPhpDoc(PHPProperty $property, string $phpVersion)

Get the signature type string for a property.

Details

bool isPhpDocCompatibleWithSignature(string $sig, string $doc, array $templateNames = [])

Check if a PhpDoc type is compatible with a signature type.

Permissive algorithm — avoids false positives from intentional patterns:

  • Typed-array narrowing: sig array, doc string[] → pass (string[] normalises to array)
  • phpstan generics: sig array, doc array<K,V> → pass (generics stripped)
  • resource widening: sig GMP, doc resource|GMP → pass (intersection non-empty)
  • bool/false split: sig bool, doc false → pass (bool expands to {false, true})
  • union reordering: sig string|false, doc false|string → pass (normalised)
  • mixed sig or doc: sig mixed, doc string → pass (mixed encompasses all)
  • object sig with class doc: sig object, doc SomeClass → pass
  • class sig with object doc: sig SomeClass, doc object → pass
  • resource→class migration: sig SomeClass, doc resource → pass (PHP8 object migration)
  • @template variable in doc: sig \SplFileInfo, doc \T (T declared via @template) → pass
  • static ↔ class name: sig \DateTime, doc static → pass
  • class-to-class narrowing: sig \Iterator, doc \ArrayIterator → pass

Catches: sig string, doc int → fail (no shared component)

Parameters

string $sig

Signature type string (always a real PHP type — no template variables)

string $doc

PhpDoc type string (may contain @template variable names)

array $templateNames

@template variable names declared on the enclosing entity

Return Value

bool

true = compatible, false = mismatch detected

array extractTemplateNames(string|null $rawPhpDoc)

Extract @template variable names from a raw PhpDoc comment.

Delegates to the one implementation the stub parsers also use. This class used to carry a second, DocBlockFactory-based one that silently dropped @template-contravariant.

Parameters

string|null $rawPhpDoc

Return Value

array

Template variable names (without any leading backslash)

string normalizeDocType(string $type)

Normalise a PhpDoc type string: strip phpstan/psalm annotations, then apply the shared normalizeType() (union ordering, FQN backslash, T[] → array).

Parameters

string $type

Return Value

string

array splitUnionComponents(string $type)

Split a union type string into individual components.

Parameters

string $type

Return Value

array

string|null getParamSigTypeForPhpDoc(PHPParameter $param, string $phpVersion)

Get the signature type string for a parameter.

Priority:

  1. Declared type from getDeclaredType() — if non-empty (not NoType)
  2. LanguageLevelTypeAware — highest version <= $phpVersion, or defaultType

Parameters

PHPParameter $param
string $phpVersion

Return Value

string|null

string|null getPropertySigTypeForPhpDoc(PHPProperty $property, string $phpVersion)

Get the signature type string for a property.

Priority:

  1. Signature type from getType() — if non-empty (not NoType)
  2. LanguageLevelTypeAware — highest version <= $phpVersion, or defaultType

Parameters

PHPProperty $property
string $phpVersion

Return Value

string|null