Skip to content

Commit

Permalink
feat: add ViewWithMethodsClassReflectionExtension (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Aug 28, 2022
1 parent 046ffb7 commit 3ebb0d7
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 133 deletions.
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ services:
class: NunoMaduro\Larastan\Methods\MacroMethodsClassReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension
-
class: NunoMaduro\Larastan\Methods\ViewWithMethodsClassReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension

-
class: NunoMaduro\Larastan\Properties\ModelAccessorExtension
Expand Down
136 changes: 3 additions & 133 deletions src/Methods/RedirectResponseMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

namespace NunoMaduro\Larastan\Methods;

use NunoMaduro\Larastan\Reflection\DynamicWhereMethodReflection;
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
{
Expand All @@ -19,7 +13,7 @@ public function hasMethod(Reflection\ClassReflection $classReflection, string $m
return false;
}

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

Expand All @@ -30,130 +24,6 @@ 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();
}
};
return new DynamicWhereMethodReflection($classReflection, $methodName);
}
}
31 changes: 31 additions & 0 deletions src/Methods/ViewWithMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace NunoMaduro\Larastan\Methods;

use NunoMaduro\Larastan\Reflection\DynamicWhereMethodReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;

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

if (! str_starts_with($methodName, 'with')) {
return false;
}

return true;
}

public function getMethod(
ClassReflection $classReflection,
string $methodName
): MethodReflection {
return new DynamicWhereMethodReflection($classReflection, $methodName);
}
}
136 changes: 136 additions & 0 deletions src/Reflection/DynamicWhereMethodReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace NunoMaduro\Larastan\Reflection;

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;

final class DynamicWhereMethodReflection implements Reflection\MethodReflection
{
private Reflection\ClassReflection $classReflection;

private string $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();
}
}
1 change: 1 addition & 0 deletions tests/Type/GeneralTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__.'/data/translate.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/model-factories.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/environment-helper.php');
yield from $this->gatherAssertTypes(__DIR__.'/data/view.php');
}

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

namespace IlluminateView;

use function PHPStan\Testing\assertType;

assertType('Illuminate\View\View', view('foo'));
assertType('Illuminate\View\View', view('foo')->with('bar', 'baz'));
assertType('Illuminate\View\View', view('foo')->withFoo('bar'));

0 comments on commit 3ebb0d7

Please sign in to comment.