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

Allow accessing fields with a method of the same name #263

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ClosureExpressionVisitor extends ExpressionVisitor
{
/**
* Accesses the field of a given object. This field has to be public
* directly or indirectly (through an accessor get*, is*, or a magic
* method, __get, __call).
* directly or indirectly (through an accessor of the same name, get*, is*,
* or a magic method, __get, __call).
*
* @param object|array $object
*
Expand All @@ -41,6 +41,10 @@ public static function getObjectFieldValue($object, string $field)
return $object[$field];
}

if (method_exists($object, $field)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method_exists also returns true if the method is not public.
https://3v4l.org/LAgYk

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly the same as it works without my changes

return $object->$field();
}

$accessors = ['get', 'is'];

foreach ($accessors as $accessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function testGetObjectFieldValueIsAccessorWithIsPrefix() : void
self::assertTrue($this->visitor->getObjectFieldValue($object, 'isBaz'));
}

public function testGetObjectFieldValueAccessorWithoutPrefix() : void
{
$object = new TestObject(1, 2, 3, true);

self::assertTrue($this->visitor->getObjectFieldValue($object, 'quux'));
}

public function testGetObjectFieldValueIsAccessorCamelCase() : void
{
$object = new TestObjectNotCamelCase(1);
Expand Down Expand Up @@ -279,18 +286,22 @@ class TestObject
/** @var mixed */
private $qux;

/** @var mixed */
private $quux;

/**
* @param mixed $foo
* @param mixed $bar
* @param mixed $baz
* @param mixed $qux
*/
public function __construct($foo = null, $bar = null, $baz = null, $qux = null)
public function __construct($foo = null, $bar = null, $baz = null, $qux = null, $quux = null)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
$this->qux = $qux;
$this->quux = $qux;
}

/**
Expand Down Expand Up @@ -328,6 +339,14 @@ public function isBaz()
{
return $this->baz;
}

/**
* @return mixed
*/
public function quux()
{
return $this->quux;
}
}

class TestObjectNotCamelCase
Expand Down