Skip to content

Commit

Permalink
Add support for is* accessors in ClosureExpressionVisitor
Browse files Browse the repository at this point in the history
When your property is called `private $isActive = true` with `function isActive()` as the getter, it should be possible to query it with `isActive = true` without getting errors.
  • Loading branch information
ruudk committed Mar 5, 2019
1 parent 555e82d commit f43cf44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function is_array;
use function iterator_to_array;
use function method_exists;
use function preg_match;
use function preg_replace_callback;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -44,11 +45,13 @@ public static function getObjectFieldValue($object, $field)
foreach ($accessors as $accessor) {
$accessor .= $field;

if (! method_exists($object, $accessor)) {
continue;
if (method_exists($object, $accessor)) {
return $object->$accessor();
}
}

return $object->$accessor();
if (preg_match('/^is[A-Z]+/', $field) === 1 && method_exists($object, $field)) {
return $object->$field();
}

// __call should be triggered for get.
Expand All @@ -74,11 +77,9 @@ public static function getObjectFieldValue($object, $field)
foreach ($accessors as $accessor) {
$accessor .= $ccField;

if (! method_exists($object, $accessor)) {
continue;
if (method_exists($object, $accessor)) {
return $object->$accessor();
}

return $object->$accessor();
}

return $object->$field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function testGetObjectFieldValueIsAccessor() : void
self::assertTrue($this->visitor->getObjectFieldValue($object, 'baz'));
}

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

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

public function testGetObjectFieldValueIsAccessorCamelCase() : void
{
$object = new TestObjectNotCamelCase(1);
Expand Down

0 comments on commit f43cf44

Please sign in to comment.