-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ViewWithMethodsClassReflectionExtension (#1348)
- Loading branch information
Showing
6 changed files
with
184 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |