Skip to content

Commit

Permalink
Merge pull request #93 from localheinz/feature/clean-up
Browse files Browse the repository at this point in the history
Enhancement: Slightly clean up code
  • Loading branch information
localheinz committed Dec 27, 2019
2 parents defdf4d + f4852d0 commit 46b4f00
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$calledOnType = $scope->getType($methodCall->var);

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
$returnType = $parametersAcceptor->getReturnType();

if (!$calledOnType instanceof ObjectProphecyType) {
return $parametersAcceptor->getReturnType();
return $returnType;
}

$types = \array_map(static function (string $class): ObjectType {
return new ObjectType($class);
}, $calledOnType->getProphesizedClasses());

$types[] = $parametersAcceptor->getReturnType();
$types[] = $returnType;

return TypeCombinator::intersect(...$types);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,37 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method

$calledOnType = $scope->getType($methodCall->var);

$prophecyType = $parametersAcceptor->getReturnType();
$returnType = $parametersAcceptor->getReturnType();

if (!$calledOnType instanceof ObjectProphecyType) {
return $prophecyType;
return $returnType;
}

if (0 === \count($methodCall->args)) {
return $prophecyType;
return $returnType;
}

$argType = $scope->getType($methodCall->args[0]->value);
$argumentType = $scope->getType($methodCall->args[0]->value);

if (!($argType instanceof ConstantStringType)) {
return $prophecyType;
if (!$argumentType instanceof ConstantStringType) {
return $returnType;
}

$class = $argType->getValue();
$className = $argumentType->getValue();

if (!($prophecyType instanceof TypeWithClassName)) {
if (!$returnType instanceof TypeWithClassName) {
throw new ShouldNotHappenException();
}

if ('static' === $class) {
return $prophecyType;
if ('static' === $className) {
return $returnType;
}

if ('self' === $class && null !== $scope->getClassReflection()) {
$class = $scope->getClassReflection()->getName();
if ('self' === $className && null !== $scope->getClassReflection()) {
$className = $scope->getClassReflection()->getName();
}

$calledOnType->addProphesizedClass($class);
$calledOnType->addProphesizedClass($className);

return $calledOnType;
}
Expand Down
25 changes: 13 additions & 12 deletions src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,33 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
$prophecyType = $parametersAcceptor->getReturnType();

$returnType = $parametersAcceptor->getReturnType();

if (0 === \count($methodCall->args)) {
return $prophecyType;
return $returnType;
}

$argType = $scope->getType($methodCall->args[0]->value);
$argumentType = $scope->getType($methodCall->args[0]->value);

if (!($argType instanceof ConstantStringType)) {
return $prophecyType;
if (!$argumentType instanceof ConstantStringType) {
return $returnType;
}

$class = $argType->getValue();
$className = $argumentType->getValue();

if (!($prophecyType instanceof TypeWithClassName)) {
if (!$returnType instanceof TypeWithClassName) {
throw new ShouldNotHappenException();
}

if ('static' === $class) {
return $prophecyType;
if ('static' === $className) {
return $returnType;
}

if ('self' === $class && null !== $scope->getClassReflection()) {
$class = $scope->getClassReflection()->getName();
if ('self' === $className && null !== $scope->getClassReflection()) {
$className = $scope->getClassReflection()->getName();
}

return new ObjectProphecyType($class);
return new ObjectProphecyType($className);
}
}

0 comments on commit 46b4f00

Please sign in to comment.