Skip to content

Commit

Permalink
add generic for method parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
arutyunyan committed Apr 22, 2024
1 parent dc90486 commit 2989b50
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public function __call($func, $args)
throw new DecoratorError('Only public methods can be decorated!');
}

$params = $method->getParameters();

foreach ($params as $paramIndex => $param) {
$paramAttrs = $param->getAttributes();

foreach ($paramAttrs as $paramAttr) {
$paramAttrInst = $paramAttr->newInstance();

if ($paramAttrInst instanceof PythonDecorator) {
$paramAttrInst->bindTo($this)->wrapper($args[$paramIndex]);
}
}
}

$attributes = $method->getAttributes();
// }

Expand Down
20 changes: 20 additions & 0 deletions tests/GenericsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,24 @@ public function testPrimitiveStrItSelfFail(): void
$box = new Box();
set_decorator_prop($box, 'str', 1);
}

public function testMethodParamAttributes(): void
{
$this->expectNotToPerformAssertions();

$box = new Box();
$box(Pen::class);

call_decorator_func_array([$box, 'addItem'], [new Pen]);
}

public function testMethodParamAttributesFail(): void
{
$this->expectException(GenericError::class);

$box = new Box();
$box(Pen::class);

call_decorator_func_array([$box, 'addItem'], [new Pencil]);
}
}
8 changes: 8 additions & 0 deletions tests/examples/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ class Box

#[Length(4)]
public $size;

#[ArrayOf(T::class)]
public $items;

public function addItem(#[T] $item)
{
$this->items[] = $item;
}
}

0 comments on commit 2989b50

Please sign in to comment.