Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct method reflection for magic with* calls on RedirectResponse #1016

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ services:
class: NunoMaduro\Larastan\Methods\ModelFactoryMethodsClassReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension
-
class: NunoMaduro\Larastan\Methods\RedirectResponseMethodsClassReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension

-
class: NunoMaduro\Larastan\Properties\ModelAccessorExtension
Expand Down
1 change: 0 additions & 1 deletion src/Methods/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function handle(ClassReflection $classReflection, string $methodName): Pa
Pipes\Facades::class,
Pipes\Managers::class,
Pipes\Auths::class,
Pipes\RedirectResponseWiths::class,
]
)
->then(
Expand Down
43 changes: 0 additions & 43 deletions src/Methods/Pipes/RedirectResponseWiths.php

This file was deleted.

159 changes: 159 additions & 0 deletions src/Methods/RedirectResponseMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace NunoMaduro\Larastan\Methods;

use PHPStan\Reflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

class RedirectResponseMethodsClassReflectionExtension implements Reflection\MethodsClassReflectionExtension
{
public function hasMethod(Reflection\ClassReflection $classReflection, string $methodName): bool
{
if ($classReflection->getName() !== 'Illuminate\Http\RedirectResponse') {
return false;
}

if (strpos($methodName, 'with') !== 0) {
return false;
}

return true;
}

public function getMethod(
Reflection\ClassReflection $classReflection,
string $methodName
): Reflection\MethodReflection {
return new class($classReflection, $methodName) implements Reflection\MethodReflection
{
/** @var Reflection\ClassReflection */
private $classReflection;

/** @var string */
private $methodName;

public function __construct(Reflection\ClassReflection $classReflection, string $methodName)
{
$this->classReflection = $classReflection;
$this->methodName = $methodName;
}

public function getDeclaringClass(): Reflection\ClassReflection
{
return $this->classReflection;
}

public function isStatic(): bool
{
return false;
}

public function isPrivate(): bool
{
return false;
}

public function isPublic(): bool
{
return true;
}

public function getDocComment(): ?string
{
return null;
}

public function getName(): string
{
return $this->methodName;
}

public function getPrototype(): Reflection\ClassMemberReflection
{
return $this;
}

public function getVariants(): array
{
return [
new FunctionVariant(
TemplateTypeMap::createEmpty(),
TemplateTypeMap::createEmpty(),
[
new class implements ParameterReflection
{
public function getName(): string
{
return 'dynamic-with';
}

public function isOptional(): bool
{
return false;
}

public function getType(): \PHPStan\Type\Type
{
return new MixedType();
}

public function passedByReference(): \PHPStan\Reflection\PassedByReference
{
return Reflection\PassedByReference::createNo();
}

public function isVariadic(): bool
{
return false;
}

public function getDefaultValue(): ?\PHPStan\Type\Type
{
return null;
}
},
],
false,
new ObjectType($this->classReflection->getName())
),
];
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function getDeprecatedDescription(): ?string
{
return null;
}

public function isFinal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function getThrowType(): ?Type
{
return null;
}

public function hasSideEffects(): TrinaryLogic
{
return TrinaryLogic::createNo();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Reflection;

use Illuminate\Http\RedirectResponse;
use NunoMaduro\Larastan\Methods\RedirectResponseMethodsClassReflectionExtension;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\VerbosityLevel;

class RedirectResponseMethodsClassReflectionExtensionTest extends PHPStanTestCase
{
/**
* @var ReflectionProvider
*/
private $reflectionProvider;

/**
* @var RedirectResponseMethodsClassReflectionExtension
*/
private $reflectionExtension;

protected function setUp(): void
{
parent::setUp();

$this->reflectionProvider = $this->createReflectionProvider();
$this->reflectionExtension = new RedirectResponseMethodsClassReflectionExtension();
}

/**
* @test
*
* @dataProvider greenMethodProvider
*/
public function it_will_find_methods_starting_with_with(string $methodName)
{
$requestClass = $this->reflectionProvider->getClass(RedirectResponse::class);

$this->assertTrue($this->reflectionExtension->hasMethod($requestClass, $methodName));
}

/**
* @test
*
* @dataProvider redMethodProvider
*/
public function it_will_not_find_methods(string $methodName)
{
$requestClass = $this->reflectionProvider->getClass(RedirectResponse::class);

$this->assertFalse($this->reflectionExtension->hasMethod($requestClass, $methodName));
}

/**
* @test
*
* @dataProvider greenMethodProvider
*/
public function it_will_have_correct_method_reflection(string $methodName)
{
$requestClass = $this->reflectionProvider->getClass(RedirectResponse::class);
$methodReflection = $this->reflectionExtension->getMethod($requestClass, $methodName);
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$this->assertSame($methodName, $methodReflection->getName());
$this->assertSame($requestClass, $methodReflection->getDeclaringClass());
$this->assertFalse($methodReflection->isStatic());
$this->assertFalse($methodReflection->isPrivate());
$this->assertTrue($methodReflection->isPublic());
$this->assertCount(1, $parametersAcceptor->getParameters());
$this->assertSame('mixed', $parametersAcceptor->getParameters()[0]->getType()->describe(VerbosityLevel::value()));
$this->assertSame(RedirectResponse::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
}

/** @return iterable<mixed> */
public function greenMethodProvider(): iterable
{
yield ['withFoo'];
yield ['withFooAndBar'];
}

/** @return iterable<mixed> */
public function redMethodProvider(): iterable
{
yield ['non-existent'];
yield ['aWith'];
yield ['WithFoo'];
}

public static function getAdditionalConfigFiles(): array
{
return [__DIR__.'/../../extension.neon'];
}
}
1 change: 1 addition & 0 deletions tests/Type/MethodsClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MethodsClassReflectionExtensionTest extends \PHPStan\Testing\TypeInference
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__.'/data/macros.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/redirect-response.php');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Type/data/redirect-response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace RedirectResponse;

use Illuminate\Http\RedirectResponse;
use function PHPStan\Testing\assertType;

assertType(RedirectResponse::class, redirect()->back()->withSuccess(true));
assertType(RedirectResponse::class, redirect()->back()->withCookie('foo'));