-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature: Transform dynamic form controls to "dynamic" string
- Loading branch information
Showing
11 changed files
with
231 additions
and
39 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
84 changes: 84 additions & 0 deletions
84
src/Compiler/NodeVisitor/TransformDynamicFormControlsToDynamicStringNodeVisitor.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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Efabrica\PHPStanLatte\Compiler\NodeVisitor; | ||
|
||
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ExprTypeNodeVisitorBehavior; | ||
use Efabrica\PHPStanLatte\Compiler\NodeVisitor\Behavior\ExprTypeNodeVisitorInterface; | ||
use Efabrica\PHPStanLatte\Resolver\NameResolver\NameResolver; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
/** | ||
* changed output from: | ||
* <code> | ||
* $form[$dynamicInput]; | ||
* </code> | ||
* | ||
* to: | ||
* <code> | ||
* $form["$dynamicInput"]; | ||
* </code> | ||
* | ||
* if feature flag is turned on and type of $dynamicInput cannot be resolved as constant string / int | ||
*/ | ||
final class TransformDynamicFormControlsToDynamicStringNodeVisitor extends NodeVisitorAbstract implements ExprTypeNodeVisitorInterface | ||
{ | ||
use ExprTypeNodeVisitorBehavior; | ||
|
||
private bool $featureTransformDynamicFormControlNamesToString; | ||
|
||
private NameResolver $nameResolver; | ||
|
||
public function __construct( | ||
bool $featureTransformDynamicFormControlNamesToString, | ||
NameResolver $nameResolver | ||
) { | ||
$this->featureTransformDynamicFormControlNamesToString = $featureTransformDynamicFormControlNamesToString; | ||
$this->nameResolver = $nameResolver; | ||
} | ||
|
||
public function enterNode(Node $node): ?Node | ||
{ | ||
if ($this->featureTransformDynamicFormControlNamesToString === false) { | ||
return null; | ||
} | ||
|
||
if (!$node instanceof ArrayDimFetch) { | ||
return null; | ||
} | ||
|
||
if ($this->nameResolver->resolve($node->var) !== 'form') { | ||
return null; | ||
} | ||
|
||
if ($node->dim === null) { | ||
return null; | ||
} | ||
|
||
$type = $this->getType($node->dim); | ||
if ($type !== null && $type->getConstantScalarValues() !== []) { | ||
return null; | ||
} | ||
|
||
if ($node->dim instanceof Variable) { | ||
$node->dim = new String_('$' . $this->nameResolver->resolve($node->dim)); | ||
} | ||
|
||
if ($node->dim instanceof PropertyFetch) { | ||
$varName = $this->nameResolver->resolve($node->dim->var); | ||
$propertyName = $this->nameResolver->resolve($node->dim->name); | ||
if ($varName === null || $propertyName === null) { | ||
return null; | ||
} | ||
$node->dim = new String_('$' . $varName . '->' . $propertyName); | ||
} | ||
|
||
return $node; | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Efabrica\PHPStanLatte\Resolver\NameResolver; | ||
|
||
use Efabrica\PHPStanLatte\Resolver\ValueResolver\ValueResolver; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\IntegerRangeType; | ||
|
||
final class FormControlNameResolver | ||
{ | ||
private bool $featureTransformDynamicFormControlNamesToString; | ||
|
||
private ValueResolver $valueResolver; | ||
|
||
private NameResolver $nameResolver; | ||
|
||
public function __construct( | ||
bool $featureTransformDynamicFormControlNamesToString, | ||
ValueResolver $valueResolver, | ||
NameResolver $nameResolver | ||
) { | ||
$this->featureTransformDynamicFormControlNamesToString = $featureTransformDynamicFormControlNamesToString; | ||
$this->valueResolver = $valueResolver; | ||
$this->nameResolver = $nameResolver; | ||
} | ||
|
||
/** | ||
* @return array<int|string>|null | ||
*/ | ||
public function resolve(Expr $expr, Scope $scope): ?array | ||
{ | ||
$type = $scope->getType($expr); | ||
if ($type instanceof IntegerRangeType) { | ||
$min = $type->getMin() !== null ? $type->getMin() : $type->getMax(); | ||
$max = $type->getMax() !== null ? $type->getMax() : $type->getMin(); | ||
|
||
if ($min === null || $max === null) { | ||
return null; | ||
} | ||
|
||
$names = []; | ||
for ($i = $min; $i <= $max; $i++) { | ||
$names[] = $i; | ||
} | ||
} else { | ||
$names = $this->valueResolver->resolve($expr, $scope); | ||
} | ||
|
||
if ($this->featureTransformDynamicFormControlNamesToString && $names === null) { | ||
if ($expr instanceof Variable) { | ||
$variableName = $this->nameResolver->resolve($expr); | ||
$names = ['$' . $variableName]; | ||
} elseif ($expr instanceof PropertyFetch) { | ||
$varName = $this->nameResolver->resolve($expr->var); | ||
$propertyName = $this->nameResolver->resolve($expr->name); | ||
if ($varName === null || $propertyName === null) { | ||
return null; | ||
} | ||
$names = ['$' . $varName . '->' . $propertyName]; | ||
} | ||
} | ||
|
||
if ($names === null) { | ||
return null; | ||
} | ||
|
||
return array_filter($names, function ($value) { | ||
return is_string($value) || is_int($value); | ||
}); | ||
} | ||
} |
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