symfony/property-info
Symfony PropertyInfo extracts metadata about PHP class properties (types, visibility, accessors) from multiple sources like reflection, PHPDoc, and serializers. Useful for building API docs, forms, validation, and other tooling that needs reliable property details.
The symfony/property-info package is a low-level, metadata-driven component designed to extract runtime property information (types, mutability, visibility, etc.) from PHP classes using multiple sources (e.g., PHPDoc annotations, Reflection, PHPStan, DocBlocks). It is not a high-level framework but rather a foundational tool for:
Key Fit for Laravel/PHP Ecosystem:
spatie/laravel-arrayable).laravelcollective/html).@var, @property), promoted constructor properties, and union types.ReflectionProperty.Serializer, Validator, or PropertyAccess, this is a natural fit.illuminate/support/Traits/HasAttributes for dynamic metadata.| Integration Point | Feasibility | Notes |
|---|---|---|
| Validation (Laravel) | High | Replace Validator::make() with custom rules using PropertyInfo for dynamic type hints. |
| API Resources (Laravel) | High | Extract property metadata for auto-generated API responses. |
| Form Builders | Medium | Useful for dynamic field generation (e.g., laravelcollective/html). |
| Serialization | High | Enhance Arrayable/Jsonable with type-aware serialization. |
| ORM (Eloquent) | Medium | Could augment getFillable() or getCasts() with runtime type checks. |
| Static Analysis | High | Integrate with PHPStan/Psalm for runtime validation. |
| Event Dispatching | Low | Not directly applicable; metadata is passive. |
Example Use Cases in Laravel:
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
$extractor = new PhpDocExtractor();
$propertyType = $extractor->getTypes(MyModel::class, 'attribute');
Validator::extend('type_match', function ($attr, $value, $params) use ($extractor) {
$types = $extractor->getTypes(MyModel::class, $attr);
return in_array(gettype($value), $types, true);
});
$propertyInfo = PropertyInfoExtractorInterface::createExtracted([
new PhpDocExtractor(),
new ReflectionExtractor(),
]);
$allowed = array_filter($request->input(), fn($val, $key) =>
$propertyInfo->hasType(MyModel::class, $key, 'string')
);
$fields = collect(MyModel::class)
->getProperties()
->mapWithKeys(fn($prop) => [
$prop->getName() => [
'type' => $extractor->getTypes(MyModel::class, $prop->getName()),
'label' => ucfirst($prop->getName()),
]
]);
| Risk Area | Severity | Mitigation |
|---|---|---|
| Dependency Conflicts | Medium | symfony/property-info depends on phpdocumentor/reflection-docblock (v6+). Ensure Laravel’s phpdocumentor version is compatible (see #63206). |
| Performance Overhead | Low | Caching extractors (e.g., PropertyInfoExtractorInterface::createExtracted()) mitigates repeated reflection calls. |
| False Positives in Types | Medium | PHPDoc parsing can be error-prone; validate against runtime checks. |
| Laravel-Specific Quirks | Low | Laravel’s ReflectionClass may behave differently; test with ReflectionExtractor. |
| Breaking Changes | Low | Symfony’s LTS policy ensures backward compatibility within major versions. |
Critical Questions for TPM:
PhpDoc, Reflection, PhpStan) to prioritize.PropertyInfo reduce reliance on getCasts() in Eloquent?phpdocumentor/reflection-docblock?
^5.0 for Symfony 7/8).PropertyInfoExtractorInterface supports caching; Laravel’s Cache facade can wrap it.PropertyInfoExtractorInterface as a singleton in AppServiceProvider.| Laravel Component | Compatibility | Integration Strategy |
|---|---|---|
| Validation | High | Replace Validator::extend() with PropertyInfo-driven rules. |
| API Resources | High | Use PropertyInfo to auto-generate $fillable or $visible based on PHPDoc types. |
| Eloquent Models | Medium | Augment getFillable()/getCasts() with runtime type checks. |
| Form Builders | Medium | Generate dynamic fields using PhpDocExtractor. |
| Serializer (e.g., Spatie) | High | Validate serialization/deserialization against PHPDoc types. |
| Static Analysis (PHPStan) | High | Leverage PhpStanExtractor for runtime validation. |
Key Synergies:
symfony/validator, symfony/property-access, or symfony/serializer, this is a native fit.symfony/property-info to composer.json (target Symfony 7/8 for Laravel 10+).PhpDocExtractor) for a critical path (e.g., validation).User).PropertyInfo in:
Cache::remember()) for performance.getCasts() to dynamic PropertyInfo checks.Backward Compatibility:
PropertyInfo usage.| Constraint | Solution |
|---|---|
| PHP 8.1+ Required | Laravel 10+ supports PHP 8.1+; no issue. |
| Symfony 7/8 Dependency | Laravel’s illuminate/support is compatible with Symfony components. |
phpdocumentor/reflection-docblock v6 |
Pin to ^5.0 if needed (see #63206). |
| **Leg |
How can I help you explore Laravel packages today?