Skip to content

Commit

Permalink
refactor(phpstan): use Broker instead with MethodCall and Scope
Browse files Browse the repository at this point in the history
To get ride of:
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   TranslatableInheritanceTest.php                                                                                                                  
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------- 
  30     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::setExtendedTitle().  
  33     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::setExtendedTitle().  
  36     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::setExtendedTitle().  
  50     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::getExtendedTitle().  
  53     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::getExtendedTitle().  
  56     Call to an undefined method Knp\DoctrineBehaviors\Tests\Fixtures\Entity\Translatable\AbstractTranslatableEntityTranslation::getExtendedTitle().  
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------- 

`$entity` was seen as an AbstractTranslatableEntity and not as an ExtendedTranslatableEntity :(
  • Loading branch information
Kocal committed Feb 12, 2020
1 parent 87d8849 commit 636254d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
27 changes: 19 additions & 8 deletions src/PHPStan/Type/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

namespace Knp\DoctrineBehaviors\PHPStan\Type;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\MethodReflection;

final class Helper
{
public static function getTranslationClassFromMethodReflection(MethodReflection $methodReflection): string
public static function getTranslationClass(Broker $broker, MethodCall $methodCall, Scope $scope): string
{
$translatableReflection = $methodReflection->getDeclaringClass();
$translatableNativeReflection = $translatableReflection->getNativeReflection();
$type = $scope->getType($methodCall->var);
$translatableClass = $type->getReferencedClasses()[0];

return $translatableNativeReflection->getMethod('getTranslationEntityClass')->invoke(null);
return $broker
->getClass($translatableClass)
->getNativeReflection()
->getMethod('getTranslationEntityClass')
->invoke(null);
}

public static function getTranslatableClassFromMethodReflection(MethodReflection $methodReflection): string
public static function getTranslatableClass(Broker $broker, MethodCall $methodCall, Scope $scope): string
{
$translationReflection = $methodReflection->getDeclaringClass();
$translationNativeReflection = $translationReflection->getNativeReflection();
$type = $scope->getType($methodCall->var);
$translatableClass = $type->getReferencedClasses()[0];

return $translationNativeReflection->getMethod('getTranslatableEntityClass')->invoke(null);
return $broker
->getClass($translatableClass)
->getNativeReflection()
->getMethod('getTranslatableEntityClass')
->invoke(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;

final class TranslatableGetTranslationsDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
final class TranslatableGetTranslationsDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareExtension
{
/** @var Broker */
private $broker;

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function getClass(): string
{
return TranslatableInterface::class;
Expand All @@ -29,7 +39,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): \PHPStan\Type\Type
{
$translationClass = Helper::getTranslationClassFromMethodReflection($methodReflection);
$translationClass = Helper::getTranslationClass($this->broker, $methodCall, $scope);

return TypeCombinator::intersect(
new ObjectType(Collection::class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;

final class TranslatableTranslateDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
final class TranslatableTranslateDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareExtension
{
/** @var Broker */
private $broker;

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function getClass(): string
{
return TranslatableInterface::class;
Expand All @@ -25,7 +35,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): \PHPStan\Type\Type
{
$translationClass = Helper::getTranslationClassFromMethodReflection($methodReflection);
$translationClass = Helper::getTranslationClass($this->broker, $methodCall, $scope);

return new ObjectType($translationClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;

final class TranslationGetTranslatableDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
final class TranslationGetTranslatableDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareExtension
{
/** @var Broker */
private $broker;

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function getClass(): string
{
return TranslationInterface::class;
Expand All @@ -25,7 +35,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): \PHPStan\Type\Type
{
$translatableClass = Helper::getTranslatableClassFromMethodReflection($methodReflection);
$translatableClass = Helper::getTranslatableClass($this->broker, $methodCall, $scope);

return new ObjectType($translatableClass);
}
Expand Down

0 comments on commit 636254d

Please sign in to comment.