Skip to content

Instantly share code, notes, and snippets.

@lyrixx lyrixx/BrokerFactory.php
Last active Jul 1, 2019

Embed
What would you like to do?
PHPStan extension to extract property type hint from constructor argument

PHPStan Extension

Why?

See https://github.com/phpstan/phpstan/issues/1562

Installation

Add the following lines your .phpstan.neon:

For version < 9:

services:
    -
        class: Tests\Phpstan\BrokerFactory
    broker:
        class: PHPStan\Broker\Broker
        factory: @Tests\Phpstan\BrokerFactory::create

else:


services:
    brokerFactory:
        class: Tests\Util\BrokerFactory
<?php
use PHPStan\Broker\Broker;
use PHPStan\Broker\BrokerFactory as PhpstanBrokerFactory;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Type\ObjectType;
class BrokerFactory extends PhpstanBrokerFactory
{
public function create(): Broker
{
$broker = parent::create();
$r = new \ReflectionProperty($broker, 'propertiesClassReflectionExtensions');
$r->setAccessible(true);
$propertiesClassReflectionExtensions = $r->getValue($broker);
array_unshift($propertiesClassReflectionExtensions, new PropertiesClassReflectionBasedOnConstructor());
$r->setValue($broker, $propertiesClassReflectionExtensions);
return $broker;
}
}
class PropertiesClassReflectionBasedOnConstructor implements PropertiesClassReflectionExtension
{
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
return $this->extractFromConstructor($classReflection->getNativeReflection(), $propertyName);
}
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$nativeClassReflection = $classReflection->getNativeReflection();
foreach ($nativeClassReflection->getConstructor()->getParameters() as $parameter) {
if ($propertyName !== $parameter->name) {
continue;
}
$type = new ObjectType((string) $parameter->getType());
return new PhpPropertyReflection($classReflection, $type, $nativeClassReflection->getProperty($propertyName), false, false);
}
}
private function extractFromConstructor(\ReflectionClass $nativeClassReflection, string $propertyName)
{
if (!$nativeClassReflection->hasProperty($propertyName)) {
return false;
}
$constructor = $nativeClassReflection->getConstructor();
if (!$constructor) {
return false;
}
foreach ($constructor->getParameters() as $parameter) {
if ($propertyName !== $parameter->name) {
continue;
}
if (!$parameter->getType()) {
return false;
}
if ($parameter->isOptional()) {
return false;
}
return class_exists((string) $parameter->getType());
}
if ($parentClass = $nativeClassReflection->getParentClass()) {
try {
$reflectionClass = new \ReflectionClass($parentClass);
} catch (\ReflectionException $e) {
return false;
}
return $this->extractFromConstructor($reflectionClass, $propertyName);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.