Skip to content

Commit

Permalink
Workaround SmartObject validation in __set() magic
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Dec 24, 2022
1 parent 190b346 commit ff68f57
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ parameters:
count: 1
path: tests/KdybyTests/Autowired/PropertiesFixtures/NonPresenterComponent.php

-
message: "#^KdybyTests\\\\Autowired\\\\PropertiesFixtures\\\\NonPresenterComponent\\:\\:__set\\(\\) calls parent\\:\\:__set\\(\\) but KdybyTests\\\\Autowired\\\\PropertiesFixtures\\\\NonPresenterComponent does not extend any class\\.$#"
count: 1
path: tests/KdybyTests/Autowired/PropertiesFixtures/NonPresenterComponent.php

-
message: "#^Offset 'arguments' on array\\{type\\: class\\-string, factory\\: class\\-string, arguments\\: array\\} in isset\\(\\) always exists and is not nullable\\.$#"
count: 1
Expand Down
11 changes: 11 additions & 0 deletions src/Kdyby/Autowired/AutowireProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ public function &__get(string $name): mixed
return $this->{$name};
}

public function __set(string $name, mixed $value): void
{
if (! isset($this->autowirePropertiesMeta[$name])) {
parent::__set($name, $value);
return;
}

// Assign directly bypassing magic from parents (e.g. SmartObject validation)
$this->{$name} = $value;
}

private function createAutowiredPropertyService(string $name): object
{
if (array_key_exists('factory', $this->autowirePropertiesMeta[$name])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/KdybyTests/Autowired/AutowirePropertiesTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AutowirePropertiesTest extends ContainerTestCase
Assert::type(SampleService::class, $control->service);

Assert::false(isset($control->serviceInTrait));
Assert::type(SampleService::class, $control->serviceInTrait);
Assert::type(SampleService::class, $control->getServiceInTrait());

Assert::false(isset($control->factoryResult));
Assert::type(SampleService::class, $control->factoryResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IntegrationPresenter extends Nette\Application\UI\Presenter
use Kdyby\Autowired\AutowireComponentFactories;

#[Autowire]
public LoremService $service;
protected LoremService $service;

#[Autowire(factory: DatagridFactory::class)]
public DatagridComponent $factoryResult;
Expand All @@ -24,4 +24,9 @@ protected function createComponentSilly(DatagridFactory $factory): DatagridCompo
return $factory->create();
}

public function getService(): LoremService
{
return $this->service;
}

}
2 changes: 1 addition & 1 deletion tests/KdybyTests/Autowired/IntegrationTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IntegrationTest extends ContainerTestCase
$presenter = $container->getByName('presenter');
Assert::type(IntegrationPresenter::class, $presenter);

Assert::type(LoremService::class, $presenter->service);
Assert::type(LoremService::class, $presenter->getService());
Assert::type(DatagridComponent::class, $presenter->factoryResult);
Assert::type(DatagridComponent::class, $presenter->getComponent('silly'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ trait AutowireAttributeTrait
{

#[Autowire]
public SampleService $serviceInTrait;
protected SampleService $serviceInTrait;

#[Autowire(factory: SampleServiceFactory::class, arguments: ['attribute trait'])]
public SampleService $factoryResultInTrait;

public function getServiceInTrait(): SampleService
{
return $this->serviceInTrait;
}

}

0 comments on commit ff68f57

Please sign in to comment.