PhpDocConformanceService
final class PhpDocConformanceService (View source)
Service for PhpDoc-vs-signature conformance checks.
Provides the compatibility algorithm and helper methods used by FunctionPhpDocConformsSignatureCheck, ClassMethodsPhpDocConformsSignatureCheck, and their Enum/Interface variants.
Extracted from PhpDocConformanceTrait for independent testability.
Constants
| PRIMITIVES |
PHP primitive types — used to distinguish class names from scalar/pseudo types. |
| private PHPSTAN_LEAF_MAP |
Maps phpstan/psalm pseudo-type leaves to the closest built-in PHP type. Keys are lowercased leaf tokens (after generics/shapes/ |
Methods
Check if a PhpDoc type is compatible with a signature type.
Extract @template variable names from a raw PhpDoc comment.
Normalise a PhpDoc type string.
Split a union type string into individual components.
Get the signature type string for a parameter.
Get the signature type string for a property.
Filter parameters by version availability and deduplicate same-named variadic pairs.
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, docstring[]→ pass (string[] normalises to array) - phpstan generics: sig
array, docarray<K,V>→ pass (generics stripped) - resource widening: sig
GMP, docresource|GMP→ pass (intersection non-empty) - bool/false split: sig
bool, docfalse→ pass (bool expands to {false, true}) - union reordering: sig
string|false, docfalse|string→ pass (normalised) - mixed sig or doc: sig
mixed, docstring→ pass (mixed encompasses all) - object sig with class doc: sig
object, docSomeClass→ pass - class sig with object doc: sig
SomeClass, docobject→ pass - resource→class migration: sig
SomeClass, docresource→ pass (PHP8 object migration) - @template variable in doc: sig
\SplFileInfo, doc\T(T declared via @template) → pass - static ↔ class name: sig
\DateTime, docstatic→ pass - class-to-class narrowing: sig
\Iterator, doc\ArrayIterator→ pass
Catches: sig string, doc int → fail (no shared component)
array
extractTemplateNames(string|null $rawPhpDoc)
Extract @template variable names from a raw PhpDoc comment.
string
normalizeDocType(string $type)
Normalise a PhpDoc type string.
Strips phpstan/psalm annotations first, then applies standard 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.
Priority:
- Declared type from getDeclaredType() — if non-empty (not NoType)
- LanguageLevelTypeAware — highest version <= $phpVersion, or defaultType
string|null
getPropertySigTypeForPhpDoc(PHPProperty $property, string $phpVersion)
Get the signature type string for a property.
Priority:
- Signature type from getType() — if non-empty (not NoType)
- LanguageLevelTypeAware — highest version <= $phpVersion, or defaultType
array
filterAndDeduplicateParamsPhpDoc(array $params, string $phpVersion)
Filter parameters by version availability and deduplicate same-named variadic pairs.