-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cefaa9
commit 0dbb803
Showing
7 changed files
with
107 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
- | ||
class: Kris\LaravelFormBuilder\PhpStan\FormGetFieldExtension | ||
tags: | ||
- phpstan.broker.dynamicMethodReturnTypeExtension |
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
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
73 changes: 73 additions & 0 deletions
73
src/Kris/LaravelFormBuilder/PhpStan/FormGetFieldExtension.php
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,73 @@ | ||
<?php | ||
|
||
namespace Kris\LaravelFormBuilder\PhpStan; | ||
|
||
use Kris\LaravelFormBuilder\Fields\ChildFormType; | ||
use Kris\LaravelFormBuilder\Form; | ||
use Kris\LaravelFormBuilder\FormHelper; | ||
use Larastan\Larastan\Concerns\HasContainer; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\Annotations\AnnotationPropertyReflection; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeWithClassName; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Scalar\String_; | ||
|
||
class FormGetFieldExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function __construct( | ||
protected ReflectionProvider $reflectionProvider, | ||
) {} | ||
|
||
public function getClass(): string { | ||
return Form::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool { | ||
return $methodReflection->getName() == 'getField'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type { | ||
return $this->getTypeFromMethodCallGetField($methodReflection, $methodCall, $scope); | ||
} | ||
|
||
protected function getTypeFromMethodCallGetField(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (count($methodCall->getArgs()) < 1) { | ||
return null; | ||
} | ||
|
||
$fieldNameArg = $methodCall->getArgs()[0]->value; | ||
if (!($fieldNameArg instanceof String_)) { | ||
return null; | ||
} | ||
|
||
$fieldName = $fieldNameArg->value; | ||
|
||
$calledOnType = $scope->getType($methodCall->var); | ||
assert($calledOnType instanceof TypeWithClassName); | ||
$formClass = $calledOnType->getClassName(); | ||
|
||
$formClassReflection = $this->reflectionProvider->getClass($formClass); | ||
|
||
if (!$formClassReflection->hasProperty($fieldName)) { | ||
return null; | ||
} | ||
|
||
$formClassFieldProperty = $formClassReflection->getProperty($fieldName, $scope); | ||
if (!($formClassFieldProperty instanceof AnnotationPropertyReflection)) { | ||
return null; | ||
} | ||
|
||
return $formClassFieldProperty->getReadableType(); | ||
} | ||
|
||
} |