Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(value-extractor-trait): Fix #122 #123

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Tool/ValueExtractorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Ramsey\Collection\Exception\InvalidPropertyOrMethod;
use Ramsey\Collection\Exception\UnsupportedOperationException;
use ReflectionProperty;

use function is_array;
use function is_object;
Expand Down Expand Up @@ -64,6 +65,15 @@ protected function extractValue(mixed $element, ?string $propertyOrMethod): mixe
));
}

if (property_exists($element, $propertyOrMethod) && method_exists($element, $propertyOrMethod)) {
$reflectionProperty = new ReflectionProperty($element, $propertyOrMethod);
if ($reflectionProperty->isPublic()) {
return $element->$propertyOrMethod;
}

return $element->{$propertyOrMethod}();
}

if (property_exists($element, $propertyOrMethod)) {
return $element->$propertyOrMethod;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Tool/ValueExtractorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,67 @@ public function getType(): string

$this->assertSame('works!', $test('testProperty'), 'Could not extract value by property');
}

public function testShouldExtractValueByMethodWhenPrivatePropertyExistsWithSameName(): void
{
$test = new class {
use ValueExtractorTrait;

/**
* @return mixed
*/
public function __invoke(mixed $element, string $propertyOrMethod)
{
return $this->extractValue($element, $propertyOrMethod);
}

public function getType(): string
{
return 'fudge';
}
};

$element = new class {
private string $testProperty = 'works!';

public function testProperty(): string
{
return $this->testProperty;
}
};

$this->assertSame('works!', $test($element, 'testProperty'), 'Could not extract value by method');
}

public function testShouldExtractValueByPropertyWhenPrivateMethodExistsWithSameName(): void
{
$test = new class {
use ValueExtractorTrait;

/**
* @return mixed
*/
public function __invoke(mixed $element, string $propertyOrMethod)
{
return $this->extractValue($element, $propertyOrMethod);
}

public function getType(): string
{
return 'fudge';
}
};

$element = new class {
public string $testProperty = 'works!';

/** @phpstan-ignore-next-line */
private function testProperty(): string
{
return 'does not work!';
}
};

$this->assertSame('works!', $test($element, 'testProperty'), 'Could not extract value by property');
}
}