Skip to content

Commit

Permalink
Object shape variables (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco authored Jul 31, 2023
1 parent dd0fcc4 commit 3cdbcc3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Tip for error message "Latte template xxx.latte was not analysed"
- Tip for standalone templates
- Feature Transform dynamic form controls to "dynamic" string (control with name $record->id will be transformed to "$record->id") (Turn this feature with parameter `latte.features.transformDynamicFormControlNamesToString: true`)
- Support for object shape variables

### Fixed
- `If condition is always true` for CheckboxList::getLabelPart(), CheckboxList::getControlPart(), RadioList::getLabelPart() and RadioList::getControlPart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testVariables(): void
'NODE NetteApplicationUIPresenter {"className":"VariablesPresenter"}',
'NODE NetteApplicationUIPresenterStandalone {"className":"VariablesPresenter"}',
'TEMPLATE arrayShapeParams.latte VariablesPresenter::arrayShapeParams ["startup","startupParent","presenter","control","flashes","a","b","c"] ["parentForm"]',
'TEMPLATE objectShapeParams.latte VariablesPresenter::objectShapeParams ["startup","startupParent","presenter","control","flashes","objectShape"] ["parentForm"]',
'TEMPLATE default.latte VariablesPresenter::default ["startup","startupParent","presenter","control","flashes","title","viaGetTemplate","stringLists","localStrings","dynamic","obj","array1","array2","list1","list2","array1WithoutType","array2WithoutType","list1WithType","list2WithType","variableFromMethodCallOnVariable","someOtherVariableWithDefault","nullOrUrl","variableFromParentCalledViaParent","variableFromOtherMethod","variableFromParent","varFromVariable","overwritting","parentOverwritting","calledParentOverwritting","calledParentOverwritted","calledParentSecondOverwritting","encapsedVariable","fromRenderDefault"] ["parentForm","onlyParentDefaultForm"]',
'TEMPLATE other.latte VariablesPresenter::other ["startup","startupParent","presenter","control","flashes","fromOtherAction","unresolvedInclude"] ["parentForm"]',
'TEMPLATE parent.latte VariablesPresenter::parent ["startup","startupParent","presenter","control","flashes","variableFromParentAction","variableFromOtherMethod"] ["parentForm","parentDefaultForm"]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Efabrica\PHPStanLatte\Tests\Rule\LatteTemplatesRule\PresenterWithoutModule\Fixtures;

use stdClass;

final class VariablesPresenter extends ParentPresenter
{
/** @var array<string[]> */
Expand All @@ -12,6 +14,9 @@ final class VariablesPresenter extends ParentPresenter
/** @var array{a?: string, b: int, c: ?string} */
private array $arrayShape = [];

/** @var stdClass&object{a?: string, b: int, c: ?string} */
private stdClass $objectShape;

protected function startup()
{
parent::startup();
Expand Down Expand Up @@ -158,6 +163,11 @@ public function actionArrayShapeParams(): void
$this->template->setParameters($this->arrayShape);
}

public function actionObjectShapeParams(): void
{
$this->template->objectShape = $this->objectShape;
}

private function itemsToArrayAssignWithoutTypes(): array
{
return ['one', 'two'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{block content}

{php \PHPStan\dumpType($objectShape)}
{php \PHPStan\dumpType($objectShape->a)}
{php \PHPStan\dumpType($objectShape->b)}
{php \PHPStan\dumpType($objectShape->c)}
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,26 @@ public function testVariables(): void
5,
'arrayShapeParams.latte',
],
[
'Dumped type: object{a?: string, b: int, c: string|null}&stdClass',
3,
'objectShapeParams.latte',
],
[
'Dumped type: mixed',
4,
'objectShapeParams.latte',
],
[
'Dumped type: int',
5,
'objectShapeParams.latte',
],
[
'Dumped type: string|null',
6,
'objectShapeParams.latte',
],
]);
}

Expand Down

0 comments on commit 3cdbcc3

Please sign in to comment.